Skip to content

Commit 23cf4ab

Browse files
fix: enhance and tighten up Vercel adapter warnings (#9436)
- warn when prerender setting makes isr config useless - don't show cron warning when everything's valid - allow to set isr to false to clear isr config in leafs (else it's impossible to do so because config is merged at the top level) - prefixed all warnings with "Warning:" which should help detect the Vercel log dashboard show these in condensed mode where only warnings/errors are shown - prerender = 'auto' routes are now also excempt from further processing if we can determine that this is equal to prerender = true (because there are no dynamic routes) --------- Co-authored-by: Rich Harris <[email protected]>
1 parent b2dbbdf commit 23cf4ab

File tree

6 files changed

+80
-31
lines changed

6 files changed

+80
-31
lines changed

.changeset/forty-suns-listen.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-vercel': minor
3+
---
4+
5+
feat: warn when prerender setting makes isr config useless

.changeset/polite-islands-glow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-vercel': patch
3+
---
4+
5+
fix: don't show cron warning when everything's valid

.changeset/tasty-weeks-rush.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-vercel': patch
3+
---
4+
5+
fix: allow to set isr to false to clear isr config in leafs

packages/adapter-vercel/index.d.ts

+19-17
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,25 @@ export interface ServerlessConfig {
3232
* [Incremental Static Regeneration](https://vercel.com/docs/concepts/incremental-static-regeneration/overview) configuration.
3333
* Serverless only.
3434
*/
35-
isr?: {
36-
/**
37-
* Expiration time (in seconds) before the cached asset will be re-generated by invoking the Serverless Function. Setting the value to `false` means it will never expire.
38-
*/
39-
expiration: number | false;
40-
/**
41-
* Random token that can be provided in the URL to bypass the cached version of the asset, by requesting the asset
42-
* with a __prerender_bypass=<token> cookie.
43-
*
44-
* Making a `GET` or `HEAD` request with `x-prerender-revalidate: <token>` will force the asset to be re-validated.
45-
*/
46-
bypassToken?: string;
47-
/**
48-
* List of query string parameter names that will be cached independently. If an empty array, query values are not considered for caching. If undefined each unique query value is cached independently
49-
*/
50-
allowQuery?: string[] | undefined;
51-
};
35+
isr?:
36+
| {
37+
/**
38+
* Expiration time (in seconds) before the cached asset will be re-generated by invoking the Serverless Function. Setting the value to `false` means it will never expire.
39+
*/
40+
expiration: number | false;
41+
/**
42+
* Random token that can be provided in the URL to bypass the cached version of the asset, by requesting the asset
43+
* with a __prerender_bypass=<token> cookie.
44+
*
45+
* Making a `GET` or `HEAD` request with `x-prerender-revalidate: <token>` will force the asset to be re-validated.
46+
*/
47+
bypassToken?: string;
48+
/**
49+
* List of query string parameter names that will be cached independently. If an empty array, query values are not considered for caching. If undefined each unique query value is cached independently
50+
*/
51+
allowQuery?: string[] | undefined;
52+
}
53+
| false;
5254
}
5355

5456
export interface EdgeConfig {

packages/adapter-vercel/index.js

+45-13
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,21 @@ const plugin = function (defaults = {}) {
153153
/** @type {Map<import('@sveltejs/kit').RouteDefinition<import('.').Config>, { expiration: number | false, bypassToken: string | undefined, allowQuery: string[], group: number, passQuery: true }>} */
154154
const isr_config = new Map();
155155

156+
/** @type {Set<string>} */
157+
const ignored_isr = new Set();
158+
156159
// group routes by config
157160
for (const route of builder.routes) {
158-
if (route.prerender === true) continue;
161+
const runtime = route.config?.runtime ?? defaults?.runtime ?? get_default_runtime();
162+
const config = { runtime, ...defaults, ...route.config };
159163

160-
const pattern = route.pattern.toString();
164+
if (is_prerendered(route)) {
165+
if (config.isr) {
166+
ignored_isr.add(route.id);
167+
}
168+
continue;
169+
}
161170

162-
const runtime = route.config?.runtime ?? defaults?.runtime ?? get_default_runtime();
163171
if (runtime && !VALID_RUNTIMES.includes(runtime)) {
164172
throw new Error(
165173
`Invalid runtime '${runtime}' for route ${
@@ -168,8 +176,6 @@ const plugin = function (defaults = {}) {
168176
);
169177
}
170178

171-
const config = { runtime, ...defaults, ...route.config };
172-
173179
if (config.isr) {
174180
const directory = path.relative('.', builder.config.kit.files.routes + route.id);
175181

@@ -197,6 +203,7 @@ const plugin = function (defaults = {}) {
197203
const hash = hash_config(config);
198204

199205
// first, check there are no routes with incompatible configs that will be merged
206+
const pattern = route.pattern.toString();
200207
const existing = conflicts.get(pattern);
201208
if (existing) {
202209
if (existing.hash !== hash) {
@@ -219,6 +226,20 @@ const plugin = function (defaults = {}) {
219226
group.routes.push(route);
220227
}
221228

229+
if (ignored_isr.size) {
230+
builder.log.warn(
231+
`\nWarning: The following routes have an ISR config which is ignored because the route is prerendered:`
232+
);
233+
234+
for (const ignored of ignored_isr) {
235+
console.log(` - ${ignored}`);
236+
}
237+
238+
console.log(
239+
'Either remove the "prerender" option from these routes to use ISR, or remove the ISR config.\n'
240+
);
241+
}
242+
222243
const singular = groups.size === 1;
223244

224245
for (const group of groups.values()) {
@@ -240,7 +261,7 @@ const plugin = function (defaults = {}) {
240261
}
241262

242263
for (const route of builder.routes) {
243-
if (route.prerender === true) continue;
264+
if (is_prerendered(route)) continue;
244265

245266
const pattern = route.pattern.toString();
246267

@@ -456,7 +477,7 @@ async function create_function_bundle(builder, entry, dir, config) {
456477
if (resolution_failures.size > 0) {
457478
const cwd = process.cwd();
458479
builder.log.warn(
459-
'The following modules failed to locate dependencies that may (or may not) be required for your app to work:'
480+
'Warning: The following modules failed to locate dependencies that may (or may not) be required for your app to work:'
460481
);
461482

462483
for (const [importer, modules] of resolution_failures) {
@@ -572,15 +593,26 @@ function validate_vercel_json(builder, vercel_config) {
572593
unmatched_paths.push(path);
573594
}
574595

575-
builder.log.warn(
576-
`\nvercel.json defines cron tasks that use paths that do not correspond to an API route with a GET handler (ignore this if the request is handled in your \`handle\` hook):`
577-
);
596+
if (unmatched_paths.length) {
597+
builder.log.warn(
598+
`\nWarning: vercel.json defines cron tasks that use paths that do not correspond to an API route with a GET handler (ignore this if the request is handled in your \`handle\` hook):`
599+
);
600+
601+
for (const path of unmatched_paths) {
602+
console.log(` - ${path}`);
603+
}
578604

579-
for (const path of unmatched_paths) {
580-
console.log(` - ${path}`);
605+
console.log('');
581606
}
607+
}
582608

583-
console.log('');
609+
/** @param {import('@sveltejs/kit').RouteDefinition} route */
610+
function is_prerendered(route) {
611+
return (
612+
route.prerender === true ||
613+
(route.prerender === 'auto' &&
614+
route.segments.every((segment) => !segment.dynamic))
615+
);
584616
}
585617

586618
export default plugin;

packages/kit/types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export interface Builder {
8787
config: ValidatedConfig;
8888
/** Information about prerendered pages and assets, if any. */
8989
prerendered: Prerendered;
90-
/** An array of dynamic (not prerendered) routes */
90+
/** An array of all routes (including prerendered) */
9191
routes: RouteDefinition[];
9292

9393
/**

0 commit comments

Comments
 (0)