Repository navigation
[Proposal] Support a custom fetch function on HydratedRouter for manifest and data requests #15472
Closed
jasonhuang-dev
started this conversation in
Proposals
Replies: 2 comments
|
I have this implemented in #15465, but I need to fix some conflicts and update it to apply to manifest fetches as well, that's a good catch. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Summary
We propose adding a
fetchprop toHydratedRouter(and potentiallyRouterProvider) that allows applications to supply a custom function to handle all internal fetch requests made by React Router — including the__manifestendpoint and.datarequests.This was briefly proposed by @rossipedia in #14800, and we'd like to expand on that idea with an additional motivating use case and a more detailed API proposal.
Motivation
Use case 1: Detecting client/server version mismatch (from #14800)
As discussed in #14800, applications running rolling deployments need to detect when the server has been updated to a new version and react accordingly (e.g. trigger a document reload, show a "new version available" banner). A custom
fetchfunction makes it straightforward to inspect response headers and act on version information.Use case 2: CDN / gateway region routing for manifest requests
In applications deployed across multiple regions with rolling deployments, a
__manifestrequest from a client that was served by Region A may be routed by the CDN to Region B — which may not yet have the new version deployed. Many CDN and API gateway setups support pinning a request to a specific origin or region via a custom request header (routing hint headers are a common pattern in multi-region infrastructure).Currently, there is no supported way to inject such headers into React Router's internal
fetch(url, { signal })call for the manifest endpoint. The only workaround is monkey-patchingwindow.fetch, which is undesirable because it:This also makes it possible to replace React Router's default
window.fetchentirely with a third-party HTTP client such as axios, enabling features like automatic retries, request interceptors, and richer error handling out of the box.Proposed API
Add an optional
fetchprop toHydratedRouter:The prop would default to
window.fetchif not provided, making it fully backward compatible.Type signature:
Why
HydratedRouter(notreact-router.config.ts)?react-router.config.tsis a build-time configuration. Custom fetch logic typically requires runtime values — e.g. a region identifier embedded in the HTML by the server, auth tokens stored in memory, or environment-specific configuration.HydratedRouterlives inentry.client.tsxand is the natural place for client-side runtime customization.Additional use cases this unlocks
Beyond the two motivating examples above, a custom
fetchfunction enables the community to implement patterns that are currently impossible or require monkey-patching:Authorization: Bearer ...) on.datarequestsRelated
fetchonHydratedRoutersuggested by @rossipedia in comments)All reactions