-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: unstable_patchRoutesOnMiss
remote module error-handling
#11775
Comments
Could you provide a reproduction? Errors thrown from |
ah ok it looks like a bug related to the bubbling of the error - I'll get a fix in for this shortly. also, just FYI you don't need a splat on your path to use this - you could just use |
That is great!
Adding the splat( |
ah - yeah you could solve it that way but I don't think I'd recommend it. That makes it tricky if you ever intended to have a splat in the tree you patch in beneath that. The issue you're running into is that So I would either include both the parent and it's index route in the critical bundle, or neither. but don't just include the parent in the critical bundle: // Include both - this is my suggestion for a root route (`/`) since that's usually
// the route folks will land on as a starting point so it's worth avoiding a
// network waterfall to fetch the homepage IMO
let routes = createBrowserRouter([{
path: '/',
Component: Root,
children: [{
index: true,
Component: Index
}]
}], {
unstable_patchRoutesOnMiss({ patch }) {
// You can then patch addition children into the root route here as needed
// patch('root', someRoutes);
}
});
// Include neither - this is my suggestion for non root routes (`/parent`) since if there's no point
// in including the parent without the index and if it's not a common initial landing page
// then you don't want either in your critical bundle
let routes = createBrowserRouter([{
path: '/',
Component: Root,
children: [{
index: true,
Component: Index
}]
}], {
unstable_patchRoutesOnMiss({ path, patch }) {
if (path === "/parent") {
// Patch both the parent and it's index route in here
// Also assume these routes are async loaded and thus not in the bundle :)
patch(null, [{
path: '/parent',
Component: Parent,
children: [{
index: true,
Component: ParentIndex
}]
});
}
}
}); |
Thanks for the explanation. It felt intuitive to have the parent in the critical bundle with splat; when patching a nested route like Anyway cudos for designing an flexible API which allows different types of tradeoffs! |
Yeah - to be honest that type of "use a splat to discover index" routes approach hadn't occurred to me during implementation and it should work. So if you don't intend to use splat routes below that insertion point it might be a fine solution. I do like the // app.jsx
const router = createBrowserRouter([{
path: '/',
Component: Root,
}, {
path: '/dashboard',
// No Component so we don't bloat the bundle and it'll just be an Outlet
handle: {
lazyChildren: () => import('./dashboard'),
}
}], {
unstable_patchRoutesOnMiss: async ({ matches, path, patch }) => {
...
}
});
// dashboard.tsx
export default routes = [{
path: '',
// Dashboard Component. is moved down to a pathless layout route
// as a sole child of `/dashboard` so it's still in the lazy bundle
Component: Dashboard,
children: [{
index: true,
Component: DashboardIndex,
}, {
path: 'widgets',
component : DashboardWidgets,
}]
}]; That way the only thing that ends up in the critical bundle is the root level I think over time we'll see some common best practices emerge in this are area as folks start using this API. |
Fixed by #11786 and will be available in the next release |
🤖 Hello there, We just published version Thanks! |
Thanks @brophdawg11. I tested that it now bubbles the error correctly to the expected error-boundary in One improvement idea for the future would be to provide the Thanks for the fix! |
Yeah that could be a good idea to keep in mind - maybe instead of being exposed as an 400 |
This would definetly be my preferred way on dealing with it, unless there are some other benefits of using |
🤖 Hello there, We just published version Thanks! |
What version of React Router are you using?
6.24.1
Steps to Reproduce
Guide on createBrowserRoute docs using
Co-locating route discovery with route definition
Expected Behavior
When
maybeLazyChildren
throws an error eg. due to network error withinunstable_patchRoutesOnMiss
, it should allow the error to be captured by one of the error-boundaries defined for the router. This would allows users to customize the error-handling behaviour similarly as with current lazy based approach.Actual Behavior
Throwing an error within
unstable_patchRoutesOnMiss
results in the same behaviour as returningundefined
from the function.One option I was experimenting with was to handle the error in the userland within the
unstable_patchRoutesOnMiss
and return aelement
showing an error-screen instead, but this wouldn't allow users to retry & recover since the discovery happens just once.The text was updated successfully, but these errors were encountered: