Skip to content

Commit 150c49c

Browse files
docs: fix wording, note SvelteKit 1.x behavior (#11301)
* docs: fix wording, note SvelteKit 1.x behavior * oops * tweaks * tweak --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 7718d0f commit 150c49c

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

documentation/docs/20-core-concepts/20-load.md

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export async function load({ params, parent }) {
374374

375375
## Errors
376376

377-
If an error is thrown during `load`, the nearest [`+error.svelte`](routing#error) will be rendered. For _expected_ errors, use the `error` helper from `@sveltejs/kit` to specify the HTTP status code and an optional message:
377+
If an error is thrown during `load`, the nearest [`+error.svelte`](routing#error) will be rendered. For [_expected_](/docs/errors#expected-errors) errors, use the `error` helper from `@sveltejs/kit` to specify the HTTP status code and an optional message:
378378

379379
```js
380380
/// file: src/routes/admin/+layout.server.js
@@ -404,11 +404,15 @@ export function load({ locals }) {
404404
}
405405
```
406406

407-
If an _unexpected_ error is thrown, SvelteKit will invoke [`handleError`](hooks#shared-hooks-handleerror) and treat it as a 500 Internal Error.
407+
Calling `error(...)` will throw an exception, making it easy to stop execution from inside helper functions.
408+
409+
If an [_unexpected_](/docs/errors#unexpected-errors) error is thrown, SvelteKit will invoke [`handleError`](hooks#shared-hooks-handleerror) and treat it as a 500 Internal Error.
410+
411+
> [In SvelteKit 1.x](migrating-to-sveltekit-2#redirect-and-error-are-no-longer-thrown-by-you) you had to `throw` the error yourself
408412
409413
## Redirects
410414

411-
To redirect users, use the `redirect` helper from `@sveltejs/kit` to specify the location to which they should be redirected alongside a `3xx` status code.
415+
To redirect users, use the `redirect` helper from `@sveltejs/kit` to specify the location to which they should be redirected alongside a `3xx` status code. Like `error(...)`, calling `redirect(...)` will throw an exception, making it easy to stop execution from inside helper functions.
412416

413417
```js
414418
/// file: src/routes/user/+layout.server.js
@@ -437,53 +441,56 @@ export function load({ locals }) {
437441
438442
In the browser, you can also navigate programmatically outside of a `load` function using [`goto`](modules#$app-navigation-goto) from [`$app.navigation`](modules#$app-navigation).
439443

444+
> [In SvelteKit 1.x](migrating-to-sveltekit-2#redirect-and-error-are-no-longer-thrown-by-you) you had to `throw` the `redirect` yourself
445+
440446
## Streaming with promises
441447

442-
Promises at the _top level_ of the returned object will be awaited, making it easy to return multiple promises without creating a waterfall. When using a server `load`, _nested_ promises will be streamed to the browser as they resolve. This is useful if you have slow, non-essential data, since you can start rendering the page before all the data is available:
448+
When using a server `load`, promises will be streamed to the browser as they resolve. This is useful if you have slow, non-essential data, since you can start rendering the page before all the data is available:
443449

444450
```js
445-
/// file: src/routes/+page.server.js
451+
/// file: src/routes/blog/[slug]/+page.server.js
452+
// @filename: ambient.d.ts
453+
declare global {
454+
const loadPost: (slug: string) => Promise<{ title: string, content: string }>;
455+
const loadComments: (slug: string) => Promise<{ content: string }>;
456+
}
457+
458+
export {};
459+
460+
// @filename: index.js
461+
// ---cut---
446462
/** @type {import('./$types').PageServerLoad} */
447-
export function load() {
463+
export async function load({ params }) {
448464
return {
449-
one: Promise.resolve(1),
450-
two: Promise.resolve(2),
451-
streamed: {
452-
three: new Promise((fulfil) => {
453-
setTimeout(() => {
454-
fulfil(3)
455-
}, 1000);
456-
})
457-
}
465+
// make sure the `await` happens at the end, otherwise we
466+
// can't start loading comments until we've loaded the post
467+
comments: loadComments(params.slug),
468+
post: await loadPost(params.slug)
458469
};
459470
}
460471
```
461472

462473
This is useful for creating skeleton loading states, for example:
463474

464475
```svelte
465-
<!--- file: src/routes/+page.svelte --->
476+
<!--- file: src/routes/blog/[slug]/+page.svelte --->
466477
<script>
467478
/** @type {import('./$types').PageData} */
468479
export let data;
469480
</script>
470481
471-
<p>
472-
one: {data.one}
473-
</p>
474-
<p>
475-
two: {data.two}
476-
</p>
477-
<p>
478-
three:
479-
{#await data.streamed.three}
480-
Loading...
481-
{:then value}
482-
{value}
483-
{:catch error}
484-
{error.message}
485-
{/await}
486-
</p>
482+
<h1>{data.post.title}</h1>
483+
<div>{@html data.post.content}</div>
484+
485+
{#await data.comments}
486+
Loading comments...
487+
{:then comments}
488+
{#each comments as comment}
489+
<p>{comment.content}</p>
490+
{/each}
491+
{:catch error}
492+
<p>error loading comments: {error.message}</p>
493+
{/await}
487494
```
488495

489496
When streaming data, be careful to handle promise rejections correctly. More specifically, the server could crash with an "unhandled promise rejection" error if a lazy-loaded promise fails before rendering starts (at which point it's caught) and isn't handling the error in some way. When using SvelteKit's `fetch` directly in the `load` function, SvelteKit will handle this case for you. For other promises, it is enough to attach a noop-`catch` to the promise to mark it as handled.
@@ -496,21 +503,21 @@ export function load({ fetch }) {
496503
ok_manual.catch(() => {});
497504

498505
return {
499-
streamed: {
500-
ok_manual,
501-
ok_fetch: fetch('/fetch/that/could/fail'),
502-
dangerous_unhandled: Promise.reject()
503-
}
506+
ok_manual,
507+
ok_fetch: fetch('/fetch/that/could/fail'),
508+
dangerous_unhandled: Promise.reject()
504509
};
505510
}
506511
```
507512

508513
> On platforms that do not support streaming, such as AWS Lambda, responses will be buffered. This means the page will only render once all promises resolve. If you are using a proxy (e.g. NGINX), make sure it does not buffer responses from the proxied server.
509514
510-
> Streaming data will only work when JavaScript is enabled. You should avoid returning nested promises from a universal `load` function if the page is server rendered, as these are _not_ streamed — instead, the promise is recreated when the function reruns in the browser.
515+
> Streaming data will only work when JavaScript is enabled. You should avoid returning promises from a universal `load` function if the page is server rendered, as these are _not_ streamed — instead, the promise is recreated when the function reruns in the browser.
511516
512517
> The headers and status code of a response cannot be changed once the response has started streaming, therefore you cannot `setHeaders` or throw redirects inside a streamed promise.
513518
519+
> [In SvelteKit 1.x](migrating-to-sveltekit-2#top-level-promises-are-no-longer-awaited) top-level promises were automatically awaited, only nested promises were streamed.
520+
514521
## Parallel loading
515522

516523
When rendering (or navigating to) a page, SvelteKit runs all `load` functions concurrently, avoiding a waterfall of requests. During client-side navigation, the result of calling multiple server `load` functions are grouped into a single response. Once all `load` functions have returned, the page is rendered.

documentation/docs/30-advanced/25-errors.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function load({ params }) {
4040
}
4141
```
4242

43-
This tells SvelteKit to set the response status code to 404 and render an [`+error.svelte`](routing#error) component, where `$page.error` is the object provided as the second argument to `error(...)`.
43+
This throws an exception that SvelteKit catches, causing it to set the response status code to 404 and render an [`+error.svelte`](routing#error) component, where `$page.error` is the object provided as the second argument to `error(...)`.
4444

4545
```svelte
4646
<!--- file: src/routes/+error.svelte --->
@@ -67,6 +67,8 @@ error(404, {
6767
+error(404, 'Not found');
6868
```
6969

70+
> [In SvelteKit 1.x](migrating-to-sveltekit-2#redirect-and-error-are-no-longer-thrown-by-you) you had to `throw` the `error` yourself
71+
7072
## Unexpected errors
7173

7274
An _unexpected_ error is any other exception that occurs while handling a request. Since these can contain sensitive information, unexpected error messages and stack traces are not exposed to users.

0 commit comments

Comments
 (0)