Skip to content

Commit

Permalink
Stabilize unstable_patchRoutesOnNavigation (#11973)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Sep 10, 2024
1 parent d573eff commit 25e4981
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 75 deletions.
7 changes: 7 additions & 0 deletions .changeset/happy-grapes-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"react-router-dom": minor
"react-router": minor
"@remix-run/router": minor
---

Stabilize `unstable_patchRoutesOnNavigation`
18 changes: 9 additions & 9 deletions docs/routers/create-browser-router.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function createBrowserRouter(
future?: FutureConfig;
hydrationData?: HydrationState;
unstable_dataStrategy?: unstable_DataStrategyFunction;
unstable_patchRoutesOnNavigation?: unstable_PatchRoutesOnNavigationFunction;
patchRoutesOnNavigation?: PatchRoutesOnNavigationFunction;
window?: Window;
}
): RemixRouter;
Expand Down Expand Up @@ -438,7 +438,7 @@ let router = createBrowserRouter(routes, {
});
```

## `opts.unstable_patchRoutesOnNavigation`
## `opts.patchRoutesOnNavigation`

<docs-warning>This API is marked "unstable" so it is subject to breaking API changes in minor releases</docs-warning>

Expand All @@ -448,12 +448,12 @@ To combat this, we introduced [`route.lazy`][route-lazy] in [v6.9.0][6-9-0] whic

In some cases, even this doesn't go far enough. For very large applications, providing all route definitions up front can be prohibitively expensive. Additionally, it might not even be possible to provide all route definitions up front in certain Micro-Frontend or Module-Federation architectures.

This is where `unstable_patchRoutesOnNavigation` comes in ([RFC][fog-of-war-rfc]). This API is for advanced use-cases where you are unable to provide the full route tree up-front and need a way to lazily "discover" portions of the route tree at runtime. This feature is often referred to as ["Fog of War"][fog-of-war] because similar to how video games expand the "world" as you move around - the router would be expanding its routing tree as the user navigated around the app - but would only ever end up loading portions of the tree that the user visited.
This is where `patchRoutesOnNavigation` comes in ([RFC][fog-of-war-rfc]). This API is for advanced use-cases where you are unable to provide the full route tree up-front and need a way to lazily "discover" portions of the route tree at runtime. This feature is often referred to as ["Fog of War"][fog-of-war] because similar to how video games expand the "world" as you move around - the router would be expanding its routing tree as the user navigated around the app - but would only ever end up loading portions of the tree that the user visited.

### Type Declaration

```ts
export interface unstable_PatchRoutesOnNavigationFunction {
export interface PatchRoutesOnNavigationFunction {
(opts: {
path: string;
matches: RouteMatch[];
Expand All @@ -467,7 +467,7 @@ export interface unstable_PatchRoutesOnNavigationFunction {

### Overview

`unstable_patchRoutesOnNavigation` will be called anytime React Router is unable to match a `path`. The arguments include the `path`, any partial `matches`, and a `patch` function you can call to patch new routes into the tree at a specific location. This method is executed during the `loading` portion of the navigation for `GET` requests and during the `submitting` portion of the navigation for non-`GET` requests.
`patchRoutesOnNavigation` will be called anytime React Router is unable to match a `path`. The arguments include the `path`, any partial `matches`, and a `patch` function you can call to patch new routes into the tree at a specific location. This method is executed during the `loading` portion of the navigation for `GET` requests and during the `submitting` portion of the navigation for non-`GET` requests.

**Patching children into an existing route**

Expand All @@ -481,7 +481,7 @@ const router = createBrowserRouter(
},
],
{
async unstable_patchRoutesOnNavigation({
async patchRoutesOnNavigation({
path,
patch,
}) {
Expand Down Expand Up @@ -512,7 +512,7 @@ const router = createBrowserRouter(
},
],
{
async unstable_patchRoutesOnNavigation({
async patchRoutesOnNavigation({
path,
patch,
}) {
Expand Down Expand Up @@ -540,7 +540,7 @@ let router = createBrowserRouter(
},
],
{
async unstable_patchRoutesOnNavigation({
async patchRoutesOnNavigation({
path,
patch,
}) {
Expand Down Expand Up @@ -598,7 +598,7 @@ let router = createBrowserRouter(
},
],
{
async unstable_patchRoutesOnNavigation({
async patchRoutesOnNavigation({
matches,
patch,
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe("v7_partialHydration", () => {
future: {
v7_partialHydration: true,
},
unstable_patchRoutesOnNavigation({ path, patch }) {
patchRoutesOnNavigation({ path, patch }) {
if (path === "/parent/child") {
patch("parent", [
{
Expand Down Expand Up @@ -157,7 +157,7 @@ describe("v7_partialHydration", () => {
future: {
v7_partialHydration: true,
},
unstable_patchRoutesOnNavigation({ path, patch }) {
patchRoutesOnNavigation({ path, patch }) {
if (path === "/parent/child") {
patch("parent", [
{
Expand Down
10 changes: 5 additions & 5 deletions packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
RouterProviderProps,
To,
unstable_DataStrategyFunction,
unstable_PatchRoutesOnNavigationFunction,
PatchRoutesOnNavigationFunction,
} from "react-router";
import {
Router,
Expand Down Expand Up @@ -151,7 +151,7 @@ export type {
ShouldRevalidateFunctionArgs,
To,
UIMatch,
unstable_PatchRoutesOnNavigationFunction,
PatchRoutesOnNavigationFunction,
} from "react-router";
export {
AbortedDeferredError,
Expand Down Expand Up @@ -259,7 +259,7 @@ interface DOMRouterOpts {
future?: Partial<Omit<RouterFutureConfig, "v7_prependBasename">>;
hydrationData?: HydrationState;
unstable_dataStrategy?: unstable_DataStrategyFunction;
unstable_patchRoutesOnNavigation?: unstable_PatchRoutesOnNavigationFunction;
patchRoutesOnNavigation?: PatchRoutesOnNavigationFunction;
window?: Window;
}

Expand All @@ -278,7 +278,7 @@ export function createBrowserRouter(
routes,
mapRouteProperties,
unstable_dataStrategy: opts?.unstable_dataStrategy,
unstable_patchRoutesOnNavigation: opts?.unstable_patchRoutesOnNavigation,
patchRoutesOnNavigation: opts?.patchRoutesOnNavigation,
window: opts?.window,
}).initialize();
}
Expand All @@ -298,7 +298,7 @@ export function createHashRouter(
routes,
mapRouteProperties,
unstable_dataStrategy: opts?.unstable_dataStrategy,
unstable_patchRoutesOnNavigation: opts?.unstable_patchRoutesOnNavigation,
patchRoutesOnNavigation: opts?.patchRoutesOnNavigation,
window: opts?.window,
}).initialize();
}
Expand Down
10 changes: 5 additions & 5 deletions packages/react-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
ShouldRevalidateFunctionArgs,
To,
UIMatch,
unstable_AgnosticPatchRoutesOnNavigationFunction,
AgnosticPatchRoutesOnNavigationFunction,
} from "@remix-run/router";
import {
AbortedDeferredError,
Expand Down Expand Up @@ -291,8 +291,8 @@ function mapRouteProperties(route: RouteObject) {
return updates;
}

export interface unstable_PatchRoutesOnNavigationFunction
extends unstable_AgnosticPatchRoutesOnNavigationFunction<RouteMatch> {}
export interface PatchRoutesOnNavigationFunction
extends AgnosticPatchRoutesOnNavigationFunction<RouteMatch> {}

export function createMemoryRouter(
routes: RouteObject[],
Expand All @@ -303,7 +303,7 @@ export function createMemoryRouter(
initialEntries?: InitialEntry[];
initialIndex?: number;
unstable_dataStrategy?: unstable_DataStrategyFunction;
unstable_patchRoutesOnNavigation?: unstable_PatchRoutesOnNavigationFunction;
patchRoutesOnNavigation?: PatchRoutesOnNavigationFunction;
}
): RemixRouter {
return createRouter({
Expand All @@ -320,7 +320,7 @@ export function createMemoryRouter(
routes,
mapRouteProperties,
unstable_dataStrategy: opts?.unstable_dataStrategy,
unstable_patchRoutesOnNavigation: opts?.unstable_patchRoutesOnNavigation,
patchRoutesOnNavigation: opts?.patchRoutesOnNavigation,
}).initialize();
}

Expand Down
Loading

0 comments on commit 25e4981

Please sign in to comment.