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: .changeset/middleware.md
+44-17Lines changed: 44 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -4,17 +4,18 @@
4
4
5
5
Support `middleware` on routes (unstable)
6
6
7
-
Routes can now define a `middleware` property accepting an array of functions that will run sequentially before route loader run in parallel. These functions accept the same arguments as `loader`/`action`and an additional `next` function to run the remaining data pipeline. This allows middlewares to perform logic before and after loaders/actions execute.
7
+
Routes can now define an array of middleware functions that will run sequentially before route handlers run. These functions accept the same arguments as `loader`/`action`plus an additional `next` function to run the remaining data pipeline. This allows middlewares to perform logic before and after handlers execute.
console.log(`Navigated to ${request.url} (${duration}ms)`);
41
+
}
42
+
```
43
+
44
+
Note that in the above example, the `next`/`middleware` functions don't return anything. This is by design as on the client there is no "response" to send over the network like there would be for middlewares running on the server. The data is all handled behind the scenes by the stateful `router`.
45
+
46
+
For a server-side middleware, the `next` function will return the HTTP `Response` that React Router will be sending across the wire, thus giving you a chance to make changes as needed. You may throw a new response to short circuit and respond immediately, or you may return a new or altered response to override the default returned by `next()`.
47
+
48
+
```tsx
49
+
asyncfunction serverLogger({
50
+
request,
51
+
params,
52
+
context,
53
+
next,
54
+
}:Route.MiddlewareArgs) {
55
+
let start =performance.now();
56
+
57
+
// 👇 Grab the response here
58
+
let res =awaitnext();
59
+
let duration =performance.now() -start;
60
+
console.log(`Navigated to ${request.url} (${duration}ms)`);
61
+
62
+
// 👇 And return it here
63
+
returnres;
64
+
}
36
65
```
37
66
38
67
You can throw a redirect from a middleware to short circuit any remaining processing:
function serverAuth({ request, params, context, next }:Route.MiddlewareArgs) {
71
+
let user =context.session.get("user");
43
72
if (!user) {
44
-
session.set("returnTo", request.url);
73
+
context.session.set("returnTo", request.url);
45
74
throwredirect("/login", 302);
46
75
}
47
76
context.user=user;
48
77
// No need to call next() if you don't need to do any post processing
49
-
};
78
+
}
50
79
```
51
80
52
-
Note that in the above example, the `next`/`middleware` functions don't return anything. This is by design as on the client there is no "response" to send over the network like there would be for middlewares running on the server. The data is all handled behind the scenes by the stateful `router`.
53
-
54
-
For a server-side middleware, the `next` function will return the HTTP `Response` that React Router will be sending across the wire, thus giving you a chance to make changes as needed. You may throw a new response to short circuit and respond immediately, or you may return a new or altered response to override the default returned by `next()`.
81
+
Here's another example of using a server middleware to detect 404s and check the CMS for a redirect:
0 commit comments