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

Add status and error to page store #3096

Merged
merged 5 commits into from Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/app/stores.js
Expand Up @@ -56,7 +56,7 @@ export const navigating = {
};

/** @param {string} verb */
const error = (verb) => {
const throw_error = (verb) => {
throw new Error(
ssr
? `Can only ${verb} session store in browser`
Expand All @@ -76,6 +76,6 @@ export const session = {

return store.subscribe(fn);
},
set: () => error('set'),
update: () => error('update')
set: () => throw_error('set'),
update: () => throw_error('update')
};
14 changes: 8 additions & 6 deletions packages/kit/src/runtime/client/renderer.js
Expand Up @@ -180,7 +180,7 @@ export class Renderer {

result = error_args
? await this._load_error(error_args)
: await this._get_navigation_result_from_branch({ url, params, branch });
: await this._get_navigation_result_from_branch({ url, params, branch, status, error });
} catch (e) {
if (error) throw e;

Expand Down Expand Up @@ -417,9 +417,11 @@ export class Renderer {
* url: URL;
* params: Record<string, string>;
* branch: Array<import('./types').BranchNode | undefined>;
* status: number;
* error?: Error;
* }} opts
*/
async _get_navigation_result_from_branch({ url, params, branch }) {
async _get_navigation_result_from_branch({ url, params, branch, status, error }) {
const filtered = /** @type {import('./types').BranchNode[] } */ (branch.filter(Boolean));
const redirect = filtered.find((f) => f.loaded && f.loaded.redirect);

Expand All @@ -443,7 +445,7 @@ export class Renderer {
}

if (!this.current.url || url.href !== this.current.url.href) {
result.props.page = { url, params };
result.props.page = { url, params, status, error };

// TODO remove this for 1.0
/**
Expand Down Expand Up @@ -737,12 +739,12 @@ export class Renderer {
}
}

return await this._get_navigation_result_from_branch({ url, params, branch });
return await this._get_navigation_result_from_branch({ url, params, branch, status, error });
}

/**
* @param {{
* status?: number;
* status: number;
* error: Error;
* url: URL;
* }} opts
Expand Down Expand Up @@ -770,6 +772,6 @@ export class Renderer {
})
];

return await this._get_navigation_result_from_branch({ url, params, branch });
return await this._get_navigation_result_from_branch({ url, params, branch, status, error });
}
}
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/render.js
Expand Up @@ -68,7 +68,7 @@ export async function render_response({
navigating: writable(null),
session
},
page: { url, params },
page: { url, params, status, error },
components: branch.map(({ node }) => node.module.default)
};

Expand Down
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/src/routes/__error.svelte
Expand Up @@ -8,11 +8,17 @@
</script>

<script>
import { page } from '$app/stores';

/** @type {number} */
export let status;

/** @type {Error} */
export let error;

if ($page.error !== error || $page.status !== status) {
throw new Error('page store contains incorrect values');
}
</script>

<svelte:head>
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/types/ambient-modules.d.ts
Expand Up @@ -93,6 +93,8 @@ declare module '$app/stores' {
page: Readable<{
url: URL;
params: Record<string, string>;
status: number;
error: Error | null;
}>;
session: Writable<Session>;
};
Expand All @@ -103,6 +105,8 @@ declare module '$app/stores' {
export const page: Readable<{
url: URL;
params: Record<string, string>;
status: number;
error: Error | null;
}>;
/**
* A readable store.
Expand Down