Skip to content

Commit

Permalink
document how to render nested error pages for 404s (#4529)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 6, 2022
1 parent eec3370 commit 1330b85
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions documentation/docs/02-layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,43 @@ If an error component has a [`load`](/docs/loading) function, it will be called
> Layouts also have access to `error` and `status` via the [page store](/docs/modules#$app-stores)
>
> Server-side stack traces will be removed from `error` in production, to avoid exposing privileged information to users.
#### 404s

Nested error pages are only rendered when an error occurs while rendering a specific page. In the case of a request that doesn't match any existing route, SvelteKit will render a generic 404 instead. For example, given these routes...

```
src/routes/
├ __error.svelte
├ marx-brothers/
│ ├ __error.svelte
│ ├ chico.svelte
│ ├ harpo.svelte
│ └ groucho.svelte
```

...the `marx-brothers/__error.svelte` file will _not_ be rendered if you visit `/marx-brothers/karl`. If you want to render the nested error page, you should create a route that matches any `/marx-brothers/*` request, and return a 404 from it:

```diff
src/routes/
├ __error.svelte
├ marx-brothers/
│ ├ __error.svelte
+│ ├ [...path].svelte
│ ├ chico.svelte
│ ├ harpo.svelte
│ └ groucho.svelte
```

```svelte
/// file: src/routes/marx-brothers/[...path.svelte]
<script context="module">
/** @type {import('./[...path]').Load} */
export function load({ params }) {
return {
status: 404,
error: new Error(`Not found: /marx-brothers/${params.path}`)
};
}
</script>
```

0 comments on commit 1330b85

Please sign in to comment.