Skip to content

Commit 24f80ac

Browse files
committed
Add docs on requetsContext
1 parent 095ee7f commit 24f80ac

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

docs/routers/create-static-handler.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,52 @@ interface StaticHandler {
8282

8383
These are the same `routes`/`basename` you would pass to [`createBrowserRouter`][createbrowserrouter]
8484

85-
## `handler.query(request)`
85+
## `handler.query(request, opts)`
8686

8787
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`.
8888

89-
## `handler.queryRoute(request, routeId?)`
89+
### `opts.requestContext`
9090

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+
export async function render(req: express.Request) {
102+
let { query, dataRoutes } = createStaticHandler(routes);
103+
let remixRequest = createFetchRequest(request);
104+
let staticHandlerContext = await query(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`:
121+
122+
```js
123+
staticHandler.queryRoute(new Request("/parent/child"), {
124+
routeId: "parent",
125+
});
126+
```
127+
128+
### `opts.requestContext`
129+
130+
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.
92131

93132
**See also:**
94133

0 commit comments

Comments
 (0)