Skip to content

Commit 35880d6

Browse files
committed
Reorder context/middleware sections
1 parent 7cf5e8c commit 35880d6

File tree

1 file changed

+54
-57
lines changed

1 file changed

+54
-57
lines changed

CHANGELOG.md

+54-57
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ We manage release notes in this file instead of the paginated Github Releases Pa
1717
- [Minor Changes](#minor-changes)
1818
- [Patch Changes](#patch-changes)
1919
- [Unstable Changes](#unstable-changes)
20+
- [Client-side `context` (unstable)](#client-side-context-unstable)
2021
- [Middleware (unstable)](#middleware-unstable)
2122
- [Middleware `context` parameter](#middleware-context-parameter)
22-
- [Client-side `context` (unstable)](#client-side-context-unstable)
2323
- [`unstable_SerializesTo`](#unstable_serializesto)
2424
- [Changes by Package](#changes-by-package)
2525
- [v7.2.0](#v720)
@@ -315,6 +315,8 @@ Date: 2025-03-06
315315
- On `fetcher` calls to undiscovered routes, this mismatch will trigger a document reload of the current path
316316
- `react-router` - Skip resource route flow in dev server in SPA mode ([#13113](https://github.com/remix-run/react-router/pull/13113))
317317
- `react-router` - Fix single fetch `_root.data` requests when a `basename` is used ([#12898](https://github.com/remix-run/react-router/pull/12898))
318+
- `react-router` - Fix types for `loaderData` and `actionData` that contained `Record`s ([#13139](https://github.com/remix-run/react-router/pull/13139))
319+
- ⚠️ This is a breaking change for users who have already adopted `unstable_SerializesTo` - see the note in the `Unstable Changes` section below for more information
318320
- `@react-router/dev` - Fix support for custom client `build.rollupOptions.output.entryFileNames` ([#13098](https://github.com/remix-run/react-router/pull/13098))
319321
- `@react-router/dev` - Fix usage of `prerender` option when `serverBundles` option has been configured or provided by a preset, e.g. `vercelPreset` from `@vercel/react-router` ([#13082](https://github.com/remix-run/react-router/pull/13082))
320322
- `@react-router/dev` - Fix support for custom `build.assetsDir` ([#13077](https://github.com/remix-run/react-router/pull/13077))
@@ -332,16 +334,61 @@ Date: 2025-03-06
332334

333335
⚠️ _[Unstable features](https://reactrouter.com/community/api-development-strategy#unstable-flags) are not recommended for production use_
334336

335-
- `react-router` - Support middleware on routes (unstable) ([#12941](https://github.com/remix-run/react-router/pull/12941))
336-
- See below for more information
337337
- `react-router` - Add `context` support to client side data routers (unstable) ([#12941](https://github.com/remix-run/react-router/pull/12941))
338-
- See below for more information
339-
- `react-router` - Fix types for `loaderData` and `actionData` that contained `Record`s ([#13139](https://github.com/remix-run/react-router/pull/13139))
340-
- ⚠️ This is a breaking change for users who have already adopted `unstable_SerializesTo`
341-
- See below for more information
338+
- `react-router` - Support middleware on routes (unstable) ([#12941](https://github.com/remix-run/react-router/pull/12941))
342339
- `@react-router/dev` - Fix errors with `future.unstable_viteEnvironmentApi` when the `ssr` environment has been configured by another plugin to be a custom `Vite.DevEnvironment` rather than the default `Vite.RunnableDevEnvironment` ([#13008](https://github.com/remix-run/react-router/pull/13008))
343340
- `@react-router/dev` - When `future.unstable_viteEnvironmentApi` is enabled and the `ssr` environment has `optimizeDeps.noDiscovery` disabled, define `optimizeDeps.entries` and `optimizeDeps.include` ([#13007](https://github.com/remix-run/react-router/pull/13007))
344341

342+
#### Client-side `context` (unstable)
343+
344+
Your application `clientLoader`/`clientAction` functions (or `loader`/`action` in library mode) will now receive a `context` parameter on the client. This is an instance of `unstable_RouterContextProvider` that you use with type-safe contexts (similar to `React.createContext`) and is most useful with the corresponding `unstable_clientMiddleware` API:
345+
346+
```ts
347+
import { unstable_createContext } from "react-router";
348+
349+
type User = {
350+
/*...*/
351+
};
352+
353+
const userContext = unstable_createContext<User>();
354+
355+
const sessionMiddleware: Route.unstable_ClientMiddlewareFunction = async ({
356+
context,
357+
}) => {
358+
let user = await getUser();
359+
context.set(userContext, user);
360+
};
361+
362+
export const unstable_clientMiddleware = [sessionMiddleware];
363+
364+
export function clientLoader({ context }: Route.ClientLoaderArgs) {
365+
let user = context.get(userContext);
366+
let profile = await getProfile(user.id);
367+
return { profile };
368+
}
369+
```
370+
371+
Similar to server-side requests, a fresh `context` will be created per navigation (or `fetcher` call). If you have initial data you'd like to populate in the context for every request, you can provide an `unstable_getContext` function at the root of your app:
372+
373+
- Library mode - `createBrowserRouter(routes, { unstable_getContext })`
374+
- Framework mode - `<HydratedRouter unstable_getContext>`
375+
376+
This function should return an value of type `unstable_InitialContext` which is a `Map<unstable_RouterContext, unknown>` of context's and initial values:
377+
378+
```ts
379+
const loggerContext = unstable_createContext<(...args: unknown[]) => void>();
380+
381+
function logger(...args: unknown[]) {
382+
console.log(new Date.toISOString(), ...args);
383+
}
384+
385+
function unstable_getContext() {
386+
let map = new Map();
387+
map.set(loggerContext, logger);
388+
return map;
389+
}
390+
```
391+
345392
#### Middleware (unstable)
346393

347394
Middleware is implemented behind a `future.unstable_middleware` flag. To enable, you must enable the flag and the types in your `react-router-config.ts` file:
@@ -520,56 +567,6 @@ function getLoadContext(req, res): unstable_InitialContext {
520567
}
521568
```
522569

523-
#### Client-side `context` (unstable)
524-
525-
Your application `clientLoader`/`clientAction` functions (or `loader`/`action` in library mode) will now receive a `context` parameter on the client. This is an instance of `unstable_RouterContextProvider` that you use with type-safe contexts (similar to `React.createContext`) and is most useful with the corresponding `unstable_clientMiddleware` API:
526-
527-
```ts
528-
import { unstable_createContext } from "react-router";
529-
530-
type User = {
531-
/*...*/
532-
};
533-
534-
const userContext = unstable_createContext<User>();
535-
536-
const sessionMiddleware: Route.unstable_ClientMiddlewareFunction = async ({
537-
context,
538-
}) => {
539-
let user = await getUser();
540-
context.set(userContext, user);
541-
};
542-
543-
export const unstable_clientMiddleware = [sessionMiddleware];
544-
545-
export function clientLoader({ context }: Route.ClientLoaderArgs) {
546-
let user = context.get(userContext);
547-
let profile = await getProfile(user.id);
548-
return { profile };
549-
}
550-
```
551-
552-
Similar to server-side requests, a fresh `context` will be created per navigation (or `fetcher` call). If you have initial data you'd like to populate in the context for every request, you can provide an `unstable_getContext` function at the root of your app:
553-
554-
- Library mode - `createBrowserRouter(routes, { unstable_getContext })`
555-
- Framework mode - `<HydratedRouter unstable_getContext>`
556-
557-
This function should return an value of type `unstable_InitialContext` which is a `Map<unstable_RouterContext, unknown>` of context's and initial values:
558-
559-
```ts
560-
const loggerContext = unstable_createContext<(...args: unknown[]) => void>();
561-
562-
function logger(...args: unknown[]) {
563-
console.log(new Date.toISOString(), ...args);
564-
}
565-
566-
function unstable_getContext() {
567-
let map = new Map();
568-
map.set(loggerContext, logger);
569-
return map;
570-
}
571-
```
572-
573570
#### `unstable_SerializesTo`
574571

575572
`unstable_SerializesTo` added a way to register custom serialization types in Single Fetch for other library and framework authors like Apollo. It was implemented with branded type whose branded property that was made optional so that casting arbitrary values was easy:

0 commit comments

Comments
 (0)