Skip to content

Commit

Permalink
respect ssr: false when rendering 404 page - fixes #4406
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 4, 2022
1 parent 89001e7 commit c47d088
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-falcons-raise.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Respect ssr option when rendering 404 page
72 changes: 40 additions & 32 deletions packages/kit/src/runtime/server/page/respond_with_error.js
Expand Up @@ -29,38 +29,46 @@ export async function respond_with_error({
resolve_opts
}) {
try {
const default_layout = await options.manifest._.nodes[0](); // 0 is always the root layout
const default_error = await options.manifest._.nodes[1](); // 1 is always the root error
const branch = [];
let stuff = {};

const layout_loaded = /** @type {Loaded} */ (
await load_node({
event,
options,
state,
route: null,
node: default_layout,
$session,
stuff: {},
is_error: false,
is_leaf: false
})
);
if (resolve_opts.ssr) {
const default_layout = await options.manifest._.nodes[0](); // 0 is always the root layout
const default_error = await options.manifest._.nodes[1](); // 1 is always the root error

const error_loaded = /** @type {Loaded} */ (
await load_node({
event,
options,
state,
route: null,
node: default_error,
$session,
stuff: layout_loaded ? layout_loaded.stuff : {},
is_error: true,
is_leaf: false,
status,
error
})
);
const layout_loaded = /** @type {Loaded} */ (
await load_node({
event,
options,
state,
route: null,
node: default_layout,
$session,
stuff: {},
is_error: false,
is_leaf: false
})
);

const error_loaded = /** @type {Loaded} */ (
await load_node({
event,
options,
state,
route: null,
node: default_error,
$session,
stuff: layout_loaded ? layout_loaded.stuff : {},
is_error: true,
is_leaf: false,
status,
error
})
);

branch.push(layout_loaded, error_loaded);
stuff = error_loaded.stuff;
}

return await render_response({
options,
Expand All @@ -70,10 +78,10 @@ export async function respond_with_error({
hydrate: options.hydrate,
router: options.router
},
stuff: error_loaded.stuff,
stuff,
status,
error,
branch: [layout_loaded, error_loaded],
branch,
event,
resolve_opts
});
Expand Down
7 changes: 6 additions & 1 deletion packages/kit/test/apps/basics/src/routes/__error.svelte
@@ -1,6 +1,11 @@
<script context="module">
/** @type {import('@sveltejs/kit').ErrorLoad} */
export function load({ status, error }) {
export function load({ status, error, url }) {
if (url.pathname === '/no-ssr/missing') {
// load functions should not be called on the server if `ssr: false`
throw new Error('load function was called erroneously');
}
return {
props: { status, error }
};
Expand Down
5 changes: 5 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Expand Up @@ -1525,6 +1525,11 @@ test.describe.parallel('Page options', () => {
).toBe('absolute');
}
});

test('does not SSR error page for 404s with ssr=false', async ({ request }) => {
const html = await request.get('/no-ssr/missing');
expect(await html.text()).not.toContain('load function was called erroneously');
});
});

test.describe.parallel('$app/paths', () => {
Expand Down

0 comments on commit c47d088

Please sign in to comment.