Open History to see the last commit
1import {
2 ErrorComponent,
3 HeadContent,
4 Link,
5 Outlet,
6 RouterProvider,
7 createRootRouteWithContext,
8 createRoute,
9 createRouter,
10 useRouter,
11} from '@tanstack/solid-router'
12import {
13 QueryClient,
14 QueryClientProvider,
15 useQuery,
16} from '@tanstack/solid-query'
17import './styles.css'
18import { render } from 'solid-js/web'
19import { SolidQueryDevtools } from '@tanstack/solid-query-devtools'
20import { createEffect, createMemo } from 'solid-js'
21import { TanStackRouterDevtools } from '@tanstack/solid-router-devtools'
22import { NotFoundError, postQueryOptions, postsQueryOptions } from './posts'
23import type { ErrorComponentProps } from '@tanstack/solid-router'
24β
25const rootRoute = createRootRouteWithContext<{
26 queryClient: QueryClient
27}>()({
28 component: RootComponent,
29 notFoundComponent: () => {
30 return (
31 <div>
32 <p>This is the notFoundComponent configured on root route</p>
33 <Link to="/">Start Over</Link>
34 </div>
35 )
36 },
37})
38β
39function RootComponent() {
40 return (
41 <>
42 <HeadContent />
43 <div class="p-2 flex gap-2 text-lg">
44 <Link
45 to="/"
46 activeProps={{
47 class: 'font-bold',
48 }}
49 activeOptions={{ exact: true }}
50 >
51 Home
52 </Link>{' '}
53 <Link
54 to="/posts"
55 activeProps={{
56 class: 'font-bold',
57 }}
58 >
59 Posts
60 </Link>{' '}
61 <Link
62 to="/route-a"
63 activeProps={{
64 class: 'font-bold',
65 }}
66 >
67 Pathless Layout
68 </Link>{' '}
69 <Link
70 // @ts-expect-error
71 to="/this-route-does-not-exist"
72 activeProps={{
73 class: 'font-bold',
74 }}
75 >
76 This Route Does Not Exist
77 </Link>
78 </div>
79 <hr />
80 <Outlet />
81 <SolidQueryDevtools buttonPosition="top-right" />
82 <TanStackRouterDevtools position="bottom-right" />
83 </>
84 )
85}
86β
87const indexRoute = createRoute({
88 getParentRoute: () => rootRoute,
89 path: '/',
90 component: IndexRouteComponent,
91})
92β
93function IndexRouteComponent() {
94 return (
95 <div class="p-2">
96 <h3>Welcome Home!</h3>
97 </div>
98 )
99}
100β
101const postsLayoutRoute = createRoute({
102 getParentRoute: () => rootRoute,
103 path: 'posts',
104 loader: ({ context: { queryClient } }) =>
105 queryClient.ensureQueryData(postsQueryOptions),
106}).lazy(() => import('./posts.lazy').then((d) => d.Route))
107β
108const postsIndexRoute = createRoute({
109 getParentRoute: () => postsLayoutRoute,
110 path: '/',
111 component: PostsIndexRouteComponent,
112})
113β
114function PostsIndexRouteComponent() {
115 return <div>Select a post.</div>
116}
117β
118const postRoute = createRoute({
119 getParentRoute: () => postsLayoutRoute,
120 path: '$postId',
121 errorComponent: PostErrorComponent,
122 loader: ({ context: { queryClient }, params: { postId } }) =>
123 queryClient.ensureQueryData(postQueryOptions(postId)),
124 component: PostRouteComponent,
125})
126β
127function PostErrorComponent({ error, reset }: ErrorComponentProps) {
128 const router = useRouter()
129 if (error instanceof NotFoundError) {
130 return <div>{error.message}</div>
131 }
132β
133 createEffect(() => {
134 reset()
135 queryClient.resetQueries()
136 })
137β
138 return (
139 <div>
140 <button
141 onClick={() => {
142 router.invalidate()
143 }}
144 >
145 retry
146 </button>
147 <ErrorComponent error={error} />
148 </div>
149 )
150}
151β
152function PostRouteComponent() {
153 const params = postRoute.useParams()
154 const postQuery = useQuery(() => postQueryOptions(params().postId))
155 const post = createMemo(() => postQuery.data)
156β
157 return (
158 <div class="space-y-2">
159 <h4 class="text-xl font-bold underline">{post()?.title}</h4>
160 <div class="text-sm">{post()?.body}</div>
161 </div>
162 )
163}
164β
165const pathlessLayoutRoute = createRoute({
166 getParentRoute: () => rootRoute,
167 id: '_pathlessLayout',
168 component: PathlessLayoutComponent,
169})
170β
171function PathlessLayoutComponent() {
172 return (
173 <div class="p-2">
174 <div class="border-b">I'm a pathless layout</div>
175 <div>
176 <Outlet />
177 </div>
178 </div>
179 )
180}
181β
182const nestedPathlessLayoutRoute = createRoute({
183 getParentRoute: () => pathlessLayoutRoute,
184 id: '_nestedPathlessLayout',
185 component: NestedPathlessLayoutComponent,
186})
187β
188function NestedPathlessLayoutComponent() {
189 return (
190 <div>
191 <div>I'm a nested pathless layout</div>
192 <div class="flex gap-2 border-b">
193 <Link
194 to="/route-a"
195 activeProps={{
196 class: 'font-bold',
197 }}
198 >
199 Go to route A
200 </Link>
201 <Link
202 to="/route-b"
203 activeProps={{
204 class: 'font-bold',
205 }}
206 >
207 Go to route B
208 </Link>
209 </div>
210 <div>
211 <Outlet />
212 </div>
213 </div>
214 )
215}
216β
217const pathlessLayoutARoute = createRoute({
218 getParentRoute: () => nestedPathlessLayoutRoute,
219 path: '/route-a',
220 component: PathlessLayoutAComponent,
221})
222β
223function PathlessLayoutAComponent() {
224 return <div>I'm A!</div>
225}
226β
227const pathlessLayoutBRoute = createRoute({
228 getParentRoute: () => nestedPathlessLayoutRoute,
229 path: '/route-b',
230 component: PathlessLayoutBComponent,
231})
232β
233function PathlessLayoutBComponent() {
234 return <div>I'm B!</div>
235}
236β
237const routeTree = rootRoute.addChildren([
238 postsLayoutRoute.addChildren([postRoute, postsIndexRoute]),
239 pathlessLayoutRoute.addChildren([
240 nestedPathlessLayoutRoute.addChildren([
241 pathlessLayoutARoute,
242 pathlessLayoutBRoute,
243 ]),
244 ]),
245 indexRoute,
246])
247β
248const queryClient = new QueryClient()
249β
250// Set up a Router instance
251const router = createRouter({
252 routeTree,
253 defaultPreload: 'intent',
254 // Since we're using React Query, we don't want loader calls to ever be stale
255 // This will ensure that the loader is always called when the route is preloaded or visited
256 defaultPreloadStaleTime: 0,
257 scrollRestoration: true,
258 context: {
259 queryClient,
260 },
261})
262β
263// Register things for typesafety
264declare module '@tanstack/solid-router' {
265 interface Register {
266 router: typeof router
267 }
268}
269β
270const rootElement = document.getElementById('app')!
271β
272if (!rootElement.innerHTML) {
273 render(
274 () => (
275 <QueryClientProvider client={queryClient}>
276 <RouterProvider router={router} />
277 </QueryClientProvider>
278 ),
279 rootElement,
280 )
281}