Open History to see the last commit
1import { Dynamic } from 'solid-js/web'
2import { lazy } from 'solid-js'
3import { isModuleNotFoundError } from '@tanstack/router-core'
4import { isServer } from '@tanstack/router-core/isServer'
5import type { AsyncRouteComponent } from './route'
6β
7export function lazyRouteComponent<
8 T extends Record<string, any>,
9 TKey extends keyof T = 'default',
10>(
11 importer: () => Promise<T>,
12 exportName?: TKey,
13): T[TKey] extends (props: infer TProps) => any
14 ? AsyncRouteComponent<TProps>
15 : never {
16 let loadPromise: Promise<any> | undefined
17 let comp: T[TKey] | T['default']
18 let error: any
19β
20 const load = () => {
21 if (!loadPromise) {
22 error = undefined
23 loadPromise = importer()
24 .then((res) => {
25 // Resolved clients have no preload work; SSR can reuse the import.
26 if (!(isServer ?? typeof window === 'undefined')) {
27 loadPromise = undefined
28 ;(Lazy as any).preload = undefined
29 }
30 comp = res[exportName ?? 'default']
31 return comp
32 })
33 .catch((err) => {
34 loadPromise = undefined
35 error = err
36 })
37 }
38β
39 return loadPromise
40 }
41 function Lazy(props: any) {
42 if (comp || error) {
43 return render(props)
44 }
45β
46 return <Loadable {...props} />
47 }
48β
49 // Both preload and lazy resolution end here without starting another load.
50 function render(props: any) {
51 // Now that we're out of preload and into actual render path,
52 // throw the error if it was a module not found error during preload
53 if (error) {
54 // If the load fails due to module not found, it may mean a new version of
55 // the build was deployed and the user's browser is still using an old version.
56 // If this happens, the old version in the user's browser would have an outdated
57 // URL to the lazy module.
58 // In that case, we want to attempt one window refresh to get the latest.
59 if (isModuleNotFoundError(error)) {
60 // We don't want an error thrown from preload in this case, because
61 // there's nothing we want to do about module not found during preload.
62 // Record the error, recover the promise with a null return,
63 // and we will attempt module not found resolution during the render path.
64β
65 if (
66 error instanceof Error &&
67 typeof window !== 'undefined' &&
68 typeof sessionStorage !== 'undefined'
69 ) {
70 // Again, we want to reload one time on module not found error and not enter
71 // a reload loop if there is some other issue besides an old deploy.
72 // That's why we store our reload attempt in sessionStorage.
73 // Use error.message as key because it contains the module path that failed.
74 const storageKey = `tanstack_router_reload:${error.message}`
75 if (!sessionStorage.getItem(storageKey)) {
76 sessionStorage.setItem(storageKey, '1')
77 window.location.reload()
78β
79 // Return no content while we wait for window to reload.
80 return null
81 }
82 }
83 }
84β
85 // Otherwise, just throw the error
86 throw error
87 }
88β
89 return <Dynamic component={comp} {...props} />
90 }
91β
92 const Loadable = lazy(async () => {
93 await load()
94 if (process.env.NODE_ENV !== 'production' && !comp && !error) {
95 throw new Error(
96 `lazyRouteComponent: export "${String(exportName ?? 'default')}" not found`,
97 )
98 }
99 return { default: render }
100 })
101β
102 ;(Lazy as any).preload = load
103β
104 return Lazy as any
105}