Skip to content

Commit f2e924e

Browse files
authored
Add docs for unstable_dataStrategy and unstable_skipActionErrorRevalidation (#11356)
1 parent 6a2a3f9 commit f2e924e

File tree

2 files changed

+188
-11
lines changed

2 files changed

+188
-11
lines changed

docs/route/should-revalidate.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ This function allows you opt-out of revalidation for a route's [loader][loader]
3838

3939
There are several instances where data is revalidated, keeping your UI in sync with your data automatically:
4040

41-
- After an [`action`][action] is called from a [`<Form>`][form].
42-
- After an [`action`][action] is called from a [`<fetcher.Form>`][fetcher]
43-
- After an [`action`][action] is called from [`useSubmit`][usesubmit]
44-
- After an [`action`][action] is called from a [`fetcher.submit`][fetcher]
41+
- After an [`action`][action] is called via:
42+
- [`<Form>`][form], [`<fetcher.Form>`][fetcher], [`useSubmit`][usesubmit], or [`fetcher.submit`][fetcher]
43+
- When the `future.unstable_skipActionErrorRevalidation` flag is enabled, `loaders` will not revalidate by default if the `action` returns or throws a 4xx/5xx `Response`
44+
- You can opt-into revalidation for these scenarios via `shouldRevalidate` and the `unstable_actionStatus` parameter
4545
- When an explicit revalidation is triggered via [`useRevalidator`][userevalidator]
4646
- When the [URL params][params] change for an already rendered route
4747
- When the URL Search params change

docs/routers/create-browser-router.md

Lines changed: 184 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@ const router = createBrowserRouter(routes, {
116116

117117
The following future flags are currently available:
118118

119-
| Flag | Description |
120-
| ------------------------------------------- | --------------------------------------------------------------------- |
121-
| `v7_fetcherPersist` | Delay active fetcher cleanup until they return to an `idle` state |
122-
| `v7_normalizeFormMethod` | Normalize `useNavigation().formMethod` to be an uppercase HTTP Method |
123-
| `v7_partialHydration` | Support partial hydration for Server-rendered apps |
124-
| `v7_prependBasename` | Prepend the router basename to navigate/fetch paths |
125-
| [`v7_relativeSplatPath`][relativesplatpath] | Fix buggy relative path resolution in splat routes |
119+
| Flag | Description |
120+
| ------------------------------------------- | ----------------------------------------------------------------------- |
121+
| `v7_fetcherPersist` | Delay active fetcher cleanup until they return to an `idle` state |
122+
| `v7_normalizeFormMethod` | Normalize `useNavigation().formMethod` to be an uppercase HTTP Method |
123+
| `v7_partialHydration` | Support partial hydration for Server-rendered apps |
124+
| `v7_prependBasename` | Prepend the router basename to navigate/fetch paths |
125+
| [`v7_relativeSplatPath`][relativesplatpath] | Fix buggy relative path resolution in splat routes |
126+
| `unstable_skipActionErrorRevalidation` | Do not revalidate by default if the action returns a 4xx/5xx `Response` |
126127

127128
## `hydrationData`
128129

@@ -181,6 +182,181 @@ const router = createBrowserRouter(
181182
);
182183
```
183184

185+
## `unstable_dataStrategy`
186+
187+
<docs-warn>This is a low-level API intended for advanced use-cases. This overrides Remix's internal handling of `loader`/`action` execution, and if done incorrectly will break your app code. Please use with caution and perform the appropriate testing.</docs-warn>
188+
189+
<docs-warn>This API is marked "unstable" so it is subject to breaking API changes in minor releases</docs-warn>
190+
191+
By default, React Router is opinionated about how your data is loaded/submitted - and most notably, executes all of your loaders in parallel for optimal data fetching. While we think this is the right behavior for most use-cases, we realize that there is no "one size fits all" solution when it comes to data fetching for the wide landscape of application requirements.
192+
193+
The `unstable_dataStrategy` option gives you full control over how your loaders and actions are executed and lays the foundation to build in more advanced APIs such as middleware, context, and caching layers. Over time, we expect that we'll leverage this API internally to bring more first class APIs to React Router, but until then (and beyond), this is your way to add more advanced functionality for your applications data needs.
194+
195+
### Type Declaration
196+
197+
```ts
198+
interface DataStrategyFunction {
199+
(args: DataStrategyFunctionArgs): Promise<
200+
HandlerResult[]
201+
>;
202+
}
203+
204+
interface DataStrategyFunctionArgs<Context = any> {
205+
request: Request;
206+
params: Params;
207+
context?: Context;
208+
matches: DataStrategyMatch[];
209+
}
210+
211+
interface DataStrategyMatch
212+
extends AgnosticRouteMatch<
213+
string,
214+
AgnosticDataRouteObject
215+
> {
216+
shouldLoad: boolean;
217+
resolve: (
218+
handlerOverride?: (
219+
handler: (ctx?: unknown) => DataFunctionReturnValue
220+
) => Promise<HandlerResult>
221+
) => Promise<HandlerResult>;
222+
}
223+
224+
interface HandlerResult {
225+
type: "data" | "error";
226+
result: any; // data, Error, Response, DeferredData
227+
status?: number;
228+
}
229+
```
230+
231+
`unstable_dataStrategy` receives the same arguments as a `loader`/`action` (`request`, `params`) but it also receives a `matches` array which is an array of the matched routes where each match is extended with 2 new fields for use in the data strategy function:
232+
233+
- **`match.resolve`** - An async function that will resolve any `route.lazy` implementations and execute the route's handler (if necessary), returning a `HandlerResult`
234+
- You should call `match.resolve` for _all_ matches every time to ensure that all lazy routes are properly resolved
235+
- This does not mean you're calling the loader/action (the "handler") - resolve will only call the handler internally if needed and if you don't pass your own `handlerOverride` function parameter
236+
- See the examples below for how to implement custom handler execution via `match.resolve`
237+
- **`match.shouldLoad`** - A boolean value indicating whether this route handler needs to be called in this pass
238+
- This array always includes _all_ matched routes even when only _some_ route handlers need to be called so that things like middleware can be implemented
239+
- This is usually only needed if you are skipping the route handler entirely and implementing custom handler logic - since it lets you determine if that custom logic should run for this route or not
240+
- For example:
241+
- If you are on `/parent/child/a` and you navigate to `/parent/child/b` - you'll get an array of three matches (`[parent, child, b]`), but only `b` will have `shouldLoad=true` because the data for `parent` and `child` is already loaded
242+
- If you are on `/parent/child/a` and you submit to `a`'s `action`, then only `a` will have `shouldLoad=true` for the action execution of `dataStrategy`
243+
- After the `action`, `dataStrategy` will be called again for the `loader` revalidation, and all matches will have `shouldLoad=true` (assuming no custom `shouldRevalidate` implementations)
244+
245+
The `dataStrategy` function should return a parallel array of `HandlerResult` instances, which is just an object of `{ type: 'data', result: unknown }` or `{ type: 'error', result: unknown }` depending on if the handler was successful or not. If the returned `handlerResult.result` is a `Response`, React Router will unwrap it for you (via `res.json` or `res.text`). If you need to do custom decoding of a `Response` but preserve the status code, you can return the decoded value in `handlerResult.result` and send the status along via `handlerResult.status` (for example, when using the `future.unstable_skipActionRevalidation` flag). `match.resolve()` will return a `HandlerResult` if you are not passing it a handler override function. If you are, then you need to wrap the `handler` result in a `HandlerResult` (see examples below).
246+
247+
### Example Use Cases
248+
249+
#### Adding logging
250+
251+
In the simplest case, let's look at hooking into this API to add some logging for when our route loaders/actions execute:
252+
253+
```ts
254+
let router = createBrowserRouter(routes, {
255+
unstable_dataStrategy({ request, matches }) {
256+
return Promise.all(
257+
matches.map(async (match) => {
258+
console.log(`Processing route ${match.route.id}`);
259+
// Don't override anything - just resolve route.lazy + call loader
260+
let result = await match.resolve();
261+
console.log(`Done processing route ${match.route.id}`);
262+
return result.
263+
})
264+
)
265+
},
266+
});
267+
```
268+
269+
#### Middleware
270+
271+
Let's define a middleware on each route via `handle` and call middleware sequentially first, then call all loaders in parallel - providing any data made available via the middleware:
272+
273+
```ts
274+
const routes [
275+
{
276+
id: "parent",
277+
path: "/parent",
278+
loader({ request }, context) { /*...*/ },
279+
handle: {
280+
async middleware({ request }, context) {
281+
context.parent = "PARENT MIDDLEWARE";
282+
},
283+
},
284+
children: [
285+
{
286+
id: "child",
287+
path: "child",
288+
loader({ request }, context) { /*...*/ },
289+
handle: {
290+
async middleware({ request }, context) {
291+
context.child = "CHILD MIDDLEWARE";
292+
},
293+
},
294+
},
295+
],
296+
},
297+
];
298+
299+
let router = createBrowserRouter(routes, {
300+
unstable_dataStrategy({ request, params, matches }) {
301+
// Run middleware sequentially and let them add data to `context`
302+
let context = {};
303+
for (match of matches) {
304+
if (match.route.handle?.middleware) {
305+
await match.route.handle.middleware({ request, params }, context);
306+
}
307+
});
308+
309+
// Run loaders in parallel with the `context` value
310+
return Promise.all(
311+
matches.map((match, i) =>
312+
match.resolve(async (handler) => {
313+
let result = await handler(context);
314+
return { type: "data", result };
315+
})
316+
)
317+
);
318+
},
319+
});
320+
```
321+
322+
#### Custom Handler
323+
324+
It's also possible you don't even want to define a loader implementation at the route level. Maybe you want to just determine the routes and issue a single GraphQL request for all of your data? You can do that by setting your `route.loader=true` so it qualifies as "having a loader", and then store GQL fragments on `route.handle`:
325+
326+
```ts
327+
const routes [
328+
{
329+
id: "parent",
330+
path: "/parent",
331+
loader: true,
332+
handle: {
333+
gql: gql`fragment Parent on Whatever { parentField }`
334+
},
335+
children: [
336+
{
337+
id: "child",
338+
path: "child",
339+
loader: true,
340+
handle: {
341+
gql: gql`fragment Child on Whatever { childField }`
342+
},
343+
},
344+
],
345+
},
346+
];
347+
348+
let router = createBrowserRouter(routes, {
349+
unstable_dataStrategy({ request, params, matches }) {
350+
// Compose route fragments into a single GQL payload
351+
let gql = getFragmentsFromRouteHandles(matches);
352+
let data = await fetchGql(gql);
353+
// Parse results back out into individual route level HandlerResult's
354+
let results = parseResultsFromGql(data);
355+
return results;
356+
},
357+
});
358+
```
359+
184360
## `window`
185361

186362
Useful for environments like browser devtool plugins or testing to use a different window than the global `window`.
@@ -199,3 +375,4 @@ Useful for environments like browser devtool plugins or testing to use a differe
199375
[clientloader]: https://remix.run/route/client-loader
200376
[hydratefallback]: ../route/hydrate-fallback-element
201377
[relativesplatpath]: ../hooks/use-resolved-path#splat-paths
378+
[currying]: https://stackoverflow.com/questions/36314/what-is-currying

0 commit comments

Comments
 (0)