1+ import { createRootRoute } from '@tanstack/solid-router' ;
2+ const TSRComponent = ( ) => < div > root hmr</ div > ;
3+ export const Route = createRootRoute ( {
4+ component : TSRComponent
5+ } ) ;
6+ if ( import . meta. hot ) {
7+ const hot = import . meta. hot ;
8+ const hotData = hot . data ??= { } ;
9+ hot . accept ( newModule => {
10+ if ( Route && newModule && newModule . Route ) {
11+ const routeId = hotData [ 'tsr-route-id' ] ?? Route . id ;
12+ if ( routeId ) {
13+ hotData [ 'tsr-route-id' ] = routeId ;
14+ }
15+ ( function handleRouteUpdate ( routeId , newRoute ) {
16+ const router = window . __TSR_ROUTER__ ;
17+ const oldRoute = router . routesById [ routeId ] ;
18+ if ( ! oldRoute ) {
19+ return ;
20+ }
21+ ;
22+ const removedKeys = new Set ( ) ;
23+ Object . keys ( oldRoute . options ) . forEach ( key => {
24+ if ( ! ( key in newRoute . options ) ) {
25+ removedKeys . add ( key ) ;
26+ delete oldRoute . options [ key ] ;
27+ }
28+ } ) ;
29+ const oldHasShellComponent = "shellComponent" in oldRoute . options ;
30+ const newHasShellComponent = "shellComponent" in newRoute . options ;
31+ const preserveComponentIdentity = oldHasShellComponent === newHasShellComponent ;
32+ const componentKeys = [ "component" , "shellComponent" , "pendingComponent" , "errorComponent" , "notFoundComponent" ] ;
33+ if ( preserveComponentIdentity ) {
34+ componentKeys . forEach ( key => {
35+ if ( key in oldRoute . options && key in newRoute . options ) {
36+ newRoute . options [ key ] = oldRoute . options [ key ] ;
37+ }
38+ } ) ;
39+ }
40+ ;
41+ oldRoute . options = newRoute . options ;
42+ oldRoute . update ( newRoute . options ) ;
43+ oldRoute . _componentsPromise = undefined ;
44+ oldRoute . _lazyPromise = undefined ;
45+ router . routesById [ oldRoute . id ] = oldRoute ;
46+ router . routesByPath [ oldRoute . fullPath ] = oldRoute ;
47+ router . processedTree . matchCache . clear ( ) ;
48+ router . processedTree . flatCache ?. clear ( ) ;
49+ router . processedTree . singleCache . clear ( ) ;
50+ router . resolvePathCache . clear ( ) ;
51+ walkReplaceSegmentTree ( oldRoute , router . processedTree . segmentTree ) ;
52+ const filter = m => m . routeId === oldRoute . id ;
53+ const activeMatch = router . stores . matches . get ( ) . find ( filter ) ;
54+ const pendingMatch = router . stores . pendingMatches . get ( ) . find ( filter ) ;
55+ const cachedMatches = router . stores . cachedMatches . get ( ) . filter ( filter ) ;
56+ if ( activeMatch || pendingMatch || cachedMatches . length > 0 ) {
57+ if ( removedKeys . has ( "loader" ) || removedKeys . has ( "beforeLoad" ) ) {
58+ const matchIds = [ activeMatch ?. id , pendingMatch ?. id , ...cachedMatches . map ( match => match . id ) ] . filter ( Boolean ) ;
59+ router . batch ( ( ) => {
60+ for ( const matchId of matchIds ) {
61+ const store = router . stores . pendingMatchStores . get ( matchId ) || router . stores . matchStores . get ( matchId ) || router . stores . cachedMatchStores . get ( matchId ) ;
62+ if ( store ) {
63+ store . set ( prev => {
64+ const next = {
65+ ...prev
66+ } ;
67+ if ( removedKeys . has ( "loader" ) ) {
68+ next . loaderData = undefined ;
69+ }
70+ ;
71+ if ( removedKeys . has ( "beforeLoad" ) ) {
72+ next . __beforeLoadContext = undefined ;
73+ next . context = rebuildMatchContextWithoutBeforeLoad ( next ) ;
74+ }
75+ ;
76+ return next ;
77+ } ) ;
78+ }
79+ }
80+ } ) ;
81+ }
82+ ;
83+ router . invalidate ( {
84+ filter,
85+ sync : true
86+ } ) ;
87+ }
88+ ;
89+ function walkReplaceSegmentTree ( route , node ) {
90+ if ( node . route ?. id === route . id ) node . route = route ;
91+ if ( node . index ) walkReplaceSegmentTree ( route , node . index ) ;
92+ node . static ?. forEach ( child => walkReplaceSegmentTree ( route , child ) ) ;
93+ node . staticInsensitive ?. forEach ( child => walkReplaceSegmentTree ( route , child ) ) ;
94+ node . dynamic ?. forEach ( child => walkReplaceSegmentTree ( route , child ) ) ;
95+ node . optional ?. forEach ( child => walkReplaceSegmentTree ( route , child ) ) ;
96+ node . wildcard ?. forEach ( child => walkReplaceSegmentTree ( route , child ) ) ;
97+ }
98+ function getStoreMatch ( matchId ) {
99+ return router . stores . pendingMatchStores . get ( matchId ) ?. get ( ) || router . stores . matchStores . get ( matchId ) ?. get ( ) || router . stores . cachedMatchStores . get ( matchId ) ?. get ( ) ;
100+ }
101+ function getMatchList ( matchId ) {
102+ const pendingMatches = router . stores . pendingMatches . get ( ) ;
103+ if ( pendingMatches . some ( match => match . id === matchId ) ) {
104+ return pendingMatches ;
105+ }
106+ ;
107+ const activeMatches = router . stores . matches . get ( ) ;
108+ if ( activeMatches . some ( match => match . id === matchId ) ) {
109+ return activeMatches ;
110+ }
111+ ;
112+ const cachedMatches = router . stores . cachedMatches . get ( ) ;
113+ if ( cachedMatches . some ( match => match . id === matchId ) ) {
114+ return cachedMatches ;
115+ }
116+ ;
117+ return [ ] ;
118+ }
119+ function getParentMatch ( match ) {
120+ const matchList = getMatchList ( match . id ) ;
121+ const matchIndex = matchList . findIndex ( item => item . id === match . id ) ;
122+ if ( matchIndex <= 0 ) {
123+ return undefined ;
124+ }
125+ ;
126+ const parentMatch = matchList [ matchIndex - 1 ] ;
127+ return getStoreMatch ( parentMatch . id ) || parentMatch ;
128+ }
129+ function rebuildMatchContextWithoutBeforeLoad ( match ) {
130+ const parentMatch = getParentMatch ( match ) ;
131+ const getParentContext = router . getParentContext ;
132+ const parentContext = getParentContext ? getParentContext . call ( router , parentMatch ) : parentMatch ?. context ?? router . options . context ;
133+ return {
134+ ...( parentContext ?? { } ) ,
135+ ...( match . __routeContext ?? { } )
136+ } ;
137+ }
138+ } ) ( routeId , newModule . Route ) ;
139+ }
140+ } ) ;
141+ }
0 commit comments