-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
useRouteLoaderData("root")
returns undefined
in root's ErrorBoundary
/Layout
when non-existent page is loaded right away.
#9209
Comments
useRouteLoaderData('root')
returns undefined
in the root's ErrorBoundary
/Layout
when non-existed page is loaded right away.undefined
in root's ErrorBoundary
/Layout
when non-existed page is loaded right away.
undefined
in root's ErrorBoundary
/Layout
when non-existed page is loaded right away.undefined
in root's ErrorBoundary
/Layout
when non-existent page is loaded right away.
This might give you the answer |
Thanks! Checked it out and it suggests to use the |
Take a look at https://github.com/kiliman/remix-global-data This explains the reasons and gives a possible solution. |
undefined
in root's ErrorBoundary
/Layout
when non-existent page is loaded right away.useRouteLoaderData
returns undefined
in root's ErrorBoundary
/Layout
when non-existent page is loaded right away.
useRouteLoaderData
returns undefined
in root's ErrorBoundary
/Layout
when non-existent page is loaded right away.useRouteLoaderData("root")
returns undefined
in root's ErrorBoundary
/Layout
when non-existent page is loaded right away.
I couldn't find any explanation for the problem i described in the linked article. It explains why Also, not sure the global data approach suggested by the article would work for the CSP nonce since it has to be generated on every request and not just once when the app starts. |
Remix doesn't bother calling the root loader for a route that doesn't exist. That's why even What data are you trying to show in your |
Well,
Please, check the stackblitz that i provided. It highlights both of the above use-cases. |
If you need 404 to execute the root loader, add a splat route and throw a 404 response. This will force Remix to go through the usual route process. // routes/$.tsx
export function loader() {
throw new Response('Not Found', { status: 404 });
}
export default function Component() {
return null;
} https://stackblitz.com/edit/remix-run-remix-z7wwii?file=app%2Froutes%2F%24.tsx |
Yep, this solved the problem for me! Thank you! |
Noticed one thing though. The This probably deserves a separate issue. There is one issue closed that looks similar to what i'm experiencing - #1136 (comment) Edited: the FOUC doesn't happen 100% of the time. Seems like it has something to do with how fast you actually click back/forward after landing on a page. |
I'm going to close this out as expected since without a splat route no route paths match so there's no root match to render. The splat route solution recommended above is the correct solution. As for the FOUC, I believe that's expected but if you wouldn't mind opening a new issue we could use that to discuss and confirm. From a quick look, I think it's a vite dev only issue since vite does a request for a 304 not modified response for assets in dev mode - if you do The reason the layout get's unmounted/remounted is that under the hood the React Router tree looks essentially like this: let routes = [{
id: 'root',
path: '/',
element: <Layout><RootComponent /></Layout>,
errorElement: <Layout><RootErrorBoundary /></Layout>,
children: ...
}]; So it's a new The workaround for now is to use a pathless route for the error boundary which would allow the root Layout to remain mounted when going from UI to 404 routes:
|
Reproduction
I'm not sure if the behaviour is expected but this breaks the csp nonce use-case for me since it's returned as part of the root loader's data. If this is expected, how should i get the csp nonce instead?
System Info
Used Package Manager
npm
Expected Behavior
useRouteLoaderData("root")
used in root'sLayout
/ErrorBoundary
components returns root loader's data when hitting a URL with no matching route.Actual Behavior
useRouteLoaderData("root")
used in root'sLayout
/ErrorBoundary
components returnsundefined
when hitting a URL with no matching route.The text was updated successfully, but these errors were encountered: