Skip to content

Commit

Permalink
expose status to __layout, if already set (#4815)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jul 14, 2022
1 parent 1c5ec9b commit bc1bd76
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 19 deletions.
10 changes: 4 additions & 6 deletions packages/kit/src/runtime/server/page/load_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import { domain_matches, path_matches } from './cookie.js';
* node: import('types').SSRNode;
* $session: any;
* stuff: Record<string, any>;
* is_error: boolean;
* is_leaf: boolean;
* status?: number;
* error?: Error;
* status: number | null;
* error: Error | null;
* }} opts
* @returns {Promise<import('./types').Loaded>}
*/
Expand All @@ -32,7 +31,6 @@ export async function load_node({
node,
$session,
stuff,
is_error,
is_leaf,
status,
error
Expand Down Expand Up @@ -344,8 +342,8 @@ export async function load_node({
return proxy;
},
stuff: { ...stuff },
status: (is_error ? status : shadow.status) ?? null,
error: is_error ? error ?? null : null
status: shadow.status ?? status ?? null,
error: error ?? null
};

if (options.dev) {
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const updated = {
* state: import('types').SSRState;
* $session: any;
* page_config: { hydrate: boolean, router: boolean };
* status: number;
* status: number | null;
* error: Error | null;
* event: import('types').RequestEvent;
* resolve_opts: import('types').RequiredResolveOptions;
Expand Down Expand Up @@ -128,7 +128,7 @@ export async function render_response({
error,
params: event.params,
routeId: event.routeId,
status,
status: status || 200,
stuff,
url: state.prerendering ? new PrerenderingURL(event.url) : event.url
},
Expand Down Expand Up @@ -321,7 +321,7 @@ export async function render_response({
}

return new Response(html, {
status,
status: status || 200,
headers
});
}
Expand Down
22 changes: 15 additions & 7 deletions packages/kit/src/runtime/server/page/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function respond(opts) {
hydrate: true,
router: true
},
status: 200,
status: null,
error: null,
event,
stuff: {}
Expand Down Expand Up @@ -84,8 +84,8 @@ export async function respond(opts) {
/** @type {Array<Loaded>} */
let branch = [];

/** @type {number} */
let status = 200;
/** @type {number | null} */
let status = null;

/** @type {Error | null} */
let error = null;
Expand All @@ -105,10 +105,15 @@ export async function respond(opts) {
if (node) {
try {
loaded = await load_node({
...opts,
event,
options,
state,
route,
node,
$session,
stuff,
is_error: false,
status,
error,
is_leaf: i === nodes.length - 1
});

Expand Down Expand Up @@ -159,10 +164,13 @@ export async function respond(opts) {
try {
const error_loaded = /** @type {import('./types').Loaded} */ (
await load_node({
...opts,
event,
options,
state,
route,
$session,
node: error_node,
stuff: node_loaded.stuff,
is_error: true,
is_leaf: false,
status,
error
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/server/page/respond_with_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export async function respond_with_error({
node: default_layout,
$session,
stuff: {},
is_error: false,
status,
error,
is_leaf: false
})
);
Expand All @@ -64,7 +65,6 @@ export async function respond_with_error({
node: default_error,
$session,
stuff: layout_loaded ? layout_loaded.stuff : {},
is_error: true,
is_leaf: false,
status,
error
Expand Down
16 changes: 15 additions & 1 deletion packages/kit/test/apps/basics/src/routes/__layout.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script context="module">
/** @type {import('@sveltejs/kit').Load} */
export async function load({ fetch, url }) {
export async function load({ fetch, url, status }) {
if (url.pathname.startsWith('/errors/error-in-layout')) {
const res = await fetch('/errors/error-in-layout/non-existent');
Expand All @@ -9,8 +9,18 @@
};
}
if (url.pathname === '/errors/status-in-layout' && !status) {
console.log('returning 500');
return {
status: 500,
error: new Error('status is accessible in __layout')
};
}
return {
props: {
status,
foo: {
bar: 'Custom layout'
}
Expand Down Expand Up @@ -42,10 +52,14 @@
/** @type {{ bar: string }} */
export let foo;
/** @type {number} */
export let status;
</script>

<slot />

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

<style>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- this file exists only so that /errors/status-in-layout is a valid route -->
<h1>this should not be visible</h1>
7 changes: 7 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,13 @@ test.describe('Errors', () => {
expect(/** @type {Response} */ (response).status()).toBe(400);
}
});

test('status is accessible in __layout, if set', async ({ page }) => {
await page.goto('/errors/status-in-layout');
expect(await page.textContent('#message')).toBe(
'This is your custom error page saying: "status is accessible in __layout"'
);
});
});

test.describe('ETags', () => {
Expand Down

0 comments on commit bc1bd76

Please sign in to comment.