Skip to content

Commit 9b71538

Browse files
committed
fix(solid-router): properly load components on lazy routes
1 parent 334ed7e commit 9b71538

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

packages/solid-router/src/lazyRouteComponent.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Dynamic, isServer } from 'solid-js/web'
2+
import { createResource } from 'solid-js'
23
import { Outlet } from './Match'
34
import type * as Solid from 'solid-js'
45
import type { AsyncRouteComponent } from './route'
@@ -42,13 +43,14 @@ export function lazyRouteComponent<
4243
const load = () => {
4344
if (typeof document === 'undefined' && ssr?.() === false) {
4445
comp = (() => null) as any
45-
return Promise.resolve()
46+
return Promise.resolve(comp)
4647
}
4748
if (!loadPromise) {
4849
loadPromise = importer()
4950
.then((res) => {
5051
loadPromise = undefined
5152
comp = res[exportName ?? 'default']
53+
return comp
5254
})
5355
.catch((err) => {
5456
error = err
@@ -94,18 +96,19 @@ export function lazyRouteComponent<
9496
throw error
9597
}
9698

97-
if (!comp) {
98-
throw load()
99-
}
99+
const [compResource] = createResource(load, {
100+
initialValue: comp,
101+
ssrLoadFrom: 'initial',
102+
})
100103

101104
if (ssr?.() === false) {
102105
return (
103106
<ClientOnly fallback={<Outlet />}>
104-
<Dynamic component={comp} {...props} />
107+
<Dynamic component={compResource()} {...props} />
105108
</ClientOnly>
106109
)
107110
}
108-
return <Dynamic component={comp} {...props} />
111+
return <Dynamic component={compResource()} {...props} />
109112
}
110113

111114
;(lazyComp as any).preload = load

0 commit comments

Comments
 (0)