You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/how-to/middleware.md
+37-21Lines changed: 37 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -133,17 +133,9 @@ function getLoadContext(req, res) {
133
133
134
134
## Quick Start (Data Mode)
135
135
136
-
### 1. Enable the middleware flag
136
+
<docs-info>Note there is no future flag in Data Mode because you can opt-into middleware by adding it to your routes, no breaking changes exist that require a future flag.</docs-info>
137
137
138
-
```tsx
139
-
const router =createBrowserRouter(routes, {
140
-
future: {
141
-
unstable_middleware: true,
142
-
},
143
-
});
144
-
```
145
-
146
-
### 2. Create a context
138
+
### 1. Create a context
147
139
148
140
Middleware uses a `context` provider instance to provide data down the middleware chain.
149
141
You can create type-safe context objects using [`unstable_createContext`][createContext]:
@@ -156,7 +148,7 @@ export const userContext =
156
148
unstable_createContext<User|null>(null);
157
149
```
158
150
159
-
### 3. Add middleware to your routes
151
+
### 2. Add middleware to your routes
160
152
161
153
```tsx
162
154
import { redirect } from"react-router";
@@ -216,17 +208,14 @@ export default function Profile() {
216
208
}
217
209
```
218
210
219
-
### 4. Add an `unstable_getContext` function (optional)
211
+
### 3. Add an `unstable_getContext` function (optional)
220
212
221
213
If you wish to include a base context on all navigations/fetches, you can add an [`unstable_getContext`][getContext] function to your router. This will be called to populate a fresh context on every navigation/fetch.
Client middleware runs in the browser in framework and data mode for client-side navigations and fetcher calls. Client middleware is different because there's no HTTP [`Request`][request], so it doesn't bubble up anything via the `next`function:
248
+
Client middleware runs in the browser in framework and data mode for client-side navigations and fetcher calls. Client middleware differs from server middleware because there's no HTTP Request, so it doesn't have a `Response` to bubble up. In most cases, you can just ignore the return value from `next`and return nothing from your middleware on the client:
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`][datastrategy] (`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.
271
+
272
+
Here's an example of the [CMS Redirect on 404][cms-redirect] use case implemented as a client side middleware:
// Check if we got a 404 from any of our routes and if so, look for a
279
+
// redirect in our CMS
280
+
const found404 =Object.values(results).some(
281
+
(r) =>
282
+
isRouteErrorResponse(r.result) &&
283
+
r.result.status===404,
284
+
);
285
+
if (found404) {
286
+
const cmsRedirect =awaitcheckCMSRedirects(
287
+
request.url,
288
+
);
289
+
if (cmsRedirect) {
290
+
throwredirect(cmsRedirect, 302);
291
+
}
292
+
}
293
+
}
294
+
```
295
+
296
+
<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>
297
+
282
298
### When Middleware Runs
283
299
284
300
It is very important to understand _when_ your middlewares will run to make sure your application is behaving as you intend.
0 commit comments