Open History to see the last commit
1import * as Solid from 'solid-js'
2import {
3 getLocationChangeInfo,
4 handleHashScroll,
5 trimPathRight,
6} from '@tanstack/router-core'
7import { useRouter } from './useRouter'
8import { useRouterState } from './useRouterState'
9import { usePrevious } from './utils'
10β
11export function Transitioner() {
12 const router = useRouter()
13 let mountLoadForRouter = { router, mounted: false }
14 const isLoading = useRouterState({
15 select: ({ isLoading }) => isLoading,
16 })
17β
18 if (router.isServer) {
19 return null
20 }
21β
22 const [isTransitioning, setIsTransitioning] = Solid.createSignal(false)
23β
24 // Track pending state changes
25 const hasPendingMatches = useRouterState({
26 select: (s) => s.matches.some((d) => d.status === 'pending'),
27 })
28β
29 const previousIsLoading = usePrevious(isLoading)
30β
31 const isAnyPending = () =>
32 isLoading() || isTransitioning() || hasPendingMatches()
33 const previousIsAnyPending = usePrevious(isAnyPending)
34β
35 const isPagePending = () => isLoading() || hasPendingMatches()
36 const previousIsPagePending = usePrevious(isPagePending)
37β
38 router.startTransition = (fn: () => void | Promise<void>) => {
39 setIsTransitioning(true)
40 Solid.startTransition(async () => {
41 try {
42 await fn()
43 } finally {
44 setIsTransitioning(false)
45 }
46 })
47 }
48β
49 // Subscribe to location changes
50 // and try to load the new location
51 Solid.onMount(() => {
52 const unsub = router.history.subscribe(router.load)
53β
54 const nextLocation = router.buildLocation({
55 to: router.latestLocation.pathname,
56 search: true,
57 params: true,
58 hash: true,
59 state: true,
60 _includeValidateSearch: true,
61 })
62β
63 if (
64 trimPathRight(router.latestLocation.href) !==
65 trimPathRight(nextLocation.href)
66 ) {
67 router.commitLocation({ ...nextLocation, replace: true })
68 }
69β
70 Solid.onCleanup(() => {
71 unsub()
72 })
73 })
74β
75 // Try to load the initial location
76 Solid.createRenderEffect(() => {
77 Solid.untrack(() => {
78 if (
79 // if we are hydrating from SSR, loading is triggered in ssr-client
80 (typeof window !== 'undefined' && router.ssr) ||
81 (mountLoadForRouter.router === router && mountLoadForRouter.mounted)
82 ) {
83 return
84 }
85 mountLoadForRouter = { router, mounted: true }
86 const tryLoad = async () => {
87 try {
88 await router.load()
89 } catch (err) {
90 console.error(err)
91 }
92 }
93 tryLoad()
94 })
95 })
96β
97 Solid.createRenderEffect(
98 Solid.on(
99 [previousIsLoading, isLoading],
100 ([previousIsLoading, isLoading]) => {
101 if (previousIsLoading.previous && !isLoading) {
102 router.emit({
103 type: 'onLoad',
104 ...getLocationChangeInfo(router.state),
105 })
106 }
107 },
108 ),
109 )
110β
111 Solid.createComputed(
112 Solid.on(
113 [isPagePending, previousIsPagePending],
114 ([isPagePending, previousIsPagePending]) => {
115 // emit onBeforeRouteMount
116 if (previousIsPagePending.previous && !isPagePending) {
117 router.emit({
118 type: 'onBeforeRouteMount',
119 ...getLocationChangeInfo(router.state),
120 })
121 }
122 },
123 ),
124 )
125β
126 Solid.createRenderEffect(
127 Solid.on(
128 [isAnyPending, previousIsAnyPending],
129 ([isAnyPending, previousIsAnyPending]) => {
130 // The router was pending and now it's not
131 if (previousIsAnyPending.previous && !isAnyPending) {
132 router.emit({
133 type: 'onResolved',
134 ...getLocationChangeInfo(router.state),
135 })
136β
137 router.__store.setState((s) => ({
138 ...s,
139 status: 'idle',
140 resolvedLocation: s.location,
141 }))
142β
143 handleHashScroll(router)
144 }
145 },
146 ),
147 )
148β
149 return null
150}