Skip to content
Source
Sign inSign up
TanStack/router

fix(react-router): route-context being available and loaders being called after a redirection #1713

MergedSeanCassieremerged 48 commits intomain fromrefs/pull/1713/headmerged by SeanCassiere

SeanCassierecommented

This PR initially was just to add tests for the route-context checks after navigation, but it became a bit more.

  • It checks route context being available after a navigation.
  • It checks route context being available after a redirection.
  • It checks that the destination route loaders are being fired when redirected to.
  • The tests introduced in #1710 are also being amended to better simulate a "real-world experience". No more relying on calls to router.navigate(), instead a button is clicked which calls navigate from useNavigate().
  • The tests in packages/react-router/tests/redirects.test.tsx related to route-context have been REMOVED in favor of the ones in packages/react-router/tests/routeContext.test.tsx as they simulate real-world usage.

I've manually tested these changes against the reproductions in the following issues and can confirm that this PR fixes the following.

Context-related issues still remain broken in this though.

  • The original reproduction in #1656

Thanks to @freshgiammi pointing out the unnecessary this.load().

    1. ☁️ Nx Cloud Report

      CI is running/has finished running commands for commit d65e453d2f287986b14a8fd9a7ea5bb6584d022c. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

      📂 See all runs for this CI Pipeline Execution


      ✅ Successfully ran 4 targets

    2. Sent with 💌 from NxCloud.

  • SeanCassierecommented

    Just keep track of failures logged out.

    Details
     FAIL  |react-router| tests/routeContext.test.tsx > beforeLoad in the route definition > on navigate, route context is present in the /person route after a redirect is thrown in the beforeLoad of the /about route
    AssertionError: expected '/about' to be '/person' // Object.is equality
    
    • Expected
    • Received
    • /person
    • /about

    ❯ tests/routeContext.test.tsx:247:38 245| 246| await sleep(WAIT_TIME * SLEEP_MODIFIER) 247| expect(window.location.pathname).toBe('/person') | ^ 248| expect(router.state.location.pathname).toBe('/person') 249|

    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/5]⎯

    FAIL |react-router| tests/routeContext.test.tsx > beforeLoad in the route definition > on navigate, route context is present in the /person route after a redirect is thrown in the loader of the /about route AssertionError: expected '/about' to be '/person' // Object.is equality

    • Expected
    • Received
    • /person
    • /about

    ❯ tests/routeContext.test.tsx:289:38 287| 288| await sleep(WAIT_TIME * SLEEP_MODIFIER) 289| expect(window.location.pathname).toBe('/person') | ^ 290| expect(router.state.location.pathname).toBe('/person') 291|

    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/5]⎯

    FAIL |react-router| tests/routeContext.test.tsx > loader in the route definition > on navigate, route context is present in the /person route after a redirect is thrown in the beforeLoad of the /about route AssertionError: expected '/about' to be '/person' // Object.is equality

    • Expected
    • Received
    • /person
    • /about

    ❯ tests/routeContext.test.tsx:511:38 509| 510| await sleep(WAIT_TIME * SLEEP_MODIFIER) 511| expect(window.location.pathname).toBe('/person') | ^ 512| expect(router.state.location.pathname).toBe('/person') 513|

    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/5]⎯

    FAIL |react-router| tests/routeContext.test.tsx > loader in the route definition > on navigate, route context is present in the /person route after a redirect is thrown in the loader of the /about route AssertionError: expected '/about' to be '/person' // Object.is equality

    • Expected
    • Received
    • /person
    • /about

    ❯ tests/routeContext.test.tsx:555:38 553| 554| await sleep(WAIT_TIME * SLEEP_MODIFIER) 555| expect(window.location.pathname).toBe('/person') | ^ 556| expect(router.state.location.pathname).toBe('/person') 557|

    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/5]⎯

    FAIL |react-router| tests/routeContext.test.tsx > useRouteContext in the component > on navigate, route context is present in the /person route after a redirect is thrown in the beforeLoad of the /about route TestingLibraryElementError: Unable to find an element with the text: {"foo":"bar"}. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.

    Ignored nodes: comments, script, style

    Ignored nodes: comments, script, style

    ❯ waitForWrapper ../../node_modules/.pnpm/@testing-library+dom@10.0.0/node_modules/@testing-library/dom/dist/wait-for.js:163:27 ❯ ../../node_modules/.pnpm/@testing-library+dom@10.0.0/node_modules/@testing-library/dom/dist/query-helpers.js:86:33 ❯ tests/routeContext.test.tsx:859:34 857| fireEvent.click(linkToAbout) 858| 859| const content = await screen.findByText(JSON.stringify({ foo: 'bar' })) | ^ 860| await waitFor(() => expect(content).toBeInTheDocument()) 861|

    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/5]⎯

    Test Files 1 failed | 22 passed (23) Tests 5 failed | 277 passed (282) Type Errors no errors Start at 12:24:18 Duration 29.31s

  • SeanCassierecommented

    The remaining tests

    Details
     FAIL  |react-router| tests/routeContext.test.tsx > beforeLoad in the route definition > on navigate, route context is present in the /person route after a redirect is thrown in the beforeLoad of the /about route
    AssertionError: expected '/about' to be '/person' // Object.is equality
    
    • Expected
    • Received
    • /person
    • /about

    ❯ tests/routeContext.test.tsx:247:38 245| 246| await sleep(WAIT_TIME * SLEEP_MODIFIER) 247| expect(window.location.pathname).toBe('/person') | ^ 248| expect(router.state.location.pathname).toBe('/person') 249|

    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/4]⎯

    FAIL |react-router| tests/routeContext.test.tsx > beforeLoad in the route definition > on navigate, route context is present in the /person route after a redirect is thrown in the loader of the /about route AssertionError: expected '/about' to be '/person' // Object.is equality

    • Expected
    • Received
    • /person
    • /about

    ❯ tests/routeContext.test.tsx:289:38 287| 288| await sleep(WAIT_TIME * SLEEP_MODIFIER) 289| expect(window.location.pathname).toBe('/person') | ^ 290| expect(router.state.location.pathname).toBe('/person') 291|

    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/4]⎯

    FAIL |react-router| tests/routeContext.test.tsx > loader in the route definition > on navigate, route context is present in the /person route after a redirect is thrown in the beforeLoad of the /about route AssertionError: expected '/about' to be '/person' // Object.is equality

    • Expected
    • Received
    • /person
    • /about

    ❯ tests/routeContext.test.tsx:511:38 509| 510| await sleep(WAIT_TIME * SLEEP_MODIFIER) 511| expect(window.location.pathname).toBe('/person') | ^ 512| expect(router.state.location.pathname).toBe('/person') 513|

    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/4]⎯

    FAIL |react-router| tests/routeContext.test.tsx > loader in the route definition > on navigate, route context is present in the /person route after a redirect is thrown in the loader of the /about route AssertionError: expected '/about' to be '/person' // Object.is equality

    • Expected
    • Received
    • /person
    • /about

    ❯ tests/routeContext.test.tsx:555:38 553| 554| await sleep(WAIT_TIME * SLEEP_MODIFIER) 555| expect(window.location.pathname).toBe('/person') | ^ 556| expect(router.state.location.pathname).toBe('/person') 557|

    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/4]⎯

    Test Files 1 failed | 22 passed (23) Tests 4 failed | 278 passed (282) Type Errors no errors Start at 12:27:03 Duration 29.04s

  • SeanCassierecommented

    So far, I've tracked it down the this.navigate and subsequent this.load call over here in router.ts#L1546.

    The problem can be observed when a navigation is attempted to a route that then performs a redirection AFTER the initial load.

    In the tests so far, the location isn't being correctly updated in either the router's internal state or the window.location.pathname.

     FAIL  |react-router| tests/routeContext.test.tsx > loader in the route definition > on navigate, route context is present in the /person route after a redirect is thrown in the loader of the /about route
    AssertionError: expected '/about' to be '/person' // Object.is equality
    
    - Expected
    + Received
    
    - /person
    + /about
    
     ❯ tests/routeContext.test.tsx:558:38
        556| 
        557|     await sleep(WAIT_TIME * SLEEP_MODIFIER)
        558|     expect(window.location.pathname).toBe('/person')
           |                                      ^
        559|     expect(router.state.location.pathname).toBe('/person')
        560| 

    Something to do with the throwing of the redirect seems to be shortcircuiting the navigation here 🤔.

  • SeanCassierecommented

    For whatever reason 8a31dee, fixes the tests. This resolved the problem where the URL wasn't being updated.

    I used useEffect instead of useLayoutEffect since the router was being unsubscribed from the history being used.

    The change here in router.ts, however, is not something I fully understand yet.

    Edit: Testing with layout routes, broke this again 🤯

  • SeanCassiere reviewed

    packages/react-router/src/Transitioner.tsx line 32
    @@ -29,4 +29,4 @@
    2929
    3030 // Subscribe to location changes
    3131 // and try to load the new location
    32- useLayoutEffect(() => {
    32+ React.useEffect(() => {

    Did this since the cleanup function from useLayoutEffect kept running after a redirect was thrown without ever completing a repaint afterwards.

  • SeanCassiere reviewed

    packages/react-router/src/router.ts line 1786
    @@ -1763,18 +1763,7 @@
    17631763 parentContext,
    17641764 ),
    1765+ context: replaceEqualDeep(match.context, parentContext),
    17651766 abortController,
    1766- }))
    1767+ }
    17671768
    1768- const beforeLoadContext =
    1769- (await route.options.beforeLoad?.({
    1770- search: match.search,
    1771- abortController,
    1772- params: match.params,
    1773- preload: !!preload,
    1774- context: parentContext,
    1775- location,
    1776- navigate: (opts: any) =>
    1777- this.navigate({ ...opts, from: match.pathname }),
    1778- buildLocation: this.buildLocation,
    1779- cause: preload ? 'preload' : match.cause,
    1780- })) ?? ({} as any)
    1769+ const beforeLoadFnContext = {

    Just broken this out of the call scope to better match what we are doing with the loader below.

  • SeanCassiere reviewed

    Resolvedby SeanCassiere
  • SeanCassiere reviewed

    OutdatedResolvedby SeanCassiere
  • schiller-manuel(GitHub profile, opens in a new tab)replied on packages/react-router/src/router.ts line 1910

    what happens if you do not comment this?

  • SeanCassierereplied on packages/react-router/src/router.ts line 1910

    If I restore it to what it originally was:

    ...
                          // we can use the options
                          await lazyPromise
    
                          checkLatest()
    
                          // Kick off the loader!
                          loaderPromise = route.options.loader?.(loaderContext)

    This is the output: output.md

  • SeanCassiere reviewed

    OutdatedResolvedby SeanCassiere
  • SeanCassiere reviewed

    packages/react-router/src/router.ts line 1551
    @@ -1548,4 +1548,4 @@
    15481548 redirect = err
    15491549 if (!this.isServer) {
    15501550 this.navigate({ ...err, replace: true, __isRedirect: true })
    1551- this.load()
    1551+ // this.load()

    The fix pointed out by @freshgiammi.

  • SeanCassiere changed the title

  • SeanCassiere reviewed

    packages/react-router/tests/redirects.test.tsx line 467
    @@ -463,5 +459,3 @@
    463459 expect(router.state.location.pathname).toBe('/g/tkdodo')
    464460 })
    465461 })
    466-
    467-function createContextRouter<TCtx extends Record<string, unknown>>(
    SeanCassierecommented

    Removed since this is no longer representative of what's actually happening in the browser. Plus these test-cases are already covered in tests/routeContext.test.tsx.

  • SeanCassiere merged this950561a

  • SeanCassiere closed this

  • Hello,

    It seems that this version introduced a bug.

    I have a simple router with an index.tsx that redirects to a login page with beforeLoad:

    export const Route = createFileRoute('/')({
      beforeLoad: () => {
        throw redirect({ to: '/login' });
      },
    });
    export const Route = createFileRoute('/login')({
      component: Login,
    });

    When I refresh my browser on "/", I get a blank page.

    It was working in previous versions.

    I can provide the entire code later.

  • I noticed that writing to the context of one route will now overwrite the context of all routes, so the router context is no longer isolated per route.

    this is confirmed working in v1.36.2 but no longer in v1.36.3, and this release just has this fix, so I think we can trace it back here

    compare this breadcrumbs example on:

  • Sign in to join this conversation.