Skip to content

Commit b86b061

Browse files
committed
fix(solid-router): enable route component HMR for Solid
1 parent 1285422 commit b86b061

23 files changed

Lines changed: 956 additions & 22 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/router-plugin': patch
3+
---
4+
5+
fix(solid-router): enable route component HMR for Solid

‎packages/router-plugin/src/core/code-splitter/plugins/framework-plugins.ts‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createReactRefreshIgnoredRouteExportsPlugin } from './react-refresh-ignored-route-exports'
2-
import { createReactRefreshRouteComponentsPlugin } from './react-refresh-route-components'
3-
import { createReactStableHmrSplitRouteComponentsPlugin } from './react-stable-hmr-split-route-components'
2+
import { createRefreshRouteComponentsPlugin } from './refresh-route-components'
3+
import { createStableHmrSplitRouteComponentsPlugin } from './stable-hmr-split-route-components'
44
import type { ReferenceRouteCompilerPlugin } from '../plugins'
55
import type { Config, HmrStyle } from '../../config'
66

@@ -9,19 +9,27 @@ export function getReferenceRouteCompilerPlugins(opts: {
99
addHmr?: boolean
1010
hmrStyle?: HmrStyle
1111
}): Array<ReferenceRouteCompilerPlugin> | undefined {
12+
if (!opts.addHmr) {
13+
return undefined
14+
}
15+
16+
const hmrStyle = opts.hmrStyle ?? 'vite'
17+
1218
switch (opts.targetFramework) {
1319
case 'react': {
14-
if (opts.addHmr) {
15-
const hmrStyle = opts.hmrStyle ?? 'vite'
16-
return [
17-
...(hmrStyle === 'vite'
18-
? [createReactRefreshIgnoredRouteExportsPlugin()]
19-
: []),
20-
createReactRefreshRouteComponentsPlugin(),
21-
createReactStableHmrSplitRouteComponentsPlugin({ hmrStyle }),
22-
]
23-
}
24-
return undefined
20+
return [
21+
...(hmrStyle === 'vite'
22+
? [createReactRefreshIgnoredRouteExportsPlugin()]
23+
: []),
24+
createRefreshRouteComponentsPlugin(),
25+
createStableHmrSplitRouteComponentsPlugin({ hmrStyle }),
26+
]
27+
}
28+
case 'solid': {
29+
return [
30+
createRefreshRouteComponentsPlugin(),
31+
createStableHmrSplitRouteComponentsPlugin({ hmrStyle }),
32+
]
2533
}
2634
default:
2735
return undefined

packages/router-plugin/src/core/code-splitter/plugins/react-refresh-route-components.ts renamed to packages/router-plugin/src/core/code-splitter/plugins/refresh-route-components.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '../../utils'
66
import type { ReferenceRouteCompilerPlugin } from '../plugins'
77

8-
const REACT_REFRESH_ROUTE_COMPONENT_IDENTS = new Set([
8+
const ROUTE_COMPONENT_OPTION_KEYS = new Set([
99
'component',
1010
'shellComponent',
1111
'pendingComponent',
@@ -27,7 +27,7 @@ function hoistInlineRouteComponents(ctx: {
2727

2828
const key = getObjectPropertyKeyName(prop)
2929

30-
if (!key || !REACT_REFRESH_ROUTE_COMPONENT_IDENTS.has(key)) {
30+
if (!key || !ROUTE_COMPONENT_OPTION_KEYS.has(key)) {
3131
return
3232
}
3333

@@ -60,11 +60,11 @@ function hoistInlineRouteComponents(ctx: {
6060
return true
6161
}
6262

63-
export function createReactRefreshRouteComponentsPlugin(): ReferenceRouteCompilerPlugin {
63+
export function createRefreshRouteComponentsPlugin(): ReferenceRouteCompilerPlugin {
6464
return {
65-
name: 'react-refresh-route-components',
65+
name: 'refresh-route-components',
6666
getStableRouteOptionKeys() {
67-
return [...REACT_REFRESH_ROUTE_COMPONENT_IDENTS]
67+
return [...ROUTE_COMPONENT_OPTION_KEYS]
6868
},
6969
onUnsplittableRoute(ctx) {
7070
if (!ctx.opts.addHmr) {

packages/router-plugin/src/core/code-splitter/plugins/react-stable-hmr-split-route-components.ts renamed to packages/router-plugin/src/core/code-splitter/plugins/stable-hmr-split-route-components.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ function hotExpressionAstFor(hmrStyle: HmrStyle): t.Expression {
3434
)
3535
}
3636

37-
export function createReactStableHmrSplitRouteComponentsPlugin(opts: {
37+
export function createStableHmrSplitRouteComponentsPlugin(opts: {
3838
hmrStyle: HmrStyle
3939
}): ReferenceRouteCompilerPlugin {
4040
return {
41-
name: 'react-stable-hmr-split-route-components',
41+
name: 'stable-hmr-split-route-components',
4242
onSplitRouteProperty(ctx) {
4343
if (ctx.splitNodeMeta.splitStrategy !== 'lazyRouteComponent') {
4444
return

‎packages/router-plugin/tests/add-hmr/snapshots/solid/[email protected]‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ const $$splitComponentImporter = () => import('arrow-function.tsx?tsr-split=comp
22
import { lazyRouteComponent } from '@tanstack/solid-router';
33
import { createFileRoute } from '@tanstack/solid-router';
44
import { fetchPosts } from '../posts';
5+
const TSRSplitComponent = (() => {
6+
const hot = import.meta.hot;
7+
const hotData = hot ? hot.data ??= {} : undefined;
8+
return hotData?.["tsr-split-component:component"] ?? lazyRouteComponent($$splitComponentImporter, "component");
9+
})();
10+
if (import.meta.hot) {
11+
(import.meta.hot.data ??= {})["tsr-split-component:component"] = TSRSplitComponent;
12+
}
513
export const Route = createFileRoute('/posts')({
614
loader: fetchPosts,
7-
component: lazyRouteComponent($$splitComponentImporter, 'component')
15+
component: TSRSplitComponent
816
});
917
if (import.meta.hot) {
1018
const hot = import.meta.hot;
@@ -32,7 +40,7 @@ if (import.meta.hot) {
3240
const oldHasShellComponent = "shellComponent" in oldRoute.options;
3341
const newHasShellComponent = "shellComponent" in newRoute.options;
3442
const preserveComponentIdentity = oldHasShellComponent === newHasShellComponent;
35-
const componentKeys = [];
43+
const componentKeys = ["component", "shellComponent", "pendingComponent", "errorComponent", "notFoundComponent"];
3644
if (preserveComponentIdentity) {
3745
componentKeys.forEach(key => {
3846
if (key in oldRoute.options && key in newRoute.options) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createRootRoute } from '@tanstack/solid-router'
2+
3+
export const Route = createRootRoute({
4+
component: () => <div>root hmr</div>,
5+
})
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import thing from 'thing'
2+
3+
export function test() {
4+
const {
5+
foo: {
6+
bar: { destructured },
7+
},
8+
} = thing
9+
10+
return destructured
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import thing from 'thing'
2+
3+
export function test() {
4+
const {
5+
foo: {
6+
bar: { destructured },
7+
},
8+
} = thing
9+
10+
return destructured
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createFileRoute } from '@tanstack/solid-router'
2+
3+
export const Route = createFileRoute('/')({
4+
component: undefined,
5+
})

0 commit comments

Comments
 (0)