Skip to content

Commit a6a8886

Browse files
authored
Adopt unstable_stream API for internal middleware implementation (#14103)
1 parent 84baf0c commit a6a8886

File tree

9 files changed

+869
-616
lines changed

9 files changed

+869
-616
lines changed

.changeset/rare-mice-report.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
[UNSTABLE] Rename and alter the signature/functionality of the `unstable_respond` API in `staticHandler.query`/`staticHandler.queryRoute`
6+
7+
- The API has been renamed to `unstable_generateMiddlewareResponse` for clarity
8+
- The main functional change is that instead of running the loaders/actions before calling `unstable_respond` and handing you the result, we now pass a `query`/`queryRoute` function as a parameter and you execute the loaders/actions inside your callback, giving you full access to pre-processing and error handling
9+
- The `query` version of the API now has a signature of `(query: (r: Request) => Promise<StaticHandlerContext | Response>) => Promise<Response>`
10+
- The `queryRoute` version of the API now has a signature of `(queryRoute: (r: Request) => Promise<Response>) => Promise<Response>`
11+
- This allows for more advanced usages such as running logic before/after calling `query` and direct error handling of errors thrown from query
12+
- ⚠️ This is a breaking change if you've adopted the `staticHandler` `unstable_respond` API
13+
14+
```tsx
15+
let response = await staticHandler.query(request, {
16+
requestContext: new unstable_RouterContextProvider(),
17+
async unstable_generateMiddlewareResponse(query) {
18+
try {
19+
// At this point we've run middleware top-down so we need to call the
20+
// handlers and generate the Response to bubble back up the middleware
21+
let result = await query(request);
22+
if (isResponse(result)) {
23+
return result; // Redirects, etc.
24+
}
25+
return await generateHtmlResponse(result);
26+
} catch (error: unknown) {
27+
return generateErrorResponse(error);
28+
}
29+
},
30+
});
31+
```

.changeset/rare-mice-report2.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
[UNSTABLE] Convert internal middleware implementations to use the new `unstable_generateMiddlewareResponse` API

0 commit comments

Comments
 (0)