@@ -339,3 +339,71 @@ test('throw error from beforeLoad when navigating to route', async () => {
339339 const indexElement = await screen . findByText ( 'fooErrorComponent' )
340340 expect ( indexElement ) . toBeInTheDocument ( )
341341} )
342+
343+ test ( 'reproducer #4245' , async ( ) => {
344+ const LOADER_WAIT_TIME = 500
345+ const rootRoute = createRootRoute ( { } )
346+
347+ const indexRoute = createRoute ( {
348+ getParentRoute : ( ) => rootRoute ,
349+ path : '/' ,
350+ loader : async ( ) => {
351+ await sleep ( LOADER_WAIT_TIME )
352+ return 'index'
353+ } ,
354+
355+ component : ( ) => {
356+ const data = indexRoute . useLoaderData ( )
357+ return (
358+ < div >
359+ < Link to = "/foo" data-testid = "link-to-foo" > foo</ Link >
360+ { data }
361+ </ div >
362+ )
363+ } ,
364+ } )
365+
366+ const fooRoute = createRoute ( {
367+ getParentRoute : ( ) => rootRoute ,
368+ path : '/foo' ,
369+ component : ( ) => < Link to = "/" data-testid = "link-to-index" > index</ Link > ,
370+ } )
371+
372+ const routeTree = rootRoute . addChildren ( [ indexRoute , fooRoute ] )
373+ const router = createRouter ( { routeTree } )
374+
375+ render ( < RouterProvider router = { router } /> )
376+ // We wait for the initial loader to complete
377+ await router . load ( )
378+ const fooLink = await screen . findByTestId ( 'link-to-foo' )
379+
380+ expect ( fooLink ) . toBeInTheDocument ( )
381+
382+ // We navigate to the foo route
383+ fireEvent . click ( fooLink )
384+
385+ // We immediately see the content of the foo route
386+ const indexLink = await screen . findByTestId ( 'link-to-index' )
387+ expect ( indexLink ) . toBeInTheDocument ( )
388+
389+ // We navigate to the index route
390+ fireEvent . click ( indexLink )
391+
392+ // We immediately see the content of the index route because the stale data is still available
393+ const fooLink2 = await screen . findByTestId ( 'link-to-foo' )
394+ expect ( fooLink2 ) . toBeInTheDocument ( )
395+
396+ // We navigate to the foo route again
397+ fireEvent . click ( fooLink2 )
398+
399+ // We immediately see the content of the foo route
400+ const indexLink2 = await screen . findByTestId ( 'link-to-index' )
401+ expect ( indexLink2 ) . toBeInTheDocument ( )
402+
403+ // We navigate to the index route again
404+ fireEvent . click ( indexLink2 )
405+
406+ // We now should see the content of the index route immediately because the stale data is still available
407+ const fooLink3 = await screen . findByTestId ( 'link-to-foo' )
408+ expect ( fooLink3 ) . toBeInTheDocument ( )
409+ } )
0 commit comments