Skip to content

Commit

Permalink
404 handling documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mateothegreat committed Dec 21, 2024
1 parent 7df78d8 commit 9039f89
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
28 changes: 28 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ npm install @mateothegreat/svelte5-router
- [Using Components \& Snippets](#using-components--snippets)
- [Accessing Parameters](#accessing-parameters)
- [Passing Props](#passing-props)
- [Not Found](#not-found)
- [Hooks](#hooks)
- [States](#states)
- [Navigation State](#navigation-state)
Expand Down Expand Up @@ -195,6 +196,33 @@ Then, in your component, you can access the prop like this:
<pre>{JSON.stringify(myProps, null, 2)}</pre>
```

### Not Found

To handle 404 errors, you can use the `.*` or `.+` pattern such as:

```ts
const routes: Route[] = [
// This will match the root path ("/") and render the Homepage component:
{
path: "^/$",
component: Homepage
},
{
path: "profile",
component: Profile
},
// This will match any path that is not found:
{
path: ".+",
component: NotFound
}
];
```

Notice the `^/$` pattern. This matches the root path ("/")indicating that this is the first "default" route.

Now, after this route, we have a catch-all route that matches any path at last indicating the route is not found.

## Hooks

Use `pre` and `post` hooks to run before and after a route is rendered to do things like authentication, logging, etc.
Expand Down
8 changes: 7 additions & 1 deletion test/app/src/app.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Github, Home } from "lucide-svelte";
import Default from "./lib/default.svelte";
import Delayed from "./lib/delayed.svelte";
import NotFound from "./lib/not-found.svelte";
import Params from "./lib/params/params.svelte";
import Props from "./lib/props/props.svelte";
import BankAccount from "./lib/protected/bank-account.svelte";
Expand All @@ -13,7 +14,7 @@
const routes: Route[] = [
{
path: "",
path: "^/$",
component: Default
},
{
Expand Down Expand Up @@ -102,6 +103,10 @@
{
path: "query-redirect",
component: QueryRedirect
},
{
path: ".+",
component: NotFound
}
];
Expand Down Expand Up @@ -160,6 +165,7 @@
Call the <span class="rounded bg-black px-2 py-0.5 text-green-500">goto("/a");</span> method
</button>
<a use:route href="/query-redirect" class="py-1hover:bg-pink-800 rounded bg-blue-600 px-3 py-1">/query-redirect</a>
<a use:route href="/not-found" class="py-1hover:bg-pink-800 rounded bg-slate-600 px-3 py-1">/not-found</a>
</div>
<div class=" w-full flex-1 bg-zinc-900 p-6">
<div class="flex flex-col gap-4 rounded-lg bg-zinc-950 p-4 shadow-xl">
Expand Down
1 change: 1 addition & 0 deletions test/app/src/lib/not-found.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
404 not found :(

0 comments on commit 9039f89

Please sign in to comment.