Skip to content

Commit b0ba3e4

Browse files
committed
Update docs and typegen
1 parent 32aa8ae commit b0ba3e4

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

docs/how-to/middleware.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,32 +276,34 @@ const route = {
276276
};
277277
```
278278

279-
However, there may be some cases where you want to do some post-processing based on the result of the loaders/action. In lieu of a `Response`, client middleware bubbles up a `Record<string, DataStrategy>` object because it is implemented as part of the default [`dataStrategy`] internally. This allows you to take conditional action in your middleware based on the outcome of the executed `loader`/`action` functions.
279+
There may be _some_ cases where you want to do some post-processing based on the result of the loaders/action. In lieu of a `Response`, client middleware bubbles up the value returned from the active [`dataStrategy`](../api/data-routers/createBrowserRouter#optsdatastrategy) (`Record<string, DataStrategyResult>` - keyed by route id). This allows you to take conditional action in your middleware based on the outcome of the executed `loader`/`action` functions.
280280

281281
Here's an example of the [CMS Redirect on 404][cms-redirect] use case implemented as a client side middleware:
282282

283283
```tsx
284284
async function cmsFallbackMiddleware({ request }, next) {
285285
const results = await next();
286286

287-
// Check if we got a 404 from any of our routes
288-
let is404 =
289-
isRouteErrorResponse(r.result) &&
290-
r.result.status === 404;
291-
if (Object.values(results).some((r) => is404(r))) {
292-
// Check CMS for a redirect
287+
// Check if we got a 404 from any of our routes and if so, look for a
288+
// redirect in our CMS
289+
const found404 = Object.values(results).some(
290+
(r) =>
291+
isRouteErrorResponse(r.result) &&
292+
r.result.status === 404,
293+
);
294+
if (found404) {
293295
const cmsRedirect = await checkCMSRedirects(
294296
request.url,
295297
);
296298
if (cmsRedirect) {
297299
throw redirect(cmsRedirect, 302);
298300
}
299301
}
300-
301-
return results;
302302
}
303303
```
304304

305+
<docs-warning>In a server middleware, you shouldn't be messing with the `Response` body and should only be reading status/headers and setting headers. Similarly, this value should be considered read-only in client middleware because it represents the "body" or "data" for the resulting navigation which should be driven by loaders/actions - not middleware. This also means that in client middleware, there's usually no need to return the results even if you needed to capture it from `await next()`;</docs-warning>
306+
305307
### When Middleware Runs
306308

307309
It is very important to understand _when_ your middlewares will run to make sure your application is behaving as you intend.

packages/react-router/lib/types/route-module-annotations.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import type { MetaDescriptor } from "../dom/ssr/routeModules";
22
import type { Location } from "../router/history";
33
import type { LinkDescriptor } from "../router/links";
4-
import type { unstable_MiddlewareNextFunction } from "../router/utils";
4+
import type {
5+
DataStrategyResult,
6+
unstable_MiddlewareNextFunction,
7+
} from "../router/utils";
58

69
import type {
710
ClientDataFunctionArgs,
@@ -82,8 +85,8 @@ type CreateServerMiddlewareFunction<T extends RouteInfo> = (
8285

8386
type CreateClientMiddlewareFunction<T extends RouteInfo> = (
8487
args: ClientDataFunctionArgs<T["params"]>,
85-
next: unstable_MiddlewareNextFunction<undefined>,
86-
) => MaybePromise<void>;
88+
next: unstable_MiddlewareNextFunction<Record<string, DataStrategyResult>>,
89+
) => MaybePromise<Record<string, DataStrategyResult> | void>;
8790

8891
type CreateServerLoaderArgs<T extends RouteInfo> = ServerDataFunctionArgs<
8992
T["params"]

0 commit comments

Comments
 (0)