@@ -4,6 +4,7 @@ import { BaseRootRoute, BaseRoute, notFound } from '../src'
44import { hydrate } from '../src/ssr/client'
55import { createTestRouter } from './routerTestUtils'
66import { dehydrateSsrMatchId } from '../src/ssr/ssr-match-id'
7+ import type { LocationRewrite } from '../src'
78import type { TsrSsrGlobal } from '../src/ssr/types'
89import type { AnyRouteMatch } from '../src'
910
@@ -395,6 +396,73 @@ describe('hydrate', () => {
395396 expect ( ( mockRouter . state . matches [ 0 ] as AnyRouteMatch ) . id ) . toBe ( '/' )
396397 } )
397398
399+ it ( 'should run custom hydration before matching routes' , async ( ) => {
400+ const history = createMemoryHistory ( { initialEntries : [ '/public' ] } )
401+
402+ const rootRoute = new BaseRootRoute ( { } )
403+ const internalRoute = new BaseRoute ( {
404+ getParentRoute : ( ) => rootRoute ,
405+ path : '/internal' ,
406+ component : ( ) => 'Internal' ,
407+ } )
408+
409+ const rewrite : LocationRewrite = {
410+ input : ( { url } ) => {
411+ if ( url . pathname === '/public' ) {
412+ url . pathname = '/internal'
413+ }
414+ return url
415+ } ,
416+ }
417+
418+ mockRouter = createTestRouter ( {
419+ routeTree : rootRoute . addChildren ( [ internalRoute ] ) ,
420+ history,
421+ isServer : true ,
422+ hydrate : ( dehydrated : { rewrite ?: boolean } ) => {
423+ if ( dehydrated . rewrite ) {
424+ mockRouter . update ( { rewrite } )
425+ }
426+ } ,
427+ } )
428+
429+ mockWindow . $_TSR = {
430+ router : {
431+ manifest : { routes : { } } ,
432+ dehydratedData : { rewrite : true } ,
433+ lastMatchId : '/internal/internal' ,
434+ matches : [
435+ {
436+ i : '__root__/' ,
437+ s : 'success' ,
438+ ssr : true ,
439+ u : Date . now ( ) ,
440+ } ,
441+ {
442+ i : '/internal/internal' ,
443+ s : 'success' ,
444+ ssr : true ,
445+ u : Date . now ( ) ,
446+ } ,
447+ ] ,
448+ } ,
449+ h : vi . fn ( ) ,
450+ e : vi . fn ( ) ,
451+ c : vi . fn ( ) ,
452+ p : vi . fn ( ) ,
453+ buffer : [ ] ,
454+ initialized : false ,
455+ }
456+
457+ await hydrate ( mockRouter )
458+
459+ expect ( mockRouter . state . location . pathname ) . toBe ( '/internal' )
460+ expect ( mockRouter . state . location . publicHref ) . toBe ( '/public' )
461+ expect (
462+ mockRouter . state . matches . map ( ( match : AnyRouteMatch ) => match . routeId ) ,
463+ ) . toEqual ( [ '__root__' , '/internal' ] )
464+ } )
465+
398466 it ( 'should handle errors during route context hydration' , async ( ) => {
399467 const consoleSpy = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
400468 mockHead . mockImplementation ( ( ) => {
0 commit comments