Skip to content

Commit adc1c4d

Browse files
committed
Make context readonly
1 parent d8d808c commit adc1c4d

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed
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] When middleware is enabled, make the `context` parameter read-only (via `Readonly<unstable_RouterContextProvider>`) so that TypeScript will not allow you to write arbitrary fields to it in loaders, actions, or middleware.

docs/how-to/middleware.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ This allows you to leave your loaders/actions untouched during initial adoption
274274

275275
<docs-warning>This approach is only intended to be used as a migration strategy when adopting middleware in React Router v7, allowing you to incrementally migrate to `context.set`/`context.get`. It is not safe to assume this approach will work in the next major version of React Router.</docs-warning>
276276

277+
<docs-warning>The `unstable_RouterContextProvider` class is also used for the client-side `context` parameter via `<HydratedRouter unstable_getContext>` and `<RouterProvider unstable_getContext>`. Since `AppLoadContext` is primarily intended as a hand-off from your HTTP server into the React Router handlers, you need to be aware that these augmented fields will not be available in `clientMiddleware`, `clientLoader`, or `clientAction` functions even thought TypeScript will tell you they are (unless, of course, you provide the fields via `unstable_getContext` on the client).</docs-warning>
278+
277279
## Common Patterns
278280

279281
### Authentication

packages/react-router/lib/router/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class unstable_RouterContextProvider {
168168
}
169169

170170
type DefaultContext = MiddlewareEnabled extends true
171-
? unstable_RouterContextProvider
171+
? Readonly<unstable_RouterContextProvider>
172172
: any;
173173

174174
/**
@@ -217,7 +217,7 @@ export interface unstable_MiddlewareNextFunction<Result = unknown> {
217217
* and then complete middlewares from the bottom-up
218218
*/
219219
export type unstable_MiddlewareFunction<Result = unknown> = (
220-
args: DataFunctionArgs<unstable_RouterContextProvider>,
220+
args: DataFunctionArgs<Readonly<unstable_RouterContextProvider>>,
221221
next: unstable_MiddlewareNextFunction<Result>,
222222
) => MaybePromise<Result | void>;
223223

packages/react-router/lib/types/route-data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export type ClientDataFunctionArgs<Params> = {
9898
* from your route middlewares. You may pass in initial context values in your
9999
* `<HydratedRouter unstable_getContext>` prop
100100
*/
101-
context: unstable_RouterContextProvider;
101+
context: Readonly<unstable_RouterContextProvider>;
102102
};
103103

104104
export type ServerDataFunctionArgs<Params> = {
@@ -132,7 +132,7 @@ export type ServerDataFunctionArgs<Params> = {
132132
* `getLoadContext` function.
133133
*/
134134
context: MiddlewareEnabled extends true
135-
? unstable_RouterContextProvider
135+
? Readonly<unstable_RouterContextProvider>
136136
: AppLoadContext;
137137
};
138138

0 commit comments

Comments
 (0)