Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bc1bd76

Browse files
committedJul 14, 2022
expose status to __layout, if already set (#4815)
1 parent 1c5ec9b commit bc1bd76

File tree

7 files changed

+48
-19
lines changed

7 files changed

+48
-19
lines changed
 

‎packages/kit/src/runtime/server/page/load_node.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ import { domain_matches, path_matches } from './cookie.js';
1717
* node: import('types').SSRNode;
1818
* $session: any;
1919
* stuff: Record<string, any>;
20-
* is_error: boolean;
2120
* is_leaf: boolean;
22-
* status?: number;
23-
* error?: Error;
21+
* status: number | null;
22+
* error: Error | null;
2423
* }} opts
2524
* @returns {Promise<import('./types').Loaded>}
2625
*/
@@ -32,7 +31,6 @@ export async function load_node({
3231
node,
3332
$session,
3433
stuff,
35-
is_error,
3634
is_leaf,
3735
status,
3836
error
@@ -344,8 +342,8 @@ export async function load_node({
344342
return proxy;
345343
},
346344
stuff: { ...stuff },
347-
status: (is_error ? status : shadow.status) ?? null,
348-
error: is_error ? error ?? null : null
345+
status: shadow.status ?? status ?? null,
346+
error: error ?? null
349347
};
350348

351349
if (options.dev) {

‎packages/kit/src/runtime/server/page/render.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const updated = {
2323
* state: import('types').SSRState;
2424
* $session: any;
2525
* page_config: { hydrate: boolean, router: boolean };
26-
* status: number;
26+
* status: number | null;
2727
* error: Error | null;
2828
* event: import('types').RequestEvent;
2929
* resolve_opts: import('types').RequiredResolveOptions;
@@ -128,7 +128,7 @@ export async function render_response({
128128
error,
129129
params: event.params,
130130
routeId: event.routeId,
131-
status,
131+
status: status || 200,
132132
stuff,
133133
url: state.prerendering ? new PrerenderingURL(event.url) : event.url
134134
},
@@ -321,7 +321,7 @@ export async function render_response({
321321
}
322322

323323
return new Response(html, {
324-
status,
324+
status: status || 200,
325325
headers
326326
});
327327
}

‎packages/kit/src/runtime/server/page/respond.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function respond(opts) {
3636
hydrate: true,
3737
router: true
3838
},
39-
status: 200,
39+
status: null,
4040
error: null,
4141
event,
4242
stuff: {}
@@ -84,8 +84,8 @@ export async function respond(opts) {
8484
/** @type {Array<Loaded>} */
8585
let branch = [];
8686

87-
/** @type {number} */
88-
let status = 200;
87+
/** @type {number | null} */
88+
let status = null;
8989

9090
/** @type {Error | null} */
9191
let error = null;
@@ -105,10 +105,15 @@ export async function respond(opts) {
105105
if (node) {
106106
try {
107107
loaded = await load_node({
108-
...opts,
108+
event,
109+
options,
110+
state,
111+
route,
109112
node,
113+
$session,
110114
stuff,
111-
is_error: false,
115+
status,
116+
error,
112117
is_leaf: i === nodes.length - 1
113118
});
114119

@@ -159,10 +164,13 @@ export async function respond(opts) {
159164
try {
160165
const error_loaded = /** @type {import('./types').Loaded} */ (
161166
await load_node({
162-
...opts,
167+
event,
168+
options,
169+
state,
170+
route,
171+
$session,
163172
node: error_node,
164173
stuff: node_loaded.stuff,
165-
is_error: true,
166174
is_leaf: false,
167175
status,
168176
error

‎packages/kit/src/runtime/server/page/respond_with_error.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export async function respond_with_error({
4646
node: default_layout,
4747
$session,
4848
stuff: {},
49-
is_error: false,
49+
status,
50+
error,
5051
is_leaf: false
5152
})
5253
);
@@ -64,7 +65,6 @@ export async function respond_with_error({
6465
node: default_error,
6566
$session,
6667
stuff: layout_loaded ? layout_loaded.stuff : {},
67-
is_error: true,
6868
is_leaf: false,
6969
status,
7070
error

‎packages/kit/test/apps/basics/src/routes/__layout.svelte

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script context="module">
22
/** @type {import('@sveltejs/kit').Load} */
3-
export async function load({ fetch, url }) {
3+
export async function load({ fetch, url, status }) {
44
if (url.pathname.startsWith('/errors/error-in-layout')) {
55
const res = await fetch('/errors/error-in-layout/non-existent');
66
@@ -9,8 +9,18 @@
99
};
1010
}
1111
12+
if (url.pathname === '/errors/status-in-layout' && !status) {
13+
console.log('returning 500');
14+
15+
return {
16+
status: 500,
17+
error: new Error('status is accessible in __layout')
18+
};
19+
}
20+
1221
return {
1322
props: {
23+
status,
1424
foo: {
1525
bar: 'Custom layout'
1626
}
@@ -42,10 +52,14 @@
4252
4353
/** @type {{ bar: string }} */
4454
export let foo;
55+
56+
/** @type {number} */
57+
export let status;
4558
</script>
4659

4760
<slot />
4861

62+
<p id="status">layout status: {status}</p>
4963
<footer>{foo.bar}</footer>
5064

5165
<style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- this file exists only so that /errors/status-in-layout is a valid route -->
2+
<h1>this should not be visible</h1>

‎packages/kit/test/apps/basics/test/test.js

+7
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,13 @@ test.describe('Errors', () => {
13031303
expect(/** @type {Response} */ (response).status()).toBe(400);
13041304
}
13051305
});
1306+
1307+
test('status is accessible in __layout, if set', async ({ page }) => {
1308+
await page.goto('/errors/status-in-layout');
1309+
expect(await page.textContent('#message')).toBe(
1310+
'This is your custom error page saying: "status is accessible in __layout"'
1311+
);
1312+
});
13061313
});
13071314

13081315
test.describe('ETags', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.