1- import type { AppLoadContext , RouterContext , ServerBuild } from 'react-router'
1+ import type { AppLoadContext , ServerBuild } from 'react-router'
22import {
33 createContext ,
44 RouterContextProvider ,
@@ -35,20 +35,42 @@ export type GetLoadContextFunction_V8 = (
3535
3636export type RequestHandler = ( request : Request , context : NetlifyContext ) => Promise < Response >
3737
38- let netlifyRouterContext : RouterContext < Partial < NetlifyContext > >
3938/**
4039 * An instance of `ReactContextProvider` providing access to
4140 * [Netlify request context]{@link https://docs.netlify.com/build/functions/api/#netlify-specific-context-object}
4241 *
43- * @example context.get(getNetlifyRouterContext() ).geo?.country?.name
42+ * @example context.get(netlifyRouterContext ).geo?.country?.name
4443 */
45- export const getNetlifyRouterContext = ( ) => {
46- // We must use a singleton because Remix contexts rely on referential equality
47- // We defer initialization until first use so that we can initialize it with a default value
48- // within a request lifecycle, where the `Netlify` global will be set. This is just for dev.
49- netlifyRouterContext ??= createContext < Partial < NetlifyContext > > ( Netlify . context ?? { } )
50- return netlifyRouterContext
51- }
44+ export const netlifyRouterContext =
45+ // We must use a singleton because Remix contexts rely on referential equality.
46+ // We can't hook into the request lifecycle in dev mode, so we use a Proxy to always read from the
47+ // current `Netlify.context` value, which is always contextual to the in-flight request.
48+ createContext < Partial < NetlifyContext > > (
49+ new Proxy (
50+ // Can't reference `Netlify.context` here because it isn't set outside of a request lifecycle
51+ { } ,
52+ {
53+ get ( _target , prop , receiver ) {
54+ return Reflect . get ( Netlify . context ?? { } , prop , receiver )
55+ } ,
56+ set ( _target , prop , value , receiver ) {
57+ return Reflect . set ( Netlify . context ?? { } , prop , value , receiver )
58+ } ,
59+ has ( _target , prop ) {
60+ return Reflect . has ( Netlify . context ?? { } , prop )
61+ } ,
62+ deleteProperty ( _target , prop ) {
63+ return Reflect . deleteProperty ( Netlify . context ?? { } , prop )
64+ } ,
65+ ownKeys ( _target ) {
66+ return Reflect . ownKeys ( Netlify . context ?? { } )
67+ } ,
68+ getOwnPropertyDescriptor ( _target , prop ) {
69+ return Reflect . getOwnPropertyDescriptor ( Netlify . context ?? { } , prop )
70+ } ,
71+ } ,
72+ ) ,
73+ )
5274
5375/**
5476 * Given a build and a callback to get the base loader context, this returns
@@ -73,7 +95,7 @@ export function createRequestHandler({
7395 try {
7496 const getDefaultReactRouterContext = ( ) => {
7597 const ctx = new RouterContextProvider ( )
76- ctx . set ( getNetlifyRouterContext ( ) , netlifyContext )
98+ ctx . set ( netlifyRouterContext , netlifyContext )
7799
78100 // Provide backwards compatibility with previous plain object context
79101 // See https://reactrouter.com/how-to/middleware#migration-from-apploadcontext.
0 commit comments