From 88b782350ffb73630fbee60af7522a0d7e9fde71 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Fri, 14 Oct 2022 15:42:07 +0200 Subject: [PATCH 01/10] [fix] more info about prerendering errors Helps with #7183 and #7244 --- .changeset/tame-bats-tell.md | 6 +++++ documentation/docs/13-page-options.md | 6 +++-- documentation/docs/16-configuration.md | 2 +- packages/adapter-static/index.js | 24 +++++++++++++++---- packages/kit/src/core/prerender/prerender.js | 4 ++-- packages/kit/src/utils/filesystem.js | 7 +++++- .../kit/test/build-errors/prerender.spec.js | 2 +- 7 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 .changeset/tame-bats-tell.md diff --git a/.changeset/tame-bats-tell.md b/.changeset/tame-bats-tell.md new file mode 100644 index 000000000000..a203217a4af9 --- /dev/null +++ b/.changeset/tame-bats-tell.md @@ -0,0 +1,6 @@ +--- +'@sveltejs/adapter-static': patch +'@sveltejs/kit': patch +--- + +[fix] more info about prerendering errors diff --git a/documentation/docs/13-page-options.md b/documentation/docs/13-page-options.md index c9dfaef92b1b..4fb2fc7a0963 100644 --- a/documentation/docs/13-page-options.md +++ b/documentation/docs/13-page-options.md @@ -72,13 +72,15 @@ Note that this will disable client-side routing for any navigation from this pag #### Troubleshooting -If you encounter an error like 'The following routes were marked as prerenderable, but were not prerendered' it's because the route in question (or a parent layout, if it's a page) has `export const prerender = true` but the page wasn't actually prerendered. +If you encounter an error like 'The following routes were marked as prerenderable, but were not prerendered' it's because the route in question (or a parent layout, if it's a page) has `export const prerender = true` but the page wasn't actually prerendered (because they were not reached by the prerendering crawler). Since these routes cannot be dynamically server-rendered, this will cause errors when people try to access the route in question. There are two ways to fix it: -* Ensure that SvelteKit can find the route by following links from [`config.kit.prerender.entries`](/docs/configuration#prerender). The pages containing the links (e.g. your `/` page) must _themselves_ be prerenderable, or they will be ignored +* Ensure that SvelteKit can find the route by following links from [`config.kit.prerender.entries`](/docs/configuration#prerender). Add links to dynamic routes (i.e. pages with `[parameters]` ) to this option if they are not found through crawling the other entry points, else they are not prerendered because SvelteKit doesn't know what value the parameters should have. The pages containing the links (e.g. your `/` page) must _themselves_ be prerenderable, or they will be ignored. * Change `export const prerender = true` to `export const prerender = 'auto'`. Routes with `'auto'` can be dynamically server rendered +If you are using `@sveltejs/adapter-static`, _all_ pages (and endpoints, if any) must be prerendered, else you need to use a different adapter. + ### ssr Normally, SvelteKit renders your page on the server first and sends that HTML to the client where it's hydrated. If you set `ssr` to `false`, it renders an empty 'shell' page instead. This is useful if your page is unable to be rendered on the server, but in most situations it's not recommended ([see appendix](/docs/appendix#ssr)). diff --git a/documentation/docs/16-configuration.md b/documentation/docs/16-configuration.md index 0b9319dfd8bf..8b1ab34fb00a 100644 --- a/documentation/docs/16-configuration.md +++ b/documentation/docs/16-configuration.md @@ -252,7 +252,7 @@ See [Prerendering](/docs/page-options#prerender). An object containing zero or m - `concurrency` — how many pages can be prerendered simultaneously. JS is single-threaded, but in cases where prerendering performance is network-bound (for example loading content from a remote CMS) this can speed things up by processing other tasks while waiting on the network response - `crawl` — determines whether SvelteKit should find pages to prerender by following links from the seed page(s) - `enabled` — set to `false` to disable prerendering altogether -- `entries` — an array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]` ) +- `entries` — an array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]` ), because SvelteKit doesn't know what value the parameters should have - `onError` - `'fail'` — (default) fails the build when a routing error is encountered when following a link diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index a32a8010b7d1..feec951a710c 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -26,11 +26,27 @@ export default function (options) { if (dynamic_routes.length > 0) { const prefix = path.relative('.', builder.config.kit.files.routes); + const has_routes_with_params = dynamic_routes.some((route) => route.includes('[')); + const config_option = + JSON.stringify(builder.config.kit.prerender.entries) === '["*"]' && + !has_routes_with_params + ? '' + : ` - adjust the \`prerender.entries\` config option ${ + has_routes_with_params + ? `(routes with parameters are not part of entry points by default)` + : '' + } — see https://kit.svelte.dev/docs/configuration#prerender for more info\n`; builder.log.error( - `@sveltejs/adapter-static: all routes must be fully prerenderable (unless using the 'fallback' option — see https://github.com/sveltejs/kit/tree/master/packages/adapter-static#spa-mode). Try adding \`export const prerender = true\` to your root +layout.js/.ts file — see https://kit.svelte.dev/docs/page-options#prerender for more details` - ); - builder.log.error( - dynamic_routes.map((id) => ` - ${path.posix.join(prefix, id)}`).join('\n') + `@sveltejs/adapter-static: all routes must be fully prerenderable, but found the following routes that are dynamic: +${dynamic_routes.map((id) => ` - ${path.posix.join(prefix, id)}`).join('\n')} + +You have the following options: + - set the 'fallback' option — see https://github.com/sveltejs/kit/tree/master/packages/adapter-static#spa-mode for more info + - add \`export const prerender = true\` to your root \`+layout.js/.ts\` or \`+layout.server.js/.ts\` file. This will try to prerender all pages. + - add \`export const prerender = true\` to your \`+server.js/ts\` files (if any) that are not called through pages (else these are not prerendered). +${config_option} +If this doesn't help, you may need to use a different adapter. @sveltejs/adapter-static can only be used for sites that don't need a backend (i.e. a static file server is enough). +See https://kit.svelte.dev/docs/page-options#prerender for more details` ); throw new Error('Encountered dynamic routes'); } diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js index 65d755c13164..347432b17919 100644 --- a/packages/kit/src/core/prerender/prerender.js +++ b/packages/kit/src/core/prerender/prerender.js @@ -431,9 +431,9 @@ export async function prerender() { if (not_prerendered.length > 0) { throw new Error( - `The following routes were marked as prerenderable, but were not prerendered:\n${not_prerendered.map( + `The following routes were marked as prerenderable, but were not prerendered, because they were not found while crawling your app:\n${not_prerendered.map( (id) => ` - ${id}` - )}\n\nSee https://kit.svelte.dev/docs/page-options#prerender-troubleshooting for more info` + )}\n\nSee https://kit.svelte.dev/docs/page-options#prerender-troubleshooting for more info and how to solve this` ); } diff --git a/packages/kit/src/utils/filesystem.js b/packages/kit/src/utils/filesystem.js index 8ddcc0c5c28a..1cd68818678c 100644 --- a/packages/kit/src/utils/filesystem.js +++ b/packages/kit/src/utils/filesystem.js @@ -6,7 +6,12 @@ export function mkdirp(dir) { try { fs.mkdirSync(dir, { recursive: true }); } catch (/** @type {any} */ e) { - if (e.code === 'EEXIST') return; + if (e.code === 'EEXIST') { + if (!fs.statSync(dir).isDirectory()) { + throw new Error(`Cannot create directory ${dir}, a file already exists at this position`); + } + return; + } throw e; } } diff --git a/packages/kit/test/build-errors/prerender.spec.js b/packages/kit/test/build-errors/prerender.spec.js index 3ddf2c352be8..122a63a57421 100644 --- a/packages/kit/test/build-errors/prerender.spec.js +++ b/packages/kit/test/build-errors/prerender.spec.js @@ -11,7 +11,7 @@ test('prerenderable routes must be prerendered', () => { stdio: 'pipe', timeout: 15000 }), - /The following routes were marked as prerenderable, but were not prerendered:\r?\n - \[x\]/gs + /The following routes were marked as prerenderable, but were not prerendered, because they were not found while crawling your app:\r?\n - \[x\]/gs ); }); From 822b6396dbc7cc5552c19c395a44cbe0bcf25cb3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 16 Oct 2022 14:12:01 -0400 Subject: [PATCH 02/10] Apply suggestions from code review --- documentation/docs/13-page-options.md | 2 +- documentation/docs/16-configuration.md | 2 +- packages/kit/src/core/prerender/prerender.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/documentation/docs/13-page-options.md b/documentation/docs/13-page-options.md index 4fb2fc7a0963..d2bbd7ebd46a 100644 --- a/documentation/docs/13-page-options.md +++ b/documentation/docs/13-page-options.md @@ -79,7 +79,7 @@ Since these routes cannot be dynamically server-rendered, this will cause errors * Ensure that SvelteKit can find the route by following links from [`config.kit.prerender.entries`](/docs/configuration#prerender). Add links to dynamic routes (i.e. pages with `[parameters]` ) to this option if they are not found through crawling the other entry points, else they are not prerendered because SvelteKit doesn't know what value the parameters should have. The pages containing the links (e.g. your `/` page) must _themselves_ be prerenderable, or they will be ignored. * Change `export const prerender = true` to `export const prerender = 'auto'`. Routes with `'auto'` can be dynamically server rendered -If you are using `@sveltejs/adapter-static`, _all_ pages (and endpoints, if any) must be prerendered, else you need to use a different adapter. +If you are using `@sveltejs/adapter-static`, _all_ pages (and endpoints, if any) must be prerendered, otherwise you need to use a different adapter. ### ssr diff --git a/documentation/docs/16-configuration.md b/documentation/docs/16-configuration.md index 8b1ab34fb00a..e5f145453faf 100644 --- a/documentation/docs/16-configuration.md +++ b/documentation/docs/16-configuration.md @@ -252,7 +252,7 @@ See [Prerendering](/docs/page-options#prerender). An object containing zero or m - `concurrency` — how many pages can be prerendered simultaneously. JS is single-threaded, but in cases where prerendering performance is network-bound (for example loading content from a remote CMS) this can speed things up by processing other tasks while waiting on the network response - `crawl` — determines whether SvelteKit should find pages to prerender by following links from the seed page(s) - `enabled` — set to `false` to disable prerendering altogether -- `entries` — an array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]` ), because SvelteKit doesn't know what value the parameters should have +- `entries` — an array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]`, because SvelteKit doesn't know what value the parameters should have) - `onError` - `'fail'` — (default) fails the build when a routing error is encountered when following a link diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js index 347432b17919..4ce344089435 100644 --- a/packages/kit/src/core/prerender/prerender.js +++ b/packages/kit/src/core/prerender/prerender.js @@ -433,7 +433,7 @@ export async function prerender() { throw new Error( `The following routes were marked as prerenderable, but were not prerendered, because they were not found while crawling your app:\n${not_prerendered.map( (id) => ` - ${id}` - )}\n\nSee https://kit.svelte.dev/docs/page-options#prerender-troubleshooting for more info and how to solve this` + )}\n\nSee https://kit.svelte.dev/docs/page-options#prerender-troubleshooting for info on how to solve this` ); } From 470ae897f1465a62f692968ae3dbc1da83bf7580 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 16 Oct 2022 14:16:03 -0400 Subject: [PATCH 03/10] swap ternary around, remove dead code --- packages/adapter-static/index.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index feec951a710c..dddd6a626624 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -29,13 +29,10 @@ export default function (options) { const has_routes_with_params = dynamic_routes.some((route) => route.includes('[')); const config_option = JSON.stringify(builder.config.kit.prerender.entries) === '["*"]' && - !has_routes_with_params - ? '' - : ` - adjust the \`prerender.entries\` config option ${ - has_routes_with_params - ? `(routes with parameters are not part of entry points by default)` - : '' - } — see https://kit.svelte.dev/docs/configuration#prerender for more info\n`; + has_routes_with_params + ? ` - adjust the \`prerender.entries\` config option (routes with parameters are not part of entry points by default) — see https://kit.svelte.dev/docs/configuration#prerender for more info\n` + : ''; + builder.log.error( `@sveltejs/adapter-static: all routes must be fully prerenderable, but found the following routes that are dynamic: ${dynamic_routes.map((id) => ` - ${path.posix.join(prefix, id)}`).join('\n')} From cd4575c6693ed8d90b1eaff9ee948d3285c5eceb Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 16 Oct 2022 14:38:48 -0400 Subject: [PATCH 04/10] small tweak --- packages/adapter-static/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index dddd6a626624..c83c4801702c 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -30,7 +30,7 @@ export default function (options) { const config_option = JSON.stringify(builder.config.kit.prerender.entries) === '["*"]' && has_routes_with_params - ? ` - adjust the \`prerender.entries\` config option (routes with parameters are not part of entry points by default) — see https://kit.svelte.dev/docs/configuration#prerender for more info\n` + ? ' - adjust the `prerender.entries` config option (routes with parameters are not part of entry points by default) — see https://kit.svelte.dev/docs/configuration#prerender for more info.\n' : ''; builder.log.error( From 6eacaabb2a37f2ed2429ca01c023a97e5347cdaa Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 16 Oct 2022 14:40:20 -0400 Subject: [PATCH 05/10] another minor tweak --- packages/adapter-static/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index c83c4801702c..db4011a995f1 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -26,10 +26,9 @@ export default function (options) { if (dynamic_routes.length > 0) { const prefix = path.relative('.', builder.config.kit.files.routes); - const has_routes_with_params = dynamic_routes.some((route) => route.includes('[')); const config_option = - JSON.stringify(builder.config.kit.prerender.entries) === '["*"]' && - has_routes_with_params + dynamic_routes.some((route) => route.includes('[')) && + JSON.stringify(builder.config.kit.prerender.entries) === '["*"]' ? ' - adjust the `prerender.entries` config option (routes with parameters are not part of entry points by default) — see https://kit.svelte.dev/docs/configuration#prerender for more info.\n' : ''; From 505979bf06561263b72f77dfe5120ef0fe3f14b7 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 17 Oct 2022 09:49:50 +0200 Subject: [PATCH 06/10] drive-by-fix --- packages/kit/src/core/adapt/builder.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/kit/src/core/adapt/builder.js b/packages/kit/src/core/adapt/builder.js index 3c1dc0bcda3f..c95921a4ba7f 100644 --- a/packages/kit/src/core/adapt/builder.js +++ b/packages/kit/src/core/adapt/builder.js @@ -33,10 +33,11 @@ export function create_builder({ config, build_data, routes, prerendered, log }) async createEntries(fn) { /** @type {import('types').RouteDefinition[]} */ const facades = routes.map((route) => { + /** @type {Set} */ const methods = new Set(); if (route.page) { - methods.add('SET'); + methods.add('GET'); } if (route.endpoint) { From d5e1347c593a1ce18f092996d0101fd03a193a49 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 17 Oct 2022 09:50:17 +0200 Subject: [PATCH 07/10] tweak when prerender.entries suggestion appears --- packages/adapter-static/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index db4011a995f1..faea4fe6b87e 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -26,10 +26,14 @@ export default function (options) { if (dynamic_routes.length > 0) { const prefix = path.relative('.', builder.config.kit.files.routes); + const has_param_routes = dynamic_routes.some((route) => route.includes('[')); const config_option = - dynamic_routes.some((route) => route.includes('[')) && - JSON.stringify(builder.config.kit.prerender.entries) === '["*"]' - ? ' - adjust the `prerender.entries` config option (routes with parameters are not part of entry points by default) — see https://kit.svelte.dev/docs/configuration#prerender for more info.\n' + has_param_routes || JSON.stringify(builder.config.kit.prerender.entries) !== '["*"]' + ? ` - adjust the \`prerender.entries\` config option ${ + has_param_routes + ? '(routes with parameters are not part of entry points by default)' + : '' + } — see https://kit.svelte.dev/docs/configuration#prerender for more info.\n` : ''; builder.log.error( From d437e8cc087505150a4187617677802a5e52536f Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 17 Oct 2022 09:56:56 +0200 Subject: [PATCH 08/10] tweak --- documentation/docs/13-page-options.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/documentation/docs/13-page-options.md b/documentation/docs/13-page-options.md index d2bbd7ebd46a..ca9ab8bf4f80 100644 --- a/documentation/docs/13-page-options.md +++ b/documentation/docs/13-page-options.md @@ -72,11 +72,11 @@ Note that this will disable client-side routing for any navigation from this pag #### Troubleshooting -If you encounter an error like 'The following routes were marked as prerenderable, but were not prerendered' it's because the route in question (or a parent layout, if it's a page) has `export const prerender = true` but the page wasn't actually prerendered (because they were not reached by the prerendering crawler). +If you encounter an error like 'The following routes were marked as prerenderable, but were not prerendered' it's because the route in question (or a parent layout, if it's a page) has `export const prerender = true` but the page wasn't actually prerendered, because it wasn't reached by the prerendering crawler. Since these routes cannot be dynamically server-rendered, this will cause errors when people try to access the route in question. There are two ways to fix it: -* Ensure that SvelteKit can find the route by following links from [`config.kit.prerender.entries`](/docs/configuration#prerender). Add links to dynamic routes (i.e. pages with `[parameters]` ) to this option if they are not found through crawling the other entry points, else they are not prerendered because SvelteKit doesn't know what value the parameters should have. The pages containing the links (e.g. your `/` page) must _themselves_ be prerenderable, or they will be ignored. +* Ensure that SvelteKit can find the route by following links from [`config.kit.prerender.entries`](/docs/configuration#prerender). Add links to dynamic routes (i.e. pages with `[parameters]` ) to this option if they are not found through crawling the other entry points, else they are not prerendered because SvelteKit doesn't know what value the parameters should have. Pages not marked as prerenderable will be ignored and their links to other pages will not be crawled, even if some of them would be prerenderable. * Change `export const prerender = true` to `export const prerender = 'auto'`. Routes with `'auto'` can be dynamically server rendered If you are using `@sveltejs/adapter-static`, _all_ pages (and endpoints, if any) must be prerendered, otherwise you need to use a different adapter. From d46e545c210272484ddf6c43db3c09c317468597 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 17 Oct 2022 10:34:24 +0200 Subject: [PATCH 09/10] add strict option --- .changeset/tame-bats-tell.md | 2 +- packages/adapter-static/README.md | 7 ++++++- packages/adapter-static/index.d.ts | 1 + packages/adapter-static/index.js | 10 ++++++---- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.changeset/tame-bats-tell.md b/.changeset/tame-bats-tell.md index a203217a4af9..9480534b0a27 100644 --- a/.changeset/tame-bats-tell.md +++ b/.changeset/tame-bats-tell.md @@ -3,4 +3,4 @@ '@sveltejs/kit': patch --- -[fix] more info about prerendering errors +[feat] more info about prerendering errors, add strict option to adapter-static diff --git a/packages/adapter-static/README.md b/packages/adapter-static/README.md index bd90b5a2d746..9c12b12bc4f1 100644 --- a/packages/adapter-static/README.md +++ b/packages/adapter-static/README.md @@ -18,7 +18,8 @@ export default { pages: 'build', assets: 'build', fallback: null, - precompress: false + precompress: false, + strict: true }) } }; @@ -71,6 +72,10 @@ Specify a fallback page for SPA mode, e.g. `index.html` or `200.html` or `404.ht If `true`, precompresses files with brotli and gzip. This will generate `.br` and `.gz` files. +### strict + +By default, `adapter-static` checks that either all pages and endpoints (if any) of your app were prerendered, or you have the `fallback` option set. This check exists to prevent you from accidentally publishing an app where some parts of it are not accessible, because they are not contained in the final output. If you know this is ok (for example when a certain page only exists conditionally), you can set `strict` to `false` to turn off this check. + ## SPA mode You can use `adapter-static` to create a single-page app or SPA by specifying a **fallback page**. diff --git a/packages/adapter-static/index.d.ts b/packages/adapter-static/index.d.ts index a8544910102d..101e02fdef12 100644 --- a/packages/adapter-static/index.d.ts +++ b/packages/adapter-static/index.d.ts @@ -5,6 +5,7 @@ export interface AdapterOptions { assets?: string; fallback?: string; precompress?: boolean; + strict?: boolean; } export default function plugin(options?: AdapterOptions): Adapter; diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index faea4fe6b87e..11b6d38bf018 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -24,7 +24,7 @@ export default function (options) { }; }); - if (dynamic_routes.length > 0) { + if (dynamic_routes.length > 0 && options.strict) { const prefix = path.relative('.', builder.config.kit.files.routes); const has_param_routes = dynamic_routes.some((route) => route.includes('[')); const config_option = @@ -33,7 +33,7 @@ export default function (options) { has_param_routes ? '(routes with parameters are not part of entry points by default)' : '' - } — see https://kit.svelte.dev/docs/configuration#prerender for more info.\n` + } — see https://kit.svelte.dev/docs/configuration#prerender for more info.` : ''; builder.log.error( @@ -41,11 +41,13 @@ export default function (options) { ${dynamic_routes.map((id) => ` - ${path.posix.join(prefix, id)}`).join('\n')} You have the following options: - - set the 'fallback' option — see https://github.com/sveltejs/kit/tree/master/packages/adapter-static#spa-mode for more info + - set the \`fallback\` option — see https://github.com/sveltejs/kit/tree/master/packages/adapter-static#spa-mode for more info. - add \`export const prerender = true\` to your root \`+layout.js/.ts\` or \`+layout.server.js/.ts\` file. This will try to prerender all pages. - add \`export const prerender = true\` to your \`+server.js/ts\` files (if any) that are not called through pages (else these are not prerendered). ${config_option} -If this doesn't help, you may need to use a different adapter. @sveltejs/adapter-static can only be used for sites that don't need a backend (i.e. a static file server is enough). + - set the \`strict\` option to \`false\` to ignore this error. Only do this if you are sure you don't need the routes in question in your final app, they can't be accessed — see https://github.com/sveltejs/kit/tree/master/packages/adapter-static#strict for more info. + +If this doesn't help, you may need to use a different adapter. @sveltejs/adapter-static can only be used for sites that don't need their own backend (i.e. a static file server is enough). See https://kit.svelte.dev/docs/page-options#prerender for more details` ); throw new Error('Encountered dynamic routes'); From 02d41ac75affd1296fdd45b0700763a6c96d946c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 18 Oct 2022 12:58:21 -0400 Subject: [PATCH 10/10] Apply suggestions from code review --- documentation/docs/13-page-options.md | 2 -- packages/adapter-static/index.js | 6 +++--- packages/kit/src/core/prerender/prerender.js | 2 +- packages/kit/test/build-errors/prerender.spec.js | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/documentation/docs/13-page-options.md b/documentation/docs/13-page-options.md index ca9ab8bf4f80..20f30913e1b5 100644 --- a/documentation/docs/13-page-options.md +++ b/documentation/docs/13-page-options.md @@ -79,8 +79,6 @@ Since these routes cannot be dynamically server-rendered, this will cause errors * Ensure that SvelteKit can find the route by following links from [`config.kit.prerender.entries`](/docs/configuration#prerender). Add links to dynamic routes (i.e. pages with `[parameters]` ) to this option if they are not found through crawling the other entry points, else they are not prerendered because SvelteKit doesn't know what value the parameters should have. Pages not marked as prerenderable will be ignored and their links to other pages will not be crawled, even if some of them would be prerenderable. * Change `export const prerender = true` to `export const prerender = 'auto'`. Routes with `'auto'` can be dynamically server rendered -If you are using `@sveltejs/adapter-static`, _all_ pages (and endpoints, if any) must be prerendered, otherwise you need to use a different adapter. - ### ssr Normally, SvelteKit renders your page on the server first and sends that HTML to the client where it's hydrated. If you set `ssr` to `false`, it renders an empty 'shell' page instead. This is useful if your page is unable to be rendered on the server, but in most situations it's not recommended ([see appendix](/docs/appendix#ssr)). diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index 11b6d38bf018..37c3f8538cc6 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -43,11 +43,11 @@ ${dynamic_routes.map((id) => ` - ${path.posix.join(prefix, id)}`).join('\n')} You have the following options: - set the \`fallback\` option — see https://github.com/sveltejs/kit/tree/master/packages/adapter-static#spa-mode for more info. - add \`export const prerender = true\` to your root \`+layout.js/.ts\` or \`+layout.server.js/.ts\` file. This will try to prerender all pages. - - add \`export const prerender = true\` to your \`+server.js/ts\` files (if any) that are not called through pages (else these are not prerendered). + - add \`export const prerender = true\` to any \`+server.js/ts\` files that are not fetched by page \`load\` functions. ${config_option} - - set the \`strict\` option to \`false\` to ignore this error. Only do this if you are sure you don't need the routes in question in your final app, they can't be accessed — see https://github.com/sveltejs/kit/tree/master/packages/adapter-static#strict for more info. + - pass \`strict: false\` to \`adapter-static\` to ignore this error. Only do this if you are sure you don't need the routes in question in your final app, as they will be unavailable. See https://github.com/sveltejs/kit/tree/master/packages/adapter-static#strict for more info. -If this doesn't help, you may need to use a different adapter. @sveltejs/adapter-static can only be used for sites that don't need their own backend (i.e. a static file server is enough). +If this doesn't help, you may need to use a different adapter. @sveltejs/adapter-static can only be used for sites that don't need a server for dynamic rendering, and can run on just a static file server. See https://kit.svelte.dev/docs/page-options#prerender for more details` ); throw new Error('Encountered dynamic routes'); diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js index 4ce344089435..40d04eb4d135 100644 --- a/packages/kit/src/core/prerender/prerender.js +++ b/packages/kit/src/core/prerender/prerender.js @@ -431,7 +431,7 @@ export async function prerender() { if (not_prerendered.length > 0) { throw new Error( - `The following routes were marked as prerenderable, but were not prerendered, because they were not found while crawling your app:\n${not_prerendered.map( + `The following routes were marked as prerenderable, but were not prerendered because they were not found while crawling your app:\n${not_prerendered.map( (id) => ` - ${id}` )}\n\nSee https://kit.svelte.dev/docs/page-options#prerender-troubleshooting for info on how to solve this` ); diff --git a/packages/kit/test/build-errors/prerender.spec.js b/packages/kit/test/build-errors/prerender.spec.js index 122a63a57421..209a3dd2b6c7 100644 --- a/packages/kit/test/build-errors/prerender.spec.js +++ b/packages/kit/test/build-errors/prerender.spec.js @@ -11,7 +11,7 @@ test('prerenderable routes must be prerendered', () => { stdio: 'pipe', timeout: 15000 }), - /The following routes were marked as prerenderable, but were not prerendered, because they were not found while crawling your app:\r?\n - \[x\]/gs + /The following routes were marked as prerenderable, but were not prerendered because they were not found while crawling your app:\r?\n - \[x\]/gs ); });