notFound() won't emit head data if `__root` has an async loader #6085
Send to my agentWhich project does this relate to?#
Router
Describe the bug#
when having a route's loader throw notFound, the not found page is rendered without head if the __root route's loader is not complete.
Your Example Website or App#
https://github.com/paperclover/bug-reproductions/tree/tanstack-start-head-omitted
Steps to Reproduce the Bug or Issue#
npm inpm run dev- open
http://localhost:3000, navigate to see css exists - open
http://localhost:3000/not-foundon new tab, no css
Expected behavior#
to wait for __root's loader and render the head data.
Screenshots or Videos#
No response
Platform#
- Router / Start Version: 1.130.2 / 1.135.2
- OS: macOS
- Browser: N/A
- Browser Version: N/A
- Bundler: vite
- Bundler Version: ^7.1.7
Additional context#
blocking a pr at work to properly handle some 404 cases where we are actually showing our ErrorComponent
so i thought i could work around this by putting the root loader logic into beforeLoad, but now if that fails i hit https://github.com/TanStack/router/issues/4684. so there isn't actually a way to perfectly work around this bug.
in the work app this means that if one of the backend services are down, a fetch or db failure. and these do happen (but rarely) according to observability.
your reproduction uses this in the route, which does not work on the client
import { setTimeout } from "node:timers/promises";if you replace it it works as expected:
diff --git a/src/routes/__root.tsx b/src/routes/__root.tsx index 830ed0b..2235ac9 100644 --- a/src/routes/__root.tsx +++ b/src/routes/__root.tsx @@ -9,7 +9,6 @@ import { import { DefaultCatchBoundary } from "../components/DefaultCatchBoundary.js"; import { NotFound } from "../components/NotFound.js"; import css from "../app.css?url"; -import { setTimeout } from "node:timers/promises"; export const Route = createRootRoute({ head: () => ({ @@ -32,7 +31,8 @@ export const Route = createRootRoute({ // remove async loader to fix bug async loader() { - await setTimeout(100); // causes bug + await new Promise((resolve) => setTimeout(resolve, 100)); },oh shoot my bad. but changing it still causes the bug to reproduce, but only on the initial page load. this was what we were facing at work.
if you replace it it works as expected:
did you try doing a page refresh on it? that is the bug.
the workaround for us is to add code on every notFound to call
ensureQueryDatafor the query that we have in our root's loader, so that the root loader completes before notFound is throwncan reproduce this on initial page load, looking into it
I think this issue might be related. Mind taking a look? https://github.com/TanStack/router/issues/5960
Sign in to join this conversation.
📝 CodeRabbit Plan Mode#
Generate an implementation plan and prompts that you can use with your favorite coding agent.
Examples
🔗 Similar Issues#
Possible Duplicates
Related Issues
🔗 Related PRs#
TanStack/router#5495 - fix(react-router,solid-router): top-level
Matchesshould usependingComponenton root route [merged] TanStack/router#5500 - docs(beforeLoad & notFoundComponents): Add note regarding notFound error thrown from beforeLoad function [merged] TanStack/router#5518 - Solid hydration [merged] TanStack/router#5686 - fix(router-core): Catch errors thrown during hydrate [merged] TanStack/router#5890 - Fix: Route with server function as loader that throws notFound crashes route on HMR #5763 [merged]👤 Suggested Assignees#
🧪 Issue enrichment is currently in early access.
To disable automatic issue enrichment, add the following to your
.coderabbit.yaml: