Skip to content
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

After defining routes with patchRoutes, trying to navigate to URL containing #hash is throwing an error #13179

Open
Westman18 opened this issue Mar 7, 2025 · 1 comment · May be fixed by #13224
Assignees
Labels

Comments

@Westman18
Copy link

I'm using React Router as a...

library

Reproduction

  1. Go to router-fragment-navigation-error on Stackblitz
  2. Wait for the app to start
  3. Manually add .../mock route to the URL
  4. Refresh the page and observe the navigation working as expected.
  5. Manually add any fragment (hash) to the working URL (.../mock#test).
  6. Refresh the page and observe the 404 Not Found react router error.

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11850H @ 2.50GHz
    Memory: 8.33 GB / 31.67 GB
  Binaries:
    Node: 20.18.2 - C:\Program Files\nodejs\node.EXE
    npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (129.0.2792.52)
    Internet Explorer: 11.0.19041.4355

Used Package Manager

npm

Expected Behavior

Navigating to a route containing hash should not throw 'no route matches url' error.
(routes are already defined at this point with .patchRoutes())

Actual Behavior

After the routes are defined with .patchRoutes(), navigating to a route containing hash (#) is throwing us an error:
Image
The same navigation, but to route without hash is working correctly in this scenario.

@Westman18 Westman18 added the bug label Mar 7, 2025
@Westman18 Westman18 changed the title After defining routes with .patchRoutes(), trying to navigate to URL containing #hash is throwing an error After defining routes with patchRoutes, trying to navigate to URL containing #hash is throwing an error Mar 7, 2025
@brophdawg11 brophdawg11 linked a pull request Mar 13, 2025 that will close this issue
@brophdawg11 brophdawg11 self-assigned this Mar 13, 2025
@brophdawg11
Copy link
Contributor

Performing a GET navigation is currently subject to some short circuit optimizations because it assumes that there haven't been any mutations that would impact the current state of things. I think there's 2 ways to work around this issue you're currently running into:

Option 1 - Is there a reason you can't do this via patchRoutesOnNavogation?

const router = createBrowserRouter(routes, {
  patchRoutesOnNavigation({ path, patch }) { 
    let routesDefinitions = await loadRouteDefinitionsHoweverYouWant(path);
    patch(null, routesDefinitions);
  },
});

Doing it there let's the router load them during initial hydration to avoid the current "hydrate a 404, then patch in a new route and navigate" flow.

Option 2 - If you use router.revalidate() after you call router.patchRoutes, that should tell the router that it needs to force a full reload which will pick up on the new routes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants