diff --git a/.changeset/moody-rabbits-complain.md b/.changeset/moody-rabbits-complain.md new file mode 100644 index 000000000000..fcd81deb4549 --- /dev/null +++ b/.changeset/moody-rabbits-complain.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Made LoadInput and LoadOutput types public, merged ErrorLoad and Load declarations diff --git a/documentation/docs/04-loading.md b/documentation/docs/04-loading.md index 57e2a75a8052..a65c36c6e998 100644 --- a/documentation/docs/04-loading.md +++ b/documentation/docs/04-loading.md @@ -50,7 +50,7 @@ It is recommended that you not store pre-request state in global variables, but ### Input -The `load` function receives an object containing six fields — `url`, `params`, `props`, `fetch`, `session` and `stuff`. The `load` function is reactive, and will re-run when its parameters change, but only if they are used in the function. Specifically, if `url`, `session` or `stuff` are used in the function, they will be re-run whenever their value changes, and likewise for the individual properties of `params`. +The `load` function receives an object containing eight fields — `url`, `params`, `props`, `fetch`, `session`, `stuff`, `status`, and `error`. The `load` function is reactive, and will re-run when its parameters change, but only if they are used in the function. Specifically, if `url`, `session` or `stuff` are used in the function, they will be re-run whenever their value changes, and likewise for the individual properties of `params`. > Note that destructuring parameters in the function declaration is enough to count as using them. @@ -93,6 +93,14 @@ If the page you're loading has an endpoint, the data returned from it is accessi `stuff` is passed from layouts to descendant layouts and pages, and can be filled with anything else you need to make available. For the root `__layout.svelte` component, it is equal to `{}`, but if that component's `load` function returns an object with a `stuff` property, it will be available to subsequent `load` functions. +#### status + +`status` is the HTTP status code when rendering an error page, or `null` otherwise. + +#### error + +`error` is the error that was thrown (or returned from a previous `load`) when rendering an error page, or `null` otherwise. + ### Output If you return a Promise from `load`, SvelteKit will delay rendering until the promise resolves. The return value has several properties, all optional: diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 9eb97962f8bd..97b3167635a4 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -497,7 +497,7 @@ export function create_client({ target, session, base, trailing_slash }) { const session = $session; if (module.load) { - /** @type {import('types').LoadInput | import('types').ErrorLoadInput} */ + /** @type {import('types').LoadInput} */ const load_input = { routeId, params: uses_params, @@ -520,7 +520,9 @@ export function create_client({ target, session, base, trailing_slash }) { node.uses.dependencies.add(href); return started ? fetch(resource, info) : initial_fetch(resource, info); - } + }, + status: status ?? null, + error: error ?? null }; if (import.meta.env.DEV) { @@ -532,11 +534,6 @@ export function create_client({ target, session, base, trailing_slash }) { }); } - if (error) { - /** @type {import('types').ErrorLoadInput} */ (load_input).status = status; - /** @type {import('types').ErrorLoadInput} */ (load_input).error = error; - } - const loaded = await module.load.call(null, load_input); if (!loaded) { diff --git a/packages/kit/src/runtime/server/page/load_node.js b/packages/kit/src/runtime/server/page/load_node.js index b67258acee3f..c72cf29abe89 100644 --- a/packages/kit/src/runtime/server/page/load_node.js +++ b/packages/kit/src/runtime/server/page/load_node.js @@ -74,7 +74,7 @@ export async function load_node({ redirect: shadow.redirect }; } else if (module.load) { - /** @type {import('types').LoadInput | import('types').ErrorLoadInput} */ + /** @type {import('types').LoadInput} */ const load_input = { url: state.prerender ? create_prerendering_url_proxy(event.url) : event.url, params: event.params, @@ -308,7 +308,9 @@ export async function load_node({ return proxy; }, - stuff: { ...stuff } + stuff: { ...stuff }, + status: is_error ? status ?? null : null, + error: is_error ? error ?? null : null }; if (options.dev) { @@ -320,11 +322,6 @@ export async function load_node({ }); } - if (is_error) { - /** @type {import('types').ErrorLoadInput} */ (load_input).status = status; - /** @type {import('types').ErrorLoadInput} */ (load_input).error = error; - } - loaded = await module.load.call(null, load_input); if (!loaded) { diff --git a/packages/kit/test/apps/basics/src/routes/errors/nested-error-page/__error.svelte b/packages/kit/test/apps/basics/src/routes/errors/nested-error-page/__error.svelte index e74d57cc7c61..cf9f99bd10c2 100644 --- a/packages/kit/test/apps/basics/src/routes/errors/nested-error-page/__error.svelte +++ b/packages/kit/test/apps/basics/src/routes/errors/nested-error-page/__error.svelte @@ -1,5 +1,5 @@