Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[breaking] prerender shells when ssr false and prerender not false #8131

Merged
merged 6 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-falcons-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] prerender shells when ssr false and prerender not false
9 changes: 8 additions & 1 deletion packages/kit/src/core/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,14 @@ export async function prerender() {
validate_common_exports(page.shared, route.id);
}

const prerender = get_option(nodes, 'prerender') ?? false;
const should_prerender = get_option(nodes, 'prerender');
const prerender =
// Try prerendering if ssr is false and no server needed.
// People can opt out of this behavior by explicitly setting prerender to false
(should_prerender !== false &&
get_option(nodes, 'ssr') === false &&
!page?.server?.actions) ||
(should_prerender ?? false);
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved

prerender_map.set(route.id, prerender);
}
Expand Down
27 changes: 26 additions & 1 deletion packages/kit/src/runtime/server/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,38 @@ export async function render_page(event, route, page, options, state, resolve_op
// 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') ?? false;
const should_prerender = get_option(nodes, 'prerender');

console.log('SHOULD', should_prerender, event.url.href);
if (should_prerender) {
const mod = leaf_node.server;
if (mod && mod.actions) {
throw new Error('Cannot prerender pages with actions');
}
} else if (state.prerendering) {
// Try to render the shell when ssr is false.
// 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
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
) {
return await render_response({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicated to code a little further below, but this way it's more readable (you don't need to hold all these conditions in your head until you arrive at the other condition)

branch: [],
fetched: [],
page_config: {
ssr: false,
csr: get_option(nodes, 'csr') ?? true
},
status,
error: null,
event,
options,
state,
resolve_opts
});
}

// if the page isn't marked as prerenderable, then bail out at this point
return new Response(undefined, {
status: 204
Expand Down
1 change: 1 addition & 0 deletions packages/kit/test/prerendering/ssr-false/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.env
21 changes: 21 additions & 0 deletions packages/kit/test/prerendering/ssr-false/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "prerendering-test-basics",
"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.54.0",
"svelte-check": "^2.9.2",
"typescript": "^4.9.3",
"uvu": "^0.5.6",
"vite": "^4.0.0"
},
"type": "module"
}
1 change: 1 addition & 0 deletions packages/kit/test/prerendering/ssr-false/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@sveltejs/kit" />;
11 changes: 11 additions & 0 deletions packages/kit/test/prerendering/ssr-false/src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
%sveltekit.head%
</head>
<body>
%sveltekit.body%
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ssr = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const actions = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Not prerenderable because it has +page.server.js actions</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const prerender = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>prerenderable shell, but opted out of prerendering explicitly</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function load() {
throw new Error('I should not be called during prerendering');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>prerenderable shell</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function load() {
return {
dynamic: Math.random()
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>prerenderable shell</p>
6 changes: 6 additions & 0 deletions packages/kit/test/prerendering/ssr-false/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {}
};

export default config;
25 changes: 25 additions & 0 deletions packages/kit/test/prerendering/ssr-false/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as fs from 'fs';
import { fileURLToPath } from '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('does not prerender non prerenderable things', () => {
assert.equal(fs.readdirSync(build).length, 2);
});

test.run();
16 changes: 16 additions & 0 deletions packages/kit/test/prerendering/ssr-false/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"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"
}
27 changes: 27 additions & 0 deletions packages/kit/test/prerendering/ssr-false/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as path from '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;
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.