fix: populate value for Netlify React Router context in dev #550
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 changesnetlifyRouterContextto a proxy so that we can lazily evaluate the context properties fromNetlify.contextduring 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 linkthis PR, and runnpx netlify devandnpx netlify deploy. Both work.