diff --git a/.changeset/cuddly-kangaroos-rush.md b/.changeset/cuddly-kangaroos-rush.md new file mode 100644 index 000000000000..295e71171c8e --- /dev/null +++ b/.changeset/cuddly-kangaroos-rush.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: don't automatically prerender non-SSR'd pages diff --git a/packages/kit/src/core/postbuild/analyse.js b/packages/kit/src/core/postbuild/analyse.js index b31df2c5f4e2..a9b1a1d1fb51 100644 --- a/packages/kit/src/core/postbuild/analyse.js +++ b/packages/kit/src/core/postbuild/analyse.js @@ -122,15 +122,7 @@ async function analyse({ manifest_path, env }) { validate_common_exports(page.universal, page.universal_id); } - const should_prerender = get_option(nodes, 'prerender'); - prerender = - should_prerender === true || - // Try prerendering if ssr is false and no server needed. Set it to 'auto' so that - // the route is not removed from the manifest, there could be a server load function. - // People can opt out of this behavior by explicitly setting prerender to false - (should_prerender !== false && get_option(nodes, 'ssr') === false && !page?.server?.actions - ? 'auto' - : should_prerender ?? false); + prerender = get_option(nodes, 'prerender') ?? false; config = get_config(nodes); } diff --git a/packages/kit/src/runtime/server/page/index.js b/packages/kit/src/runtime/server/page/index.js index c649be652bf8..fdf7713be962 100644 --- a/packages/kit/src/runtime/server/page/index.js +++ b/packages/kit/src/runtime/server/page/index.js @@ -79,38 +79,13 @@ export async function render_page(event, page, options, manifest, state, resolve // it's crucial that we do this before returning the non-SSR response, otherwise // SvelteKit will erroneously believe that the path has been prerendered, // causing functions to be omitted from the manifesst generated later - const should_prerender = get_option(nodes, 'prerender'); - + const should_prerender = get_option(nodes, 'prerender') ?? false; if (should_prerender) { const mod = leaf_node.server; if (mod?.actions) { throw new Error('Cannot prerender pages with actions'); } } else if (state.prerendering) { - // Try to render the shell when ssr is false and prerendering not explicitly disabled. - // People can opt out of this behavior by explicitly setting prerender to false. - if ( - should_prerender !== false && - get_option(nodes, 'ssr') === false && - !leaf_node.server?.actions - ) { - return await render_response({ - branch: [], - fetched: [], - page_config: { - ssr: false, - csr: get_option(nodes, 'csr') ?? true - }, - status, - error: null, - event, - options, - manifest, - state, - resolve_opts - }); - } - // if the page isn't marked as prerenderable, then bail out at this point return new Response(undefined, { status: 204 diff --git a/packages/kit/test/prerendering/ssr-false/.gitignore b/packages/kit/test/prerendering/ssr-false/.gitignore deleted file mode 100644 index 1e18f275e97c..000000000000 --- a/packages/kit/test/prerendering/ssr-false/.gitignore +++ /dev/null @@ -1 +0,0 @@ -!.env \ No newline at end of file diff --git a/packages/kit/test/prerendering/ssr-false/package.json b/packages/kit/test/prerendering/ssr-false/package.json deleted file mode 100644 index f4f34b88fb17..000000000000 --- a/packages/kit/test/prerendering/ssr-false/package.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "prerendering-test-ssr-false", - "private": true, - "version": "0.0.2-next.0", - "scripts": { - "dev": "vite dev", - "build": "vite build", - "preview": "vite preview", - "check": "svelte-kit sync && tsc && svelte-check", - "test": "svelte-kit sync && pnpm build && uvu test" - }, - "devDependencies": { - "@sveltejs/kit": "workspace:^", - "svelte": "^3.55.1", - "svelte-check": "^3.0.2", - "typescript": "^4.9.4", - "uvu": "^0.5.6", - "vite": "^4.1.1" - }, - "type": "module" -} diff --git a/packages/kit/test/prerendering/ssr-false/src/app.d.ts b/packages/kit/test/prerendering/ssr-false/src/app.d.ts deleted file mode 100644 index 7b39ff31cd34..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/app.d.ts +++ /dev/null @@ -1 +0,0 @@ -/// ; diff --git a/packages/kit/test/prerendering/ssr-false/src/app.html b/packages/kit/test/prerendering/ssr-false/src/app.html deleted file mode 100644 index d35c108bb8f2..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/app.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - %sveltekit.head% - - - %sveltekit.body% - - diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/+layout.js b/packages/kit/test/prerendering/ssr-false/src/routes/+layout.js deleted file mode 100644 index a3d15781a772..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/+layout.js +++ /dev/null @@ -1 +0,0 @@ -export const ssr = false; diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/not-prerenderable/+page.server.js b/packages/kit/test/prerendering/ssr-false/src/routes/not-prerenderable/+page.server.js deleted file mode 100644 index 0e3c3113444f..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/not-prerenderable/+page.server.js +++ /dev/null @@ -1,3 +0,0 @@ -export const actions = { - default: () => {} -}; diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/not-prerenderable/+page.svelte b/packages/kit/test/prerendering/ssr-false/src/routes/not-prerenderable/+page.svelte deleted file mode 100644 index 3a4fb4b1663c..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/not-prerenderable/+page.svelte +++ /dev/null @@ -1 +0,0 @@ -

Not prerenderable because it has +page.server.js actions

diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/opt-out/+page.js b/packages/kit/test/prerendering/ssr-false/src/routes/opt-out/+page.js deleted file mode 100644 index d43d0cd2a55d..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/opt-out/+page.js +++ /dev/null @@ -1 +0,0 @@ -export const prerender = false; diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/opt-out/+page.svelte b/packages/kit/test/prerendering/ssr-false/src/routes/opt-out/+page.svelte deleted file mode 100644 index c2e1feb1d2eb..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/opt-out/+page.svelte +++ /dev/null @@ -1 +0,0 @@ -

prerenderable shell, but opted out of prerendering explicitly

diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-2/+page.server.js b/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-2/+page.server.js deleted file mode 100644 index 59b30f2adadf..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-2/+page.server.js +++ /dev/null @@ -1,3 +0,0 @@ -export function load() { - throw new Error('I should not be called during prerendering'); -} diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-2/+page.svelte b/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-2/+page.svelte deleted file mode 100644 index f844d12b1532..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-2/+page.svelte +++ /dev/null @@ -1 +0,0 @@ -

prerenderable shell

diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-3/+page.js b/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-3/+page.js deleted file mode 100644 index f1b9bcf9eee4..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-3/+page.js +++ /dev/null @@ -1,2 +0,0 @@ -export const prerender = 'auto'; -export const ssr = true; diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-3/+page.svelte b/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-3/+page.svelte deleted file mode 100644 index f844d12b1532..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable-3/+page.svelte +++ /dev/null @@ -1 +0,0 @@ -

prerenderable shell

diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable/+page.js b/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable/+page.js deleted file mode 100644 index 59b30f2adadf..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable/+page.js +++ /dev/null @@ -1,3 +0,0 @@ -export function load() { - throw new Error('I should not be called during prerendering'); -} diff --git a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable/+page.svelte b/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable/+page.svelte deleted file mode 100644 index f844d12b1532..000000000000 --- a/packages/kit/test/prerendering/ssr-false/src/routes/prerenderable/+page.svelte +++ /dev/null @@ -1 +0,0 @@ -

prerenderable shell

diff --git a/packages/kit/test/prerendering/ssr-false/svelte.config.js b/packages/kit/test/prerendering/ssr-false/svelte.config.js deleted file mode 100644 index 821c14379ec8..000000000000 --- a/packages/kit/test/prerendering/ssr-false/svelte.config.js +++ /dev/null @@ -1,6 +0,0 @@ -/** @type {import('@sveltejs/kit').Config} */ -const config = { - kit: {} -}; - -export default config; diff --git a/packages/kit/test/prerendering/ssr-false/test/test.js b/packages/kit/test/prerendering/ssr-false/test/test.js deleted file mode 100644 index 26004b3640dc..000000000000 --- a/packages/kit/test/prerendering/ssr-false/test/test.js +++ /dev/null @@ -1,39 +0,0 @@ -import * as fs from 'node:fs'; -import { fileURLToPath } from 'node:url'; -import { test } from 'uvu'; -import * as assert from 'uvu/assert'; - -const build = fileURLToPath(new URL('../.svelte-kit/output/prerendered/pages', import.meta.url)); - -/** @param {string} file */ -const read = (file, encoding = 'utf-8') => fs.readFileSync(`${build}/${file}`, encoding); - -test('prerenders /prerenderable shell', () => { - const content = read('prerenderable.html'); - assert.ok(!content.includes('prerenderable shell')); -}); - -test('prerenders /prerenderable-2 shell', () => { - const content = read('prerenderable-2.html'); - assert.ok(!content.includes('prerenderable shell')); -}); - -test('prerenders /prerenderable-3 shell with ssr=true and prerender=auto', () => { - const content = read('prerenderable-3.html'); - assert.ok(content.includes('prerenderable shell')); -}); - -test('does not prerender non prerenderable things', () => { - assert.equal(fs.readdirSync(build).length, 3); -}); - -test('keeps not-explicitly-prerendered routes in the manifest', () => { - const manifest = fileURLToPath( - new URL('../.svelte-kit/output/server/manifest.js', import.meta.url) - ); - const content = fs.readFileSync(manifest, 'utf-8'); - assert.ok(content.includes('/prerenderable')); - assert.ok(content.includes('/prerenderable-2')); -}); - -test.run(); diff --git a/packages/kit/test/prerendering/ssr-false/tsconfig.json b/packages/kit/test/prerendering/ssr-false/tsconfig.json deleted file mode 100644 index df547741d5bb..000000000000 --- a/packages/kit/test/prerendering/ssr-false/tsconfig.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "compilerOptions": { - "allowJs": true, - "checkJs": true, - "noEmit": true, - "module": "esnext", - "moduleResolution": "node", - "paths": { - "@sveltejs/kit": ["../../../types"], - "$lib": ["./src/lib"], - "$lib/*": ["./src/lib/*"], - "types": ["../../../types/internal"] - } - }, - "extends": "./.svelte-kit/tsconfig.json" -} diff --git a/packages/kit/test/prerendering/ssr-false/vite.config.js b/packages/kit/test/prerendering/ssr-false/vite.config.js deleted file mode 100644 index 574b809dd36f..000000000000 --- a/packages/kit/test/prerendering/ssr-false/vite.config.js +++ /dev/null @@ -1,27 +0,0 @@ -import * as path from 'node:path'; -import { sveltekit } from '@sveltejs/kit/vite'; - -/** @type {import('vite').UserConfig} */ -const config = { - build: { - minify: false - }, - - clearScreen: false, - - logLevel: 'silent', - - plugins: [sveltekit()], - - define: { - 'process.env.MY_ENV': '"MY_ENV DEFINED"' - }, - - server: { - fs: { - allow: [path.resolve('../../../src')] - } - } -}; - -export default config; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0578685eb8e5..35f6014deb2c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -640,22 +640,6 @@ importers: uvu: 0.5.6 vite: 4.1.1 - packages/kit/test/prerendering/ssr-false: - specifiers: - '@sveltejs/kit': workspace:^ - svelte: ^3.55.1 - svelte-check: ^3.0.2 - typescript: ^4.9.4 - uvu: ^0.5.6 - vite: ^4.1.1 - devDependencies: - '@sveltejs/kit': link:../../.. - svelte: 3.55.1 - svelte-check: 3.0.2_svelte@3.55.1 - typescript: 4.9.4 - uvu: 0.5.6 - vite: 4.1.1 - packages/migrate: specifiers: '@types/prompts': ^2.4.1