router: [TanStack Start] HMR is not working from root route (__root)
Describe the bug
HMR is not working in root route. only working from normal routes.
Your Example Website or App
https://github.com/TanStack/router/tree/main/examples/react/start-basic-rsc
Steps to Reproduce the Bug or Issue
- Clone (or copy or degit) start-basic-rsc example, install dependencies and run dev server.
- Open http://localhost:3000
- Try to edit from
__root.tsx. I just added a link beside Home.
Expected behavior
In terminal, dev server printed a log about page has reloaded.
[vite] page reload app/routes/__root.tsx
but the changes are not reflected in the browser.
Screenshots or Videos
https://github.com/user-attachments/assets/26aaa1a3-80c1-40af-8dd8-f2403051519e
Platform
- OS: macOs
- Browser: Arc / Chrome
- Version: 126.0.6478.127
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 36
- Comments: 43 (5 by maintainers)
For anyone reading this who’s looking for a workaround until a fix is merged, you can add the following to your
__root.tsxto enable HMR in Tanstack Start:This does the same thing as #2286 and #2316 (albeit in a slightly more hacky way). Once the fix is merged, you should be able to safely remove this code from your
__root.tsxagainTo summarize, we see the following HMR problems with file based routing:
HMR is not triggered when editing
routes/__root.tsxcomponents/Example.tsximported into anyroutes/**.tsxfileroutes/_layout.tsxHMR is triggered when editing
routes/index.tsxroutes/example.example.tsx(@anulman are you sure this doesn’t work for any plain route (no_prefix) file?)routes/with-directory/example.tsxReproduction
All of this can be testet with the Start Basic StackBlitz. Create a new File
app/components/Example.tsx:Now import this into
app/routes/index.tsxand render<Example />:While editing “Welcome Home!!!” will trigger HMR, editing “Changing this will not trigger HMR” will not. The latter shows
in the console, but a manual reload is necessary.
This is crazy HMR is not working on any file outside of the
routesfolder (Tanstack Start) ? Since September ?I’m experiencing this as well, not just in my root route but in any top-level route.
I also have not been able to separate out
components/Foo.tsxfiles and have them hot-reload. The dev console & JS console confirm the HMR event has been emitted and received, but the React JS—even for route sub-components modified in non-route files (e.g.import { FooPage } from '~/components/FooPage';) does not re-render the HMR’ed component.The HMR fixes by @lithdew have been merged in and most of the HMR issues should be resolved with the https://github.com/TanStack/router/releases/tag/v1.99.3.
If more, HMR bugs pop up, please open a new Issue on github so that it can be tracked separately.
app/routes/__root.tsxThis work for me
We just added TanStack router to our vite-based project, and now we are also experiencing this HMR failure. I tried a few different routes, it does seem to only be happening with __root.tsx.
Thanks, @MarkSFrancis 's solution works. But the value of
scriptsshould be a regular array, not a function that returns array. Otherwise I get type errors.Here’s a version that works with
"@tanstack/react-router": "^1.95.3"and"@tanstack/start": "^1.95.3":I was pulling my hair and wondering why my changes outside of the
routes/but still underapp/are not properly watched. I find it odd that vite’s hrm alludes that it did update when it didn’t. Main reason for the hair pulling. Example of my console:My working directory as followed:
My `package.json’ has the following:
The workaround on this thread that adds a script tag injecting the refresh runtime will throw an error if added to the
__root.tsxor anywhere else. The error thrown in the browser is the following:Any new suggestions will be happy to add to test. For now we keep manually
⌘ + r. Happy holidays@MarkSFrancis Lifesaver, been trying to work Start into a side project of mine and was pretty sad about the HMR (but its alpha so who cares). A great fix for now until the team gets it going!
okay thanks, I just tried it now and it’s still not working 🫤. I litereally copy pasted the head you have into my root, I made sure i’m exporting the RootComponent, I even tried moving it into its own file(-RootComponent.tsx), the page never hot reloaded when I changed a component inside RouteComponent.
@MarkSFrancis Thanks!
It didn’t work for me at first. I had to marked my Route components for
exportand now it works!ie instead of:
I had to do:
@yassine-safraoui I messed it up. It should go inside the return value of the
headproperty’s function. Just updated my commentas soon as I used functions instead of const, it worked. You’re god’s send!
I’m facing the same issue…
FYI I’m experiencing the same issue (all expect the root routes perform a full reload) when enabling autoCodeSplitting in my tsr.config.json 🫤
@sngmn451 thank you! worked for me too
Currently, while the page is indeed reloaded upon file modification, it does a full page reload instead of a HMR. But this only happens on regular routes, not the __root route (on which HMR works). Using Tanstack Start
For me this was the issue in all routes! bless you
@MarkSFrancis - worked wonders for me trying to get authentication working, as soon as i added useServerFn everything exploded!! Found this issue and things are rolling again
none of those fixed my issue. I’m not using tanstack start, only tanstack router, but i have the exact same problem