Skip to content

Conversation

@serhalp
Copy link
Member

@serhalp serhalp commented Oct 16, 2025

Description

Follow-up to #546.

Since the netlifyRouterContext.set() call was occurring in the server entry point built for production but not in dev, and no default value was set for the context at construction time, using middleware in dev would throw.

There's no ergonomic way to set up a .set() call dynamically on a context in dev, as far as I can tell. This leaves the default value set at construction time as our only option.

To make this work, we can use the global Netlify.context. However, this is only accessible during a request-response lifecycle, not in module scope at module init time. So this commit simply changes netlifyRouterContext to a proxy so that we can lazily evaluate the context properties from Netlify.context during a request lifecycle.

To be clear, the Netlify router context will only work in local dev when using either the Netlify CLI or @netlify/vite-plugin.

Related Tickets & Documents

Follow-up to #546

See also netlify/react-router-template#12

QA Instructions, Screenshots, Recordings

Clone netlify/react-router-template#12, npm link this PR, and run npx netlify dev and npx netlify deploy. Both work.

@netlify
Copy link

netlify bot commented Oct 16, 2025

Deploy Preview for remix-edge ready!

Name Link
🔨 Latest commit df7719a
🔍 Latest deploy log https://app.netlify.com/projects/remix-edge/deploys/68f27068fcc816000802f5e7
😎 Deploy Preview https://deploy-preview-550--remix-edge.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link

netlify bot commented Oct 16, 2025

Deploy Preview for remix-serverless ready!

Name Link
🔨 Latest commit df7719a
🔍 Latest deploy log https://app.netlify.com/projects/remix-serverless/deploys/68f270687a8066000888ce12
😎 Deploy Preview https://deploy-preview-550--remix-serverless.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions bot added the type: bug code to address defects in shipped code label Oct 16, 2025
@serhalp serhalp force-pushed the fix/react-router-middleware-dev branch from 7c1fbbc to 63e18b1 Compare October 16, 2025 21:04
@serhalp serhalp marked this pull request as ready for review October 16, 2025 21:04
@serhalp serhalp requested a review from a team as a code owner October 16, 2025 21:04
Follow-up to #546.

Since the `netlifyRouterContext.set()` call was occurring in the server entry point built for
production but not in dev, and no default value was set for the context at construction time, using
middleware in dev would throw.

There's no ergonomic way to set up a `.set()` call dynamically on a context in dev, as far as I can
tell. This leaves the default value set at construction time as our only option.

To make this work, we can use the global `Netlify.context`. However, this is only accessible during
a request-response lifecycle, not in module scope at module init time. So this commit simply changes
`netlifyRouterContext` to a function `getNetlifyRouterContext()` so that we can lazily construct the
context with a default value of `Netlify.context` during a request lifecycle.

To be clear, the Netlify router context will only work in local dev when using either the Netlify
CLI or `@netlify/vite-plugin`.
@serhalp serhalp force-pushed the fix/react-router-middleware-dev branch 2 times, most recently from cd9a5a9 to 83292a9 Compare October 17, 2025 16:34
@serhalp serhalp force-pushed the fix/react-router-middleware-dev branch from 83292a9 to df7719a Compare October 17, 2025 16:35
@serhalp serhalp merged commit 8d2c9fd into main Oct 20, 2025
22 of 25 checks passed
@serhalp serhalp deleted the fix/react-router-middleware-dev branch October 20, 2025 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug code to address defects in shipped code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants