From 21d20c3236a2831516469c219af00d53f32b7caf Mon Sep 17 00:00:00 2001 From: Daniel Imfeld Date: Wed, 13 Apr 2022 08:09:24 -1000 Subject: [PATCH] Revert change to render fallback when prerendering is disabled (#4604) * Revert #4443: Create fallback HTML file when prerendering is disabled Fixes #4492 * Document `config.kit.prerender.entries: []` workaround for SPA mode * add test * tweak changeset * lockfile * Update packages/adapter-static/README.md Co-authored-by: Rich Harris --- .changeset/red-adults-wonder.md | 5 +++ packages/adapter-static/README.md | 2 ++ .../kit/src/core/build/prerender/prerender.js | 34 +++++++++---------- .../test/prerendering/disabled/package.json | 20 +++++++++++ .../test/prerendering/disabled/src/app.html | 11 ++++++ .../test/prerendering/disabled/src/hooks.js | 1 + .../disabled/src/routes/index.svelte | 1 + .../prerendering/disabled/svelte.config.js | 27 +++++++++++++++ pnpm-lock.yaml | 14 ++++++++ 9 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 .changeset/red-adults-wonder.md create mode 100644 packages/kit/test/prerendering/disabled/package.json create mode 100644 packages/kit/test/prerendering/disabled/src/app.html create mode 100644 packages/kit/test/prerendering/disabled/src/hooks.js create mode 100644 packages/kit/test/prerendering/disabled/src/routes/index.svelte create mode 100644 packages/kit/test/prerendering/disabled/svelte.config.js diff --git a/.changeset/red-adults-wonder.md b/.changeset/red-adults-wonder.md new file mode 100644 index 000000000000..111709686a67 --- /dev/null +++ b/.changeset/red-adults-wonder.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Only render fallback when prerendering is enabled diff --git a/packages/adapter-static/README.md b/packages/adapter-static/README.md index 6158f7d1c41e..c6b45666483d 100644 --- a/packages/adapter-static/README.md +++ b/packages/adapter-static/README.md @@ -71,6 +71,8 @@ export default { When operating in SPA mode, you can omit `config.kit.prerender.default` (or set it to `false`, its default value), and only pages that have the [`prerender`](https://kit.svelte.dev/docs/page-options#prerender) option set will be prerendered at build time. +SvelteKit will still crawl your app's entry points looking for prerenderable pages. If `svelte-kit build` fails because of pages that can't be loaded outside the browser, you can set `config.kit.prerender.entries` to `[]` to prevent this from happening. (Setting `config.kit.prerender.enabled` also has this effect, but would prevent the fallback page from being generated.) + > ⚠️ During development, SvelteKit will still attempt to server-side render your routes. This means accessing things that are only available in the browser (such as the `window` object) will result in errors, even though this would be valid in the output app. To align the behavior of SvelteKit's dev mode with your SPA, you can [call `resolve()` with a parameter of `{ssr: false}` inside the `handle()` hook](https://kit.svelte.dev/docs/hooks#handle). ## GitHub Pages diff --git a/packages/kit/src/core/build/prerender/prerender.js b/packages/kit/src/core/build/prerender/prerender.js index b8d20d16e37a..60ecddc2ceb0 100644 --- a/packages/kit/src/core/build/prerender/prerender.js +++ b/packages/kit/src/core/build/prerender/prerender.js @@ -55,6 +55,10 @@ export async function prerender({ config, entries, files, log }) { paths: [] }; + if (!config.kit.prerender.enabled) { + return prerendered; + } + installFetch(); const server_root = join(config.kit.outDir, 'output'); @@ -71,23 +75,6 @@ export async function prerender({ config, entries, files, log }) { const server = new Server(manifest); - const rendered = await server.respond(new Request('http://sveltekit-prerender/[fallback]'), { - getClientAddress, - prerender: { - fallback: true, - default: false, - dependencies: new Map() - } - }); - - const file = `${config.kit.outDir}/output/prerendered/fallback.html`; - mkdirp(dirname(file)); - writeFileSync(file, await rendered.text()); - - if (!config.kit.prerender.enabled) { - return prerendered; - } - const error = normalise_error_handler(log, config.kit.prerender.onError); const q = queue(config.kit.prerender.concurrency); @@ -281,6 +268,19 @@ export async function prerender({ config, entries, files, log }) { await q.done(); } + const rendered = await server.respond(new Request('http://sveltekit-prerender/[fallback]'), { + getClientAddress, + prerender: { + fallback: true, + default: false, + dependencies: new Map() + } + }); + + const file = `${config.kit.outDir}/output/prerendered/fallback.html`; + mkdirp(dirname(file)); + writeFileSync(file, await rendered.text()); + return prerendered; } diff --git a/packages/kit/test/prerendering/disabled/package.json b/packages/kit/test/prerendering/disabled/package.json new file mode 100644 index 000000000000..d671863dd09e --- /dev/null +++ b/packages/kit/test/prerendering/disabled/package.json @@ -0,0 +1,20 @@ +{ + "name": "prerendering-test-basics", + "private": true, + "version": "0.0.2-next.0", + "scripts": { + "dev": "node ../../cli.js dev", + "build": "node ../../cli.js build", + "preview": "node ../../cli.js preview", + "check": "tsc && svelte-check", + "test": "npm run build" + }, + "devDependencies": { + "@sveltejs/kit": "workspace:*", + "svelte": "^3.43.0", + "svelte-check": "^2.5.0", + "typescript": "~4.6.2", + "uvu": "^0.5.2" + }, + "type": "module" +} diff --git a/packages/kit/test/prerendering/disabled/src/app.html b/packages/kit/test/prerendering/disabled/src/app.html new file mode 100644 index 000000000000..11ff812a1abc --- /dev/null +++ b/packages/kit/test/prerendering/disabled/src/app.html @@ -0,0 +1,11 @@ + + + + + + %svelte.head% + + + %svelte.body% + + diff --git a/packages/kit/test/prerendering/disabled/src/hooks.js b/packages/kit/test/prerendering/disabled/src/hooks.js new file mode 100644 index 000000000000..91cd62c5339b --- /dev/null +++ b/packages/kit/test/prerendering/disabled/src/hooks.js @@ -0,0 +1 @@ +throw new Error('this file should not be loaded if prerendering is disabled'); diff --git a/packages/kit/test/prerendering/disabled/src/routes/index.svelte b/packages/kit/test/prerendering/disabled/src/routes/index.svelte new file mode 100644 index 000000000000..5ba6aba84458 --- /dev/null +++ b/packages/kit/test/prerendering/disabled/src/routes/index.svelte @@ -0,0 +1 @@ +

not prerendered

diff --git a/packages/kit/test/prerendering/disabled/svelte.config.js b/packages/kit/test/prerendering/disabled/svelte.config.js new file mode 100644 index 000000000000..fd6c9adbfeda --- /dev/null +++ b/packages/kit/test/prerendering/disabled/svelte.config.js @@ -0,0 +1,27 @@ +import path from 'path'; +import adapter from '../../../../adapter-static/index.js'; + +/** @type {import('@sveltejs/kit').Config} */ +const config = { + kit: { + adapter: adapter(), + + prerender: { + enabled: false + }, + + vite: { + build: { + minify: false + }, + clearScreen: false, + server: { + fs: { + allow: [path.resolve('../../../src')] + } + } + } + } +}; + +export default config; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 56da382a014c..b03b1df7d356 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -380,6 +380,20 @@ importers: typescript: 4.6.2 uvu: 0.5.2 + packages/kit/test/prerendering/disabled: + specifiers: + '@sveltejs/kit': workspace:* + svelte: ^3.43.0 + svelte-check: ^2.5.0 + typescript: ~4.6.2 + uvu: ^0.5.2 + devDependencies: + '@sveltejs/kit': link:../../.. + svelte: 3.44.2 + svelte-check: 2.5.0_svelte@3.44.2 + typescript: 4.6.2 + uvu: 0.5.2 + packages/kit/test/prerendering/options: specifiers: '@sveltejs/kit': workspace:*