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/routers/create-static-handler.md
+42-3Lines changed: 42 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,13 +82,52 @@ interface StaticHandler {
82
82
83
83
These are the same `routes`/`basename` you would pass to [`createBrowserRouter`][createbrowserrouter]
84
84
85
-
## `handler.query(request)`
85
+
## `handler.query(request, opts)`
86
86
87
87
The `handler.query()` method takes in a Fetch request, performs route matching, and executes all relevant route action/loader methods depending on the request. The return `context` value contains all of the information required to render the HTML document for the request (route-level `actionData`, `loaderData`, `errors`, etc.). If any of the matched routes return or throw a redirect response, then `query()` will return that redirect in the form of Fetch `Response`.
88
88
89
-
##`handler.queryRoute(request, routeId?)`
89
+
### `opts.requestContext`
90
90
91
-
The `handler.queryRoute` is a more-targeted version that queries a singular route and runs it's loader or action based on the request. You can specify a specific `routeId` or let it match the appropriate route automatically based on the request. The return value is the values returned from the loader or action, which is usually a `Response` object.
91
+
If you need to pass information from your server into Remix actions/loaders, you can do so with `opts.requestContext` and it will show up in your actions/loaders in the context parameter.
92
+
93
+
```ts
94
+
const routes = [{
95
+
path: '/',
96
+
loader({ request, context }) {
97
+
// Access `context.dataFormExpressMiddleware` here
98
+
},
99
+
}];
100
+
101
+
exportasyncfunction render(req:express.Request) {
102
+
let { query, dataRoutes } =createStaticHandler(routes);
103
+
let remixRequest =createFetchRequest(request);
104
+
let staticHandlerContext =awaitquery(remixRequest, {
105
+
// Pass data from the express layer to the remix layer here
106
+
requestContext: {
107
+
dataFromExpressMiddleware: req.something
108
+
}
109
+
});
110
+
...
111
+
}
112
+
```
113
+
114
+
## `handler.queryRoute(request, opts)`
115
+
116
+
The `handler.queryRoute` is a more-targeted version that queries a singular route and runs it's loader or action based on the request. By default, it will match the target route based on the request URL. The return value is the values returned from the loader or action, which is usually a `Response` object.
117
+
118
+
### `opts.routeId`
119
+
120
+
If you need to call a specific route action/loader that doesn't exactly correspond to the URL (for example, a parent route loader), you can specify a `routeId`:
If you need to pass information from your server into Remix actions/loaders, you can do so with `opts.requestContext` and it will show up in your actions/loaders in the context parameter. See the example in the `query()` section above.
0 commit comments