Skip to content

Commit

Permalink
Update patchRoutesOnMiss docs to reflect index matching
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Jul 2, 2024
1 parent 2f25789 commit f8883e1
Showing 1 changed file with 46 additions and 34 deletions.
80 changes: 46 additions & 34 deletions docs/routers/create-browser-router.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,24 +478,16 @@ let router = createBrowserRouter(
path: "/",
Component: Home,
},
{
id: "dashboard",
path: "/dashboard",
},
{
id: "account",
path: "/account",
},
],
{
async unstable_patchRoutesOnMiss({ path, patch }) {
if (path.startsWith("/dashboard")) {
let children = await import("./dashboard");
patch("dashboard", children);
patch(null, children);
}
if (path.startsWith("/account")) {
let children = await import("./account");
patch("account", children);
patch(null, children);
}
},
}
Expand All @@ -507,32 +499,52 @@ let router = createBrowserRouter(
If you don't wish to perform your own pseudo-matching, you can leverage the partial `matches` array and the `handle` field on a route to keep the children definitions co-located:

```jsx
let router = createBrowserRouter([
{
path: "/",
Component: Home,
},
{
path: "/dashboard",
handle: {
lazyChildren: () => import('./dashboard');
}
},
let router = createBrowserRouter(
[
{
path: "/",
Component: Home,
},
{
path: "/dashboard",
children: [
{
// If we want to include /dashboard in the critical routes, we need to
// also include it's index route since patchRoutesOnMiss will not be
// called on a navigation to `/dashboard` because it will have successfully
// matched the `/dashboard` parent route
index: true,
// ...
},
],
handle: {
lazyChildren: () => import("./dashboard"),
},
},
{
path: "/account",
children: [
{
index: true,
// ...
},
],
handle: {
lazyChildren: () => import("./account"),
},
},
],
{
path: "/account",
handle: {
lazyChildren: () => import('./account');
}
},
], {
async unstable_patchRoutesOnMiss({ matches, patch }) {
let leafRoute = matches[matches.length - 1]?.route;
if (leafRoute?.handle?.lazyChildren) {
let children = await leafRoute.handle.lazyChildren();
patch(leafRoute.id, children);
}
async unstable_patchRoutesOnMiss({ matches, patch }) {
let leafRoute = matches[matches.length - 1]?.route;
if (leafRoute?.handle?.lazyChildren) {
let children =
await leafRoute.handle.lazyChildren();
patch(leafRoute.id, children);
}
},
}
});
);
```
## `opts.window`
Expand Down

0 comments on commit f8883e1

Please sign in to comment.