router: HMR crash (under very specific circumstances)

Describe the bug

I’ve got a real interesting HMR bug. It only happens under very specific circumstances.

This is the error message

Invariant failed: No match found for route '/_layout/a/b' while rendering the '/_layout/_layout/a/b' route. Did you mean to pass '{ strict: false }' instead?

Of course there’s only one _layout file in my file tree, not two, so tanstack is adding an extra layout.

Your Example Website or App

https://stackblitz.com/edit/github-szfxdx?file=README.md

Steps to Reproduce the Bug or Issue

  1. pnpm install
  2. pnpm dev
  3. open http://localhost:3999 (you’ll be redirected to http://localhost:3999/a/b, which is the problematic route)
  4. In your editor, open bQuery.graphql.ts
  5. With the browser still open, press save inside the file.
  6. HMR should trigger and render this error:
Invariant failed: No match found for route '/_layout/a/b' while rendering the '/_layout/_layout/a/b' route. Did you mean to pass '{ strict: false }' instead?

Expected behavior

HMR shouldn’t crash the page when editing files.

Screenshots or Videos

No response

Platform

Stackblitz

Additional context

I can only get it to reproduce with Relay in the mix, but the double _layout in the error message still makes me believe it’s tanstack causing the error.

The error only happens under the very specific circumstances I put together in the repro. If the main.tsx and router.tsx files are merged into one, the error disappears. Without lazy routes, the error disappears. Without the nested layout, the error disappears.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20 (9 by maintainers)

Most upvoted comments

So I managed to fix my problem and I’m not sure it is related to this issue after all but in case someone ends up here after searching HMR, here is what I found. It seems that if a context provider is declared outside of the RouterProvider, it will be lost on HMR:

<MyProvider>
  <RouterProvider router={router} />
</MyProvider>

is not ok. Instead I needed to move MyProvider inside the __root.

Having similar issue, It occurs when i use useSearch and the route is under _protected

const search = useSearch({ from: ‘/_protected/a/b/’ }) Error: Invariant failed: Could not find an active match from "/_protected/a/b/"

when i try: const search = useSearch({ from: ‘/a/b/’ })

The issue disappears, but i getting type error: Type '"/a/b/"' is not assignable to type '"__root__" | "/" | "/_protected/a/b" | .....

using strict: false helps const search = useSearch({ strict: false })

This is one funky bug