router icon indicating copy to clipboard operation
router copied to clipboard

Unexpected behavior with not found component specified in the __root

Open notcodev opened this issue 1 year ago • 4 comments

Describe the bug

I specified the notFoundComponent in the root and after I throw a not found error in child route (ex. /$type/$id) I got a warning that telling me that I didn't specified a notFoundComponent in child route, but I want catch it at root level to avoid layout rendering

P.S. I checked documentation and found that router has a notFoundMode field in options, after that I specified a 'root' mode and it didn't help

Your Example Website or App

https://stackblitz.com/edit/github-3cnvav-8s3gis?file=src%2Froutes%2F__root.tsx

Steps to Reproduce the Bug or Issue

  1. Open page at path /unknown/id

Expected behavior

Route render root notFoundComponent

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

notcodev avatar Jun 03 '24 19:06 notcodev

If I click on Unknown in your reproducer, I see 123 being rendered which seems to be the root notFoundComponent. Isn't that the expected behavior?

schiller-manuel avatar Jun 03 '24 20:06 schiller-manuel

If I click on Unknown in your reproducer, I see 123 being rendered which seems to be the root notFoundComponent. Isn't that the expected behavior?

But if I open it directly it has unexpected behavior, it opens default tanstack not found component, I'll upload video soon to prove that

notcodev avatar Jun 03 '24 21:06 notcodev

https://github.com/TanStack/router/assets/91194878/df06438b-984b-44f0-abd0-d6b421e7b20d

Look at this, when I open it via link on page it works correctly, but if I reload page it shows default not found component with text "Not found", essentially it should has the same behavior as when opening via link

notcodev avatar Jun 03 '24 21:06 notcodev

I found the way to fix it, I just wrapped all routes in the layout and added notFoundComponent component to it, but I think when I specify notFoundComponent in the root it should work the same

notcodev avatar Jun 03 '24 21:06 notcodev

This is not fixed. In "@tanstack/react-router": "^1.120.20", the root notFoundComponent is ingored in both client navigation and address bar navigation, for both notFoundMode: 'root' and 'fuzzy'

https://stackblitz.com/edit/github-3cnvav-7mr4mpbu?file=src%2Fmain.tsx

https://github.com/user-attachments/assets/09edae08-5587-493b-9cff-be8321f88be5

hanayashiki avatar Jun 10 '25 04:06 hanayashiki

In case anyone is still interested, what fixed this for me was adding a / to the createRoute definitions -- /my-route lead to this problem, /my-route/ fixed it.

aditya-lenze avatar Oct 29 '25 06:10 aditya-lenze