Open History to see the last commit
1import { render } from 'solid-js/web'
2import {
3 ErrorComponent,
4 HeadContent,
5 Link,
6 Outlet,
7 RouterProvider,
8 createRootRoute,
9 createRoute,
10 createRouter,
11 redirect,
12} from '@tanstack/solid-router'
13import { TanStackRouterDevtools } from '@tanstack/solid-router-devtools'
14import { NotFoundError, fetchPost, fetchPosts } from './posts'
15import './styles.css'
16import type { ErrorComponentProps } from '@tanstack/solid-router'
17β
18const rootRoute = createRootRoute({
19 component: RootComponent,
20 notFoundComponent: () => {
21 return (
22 <div>
23 <p>This is the notFoundComponent configured on root route</p>
24 <Link to="/">Start Over</Link>
25 </div>
26 )
27 },
28})
29β
30function RootComponent() {
31 return (
32 <>
33 <HeadContent />
34 <div class="p-2 flex gap-2 text-lg border-b">
35 <Link
36 to="/"
37 activeProps={{
38 class: 'font-bold',
39 }}
40 activeOptions={{ exact: true }}
41 >
42 Home
43 </Link>{' '}
44 <Link
45 to="/posts"
46 activeProps={{
47 class: 'font-bold',
48 }}
49 >
50 Posts
51 </Link>{' '}
52 <Link to="/posts" viewTransition>
53 View Transition
54 </Link>{' '}
55 <Link to="/posts" viewTransition={{ types: ['dummy'] }}>
56 View Transition types
57 </Link>{' '}
58 <Link
59 to="/layout-a"
60 activeProps={{
61 class: 'font-bold',
62 }}
63 >
64 Layout
65 </Link>{' '}
66 <Link
67 // @ts-expect-error
68 to="/this-route-does-not-exist"
69 activeProps={{
70 class: 'font-bold',
71 }}
72 >
73 This Route Does Not Exist
74 </Link>
75 </div>
76 <Outlet />
77 <TanStackRouterDevtools position="bottom-right" />
78 </>
79 )
80}
81const indexRoute = createRoute({
82 getParentRoute: () => rootRoute,
83 path: '/',
84 component: IndexComponent,
85})
86β
87function IndexComponent() {
88 return (
89 <div class="p-2">
90 <h3>Welcome Home!</h3>
91 </div>
92 )
93}
94β
95export const postsRoute = createRoute({
96 getParentRoute: () => rootRoute,
97 path: 'posts',
98 loader: () => fetchPosts(),
99}).lazy(() => import('./posts.lazy').then((d) => d.Route))
100β
101const postsIndexRoute = createRoute({
102 getParentRoute: () => postsRoute,
103 path: '/',
104 component: PostsIndexComponent,
105})
106β
107function PostsIndexComponent() {
108 return <div>Select a post.</div>
109}
110β
111function PostErrorComponent({ error }: ErrorComponentProps) {
112 if (error instanceof NotFoundError) {
113 return <div>{error.message}</div>
114 }
115β
116 return <ErrorComponent error={error} />
117}
118β
119const postRoute = createRoute({
120 getParentRoute: () => postsRoute,
121 path: '$postId',
122 errorComponent: PostErrorComponent,
123 loader: ({ params }) => fetchPost(params.postId),
124 component: PostComponent,
125})
126β
127function PostComponent() {
128 const post = postRoute.useLoaderData()
129β
130 return (
131 <div class="space-y-2">
132 <h4 class="text-xl font-bold">{post().title}</h4>
133 <hr class="opacity-20" />
134 <div class="text-sm">{post().body}</div>
135 </div>
136 )
137}
138β
139const layoutRoute = createRoute({
140 getParentRoute: () => rootRoute,
141 id: '_layout',
142 component: LayoutComponent,
143})
144β
145function LayoutComponent() {
146 return (
147 <div class="p-2">
148 <div class="border-b">I'm a layout</div>
149 <div>
150 <Outlet />
151 </div>
152 </div>
153 )
154}
155β
156const layout2Route = createRoute({
157 getParentRoute: () => layoutRoute,
158 id: '_layout-2',
159 component: Layout2Component,
160})
161β
162function Layout2Component() {
163 return (
164 <div>
165 <div>I'm a nested layout</div>
166 <div class="flex gap-2 border-b">
167 <Link
168 to="/layout-a"
169 activeProps={{
170 class: 'font-bold',
171 }}
172 >
173 Layout A
174 </Link>
175 <Link
176 to="/layout-b"
177 activeProps={{
178 class: 'font-bold',
179 }}
180 >
181 Layout B
182 </Link>
183 </div>
184 <div>
185 <Outlet />
186 </div>
187 </div>
188 )
189}
190β
191const layoutARoute = createRoute({
192 getParentRoute: () => layout2Route,
193 path: '/layout-a',
194 component: LayoutAComponent,
195})
196β
197function LayoutAComponent() {
198 return <div>I'm layout A!</div>
199}
200β
201const layoutBRoute = createRoute({
202 getParentRoute: () => layout2Route,
203 path: '/layout-b',
204 component: LayoutBComponent,
205})
206β
207function LayoutBComponent() {
208 return <div>I'm layout B!</div>
209}
210β
211const paramsPsRoute = createRoute({
212 getParentRoute: () => rootRoute,
213 path: '/params-ps',
214})
215β
216const paramsPsIndexRoute = createRoute({
217 getParentRoute: () => paramsPsRoute,
218 path: '/',
219 component: function ParamsIndex() {
220 return (
221 <div>
222 <h3 class="pb-2">Named path params</h3>
223 <ul class="grid mb-2">
224 <li>
225 <Link
226 data-testid="l-to-named-foo"
227 to="/params-ps/named/$foo"
228 params={{ foo: 'foo' }}
229 >
230 /params-ps/named/$foo
231 </Link>
232 </li>
233 <li>
234 <Link
235 data-testid="l-to-named-prefixfoo"
236 to="/params-ps/named/prefix{$foo}"
237 params={{ foo: 'foo' }}
238 >
239 /params-ps/named/{'prefix{$foo}'}
240 </Link>
241 </li>
242 <li>
243 <Link
244 data-testid="l-to-named-foosuffix"
245 to="/params-ps/named/{$foo}suffix"
246 params={{ foo: 'foo' }}
247 >
248 /params-ps/named/{'{$foo}suffix'}
249 </Link>
250 </li>
251 </ul>
252 <hr />
253 <h3 class="pb-2">Wildcard path params</h3>
254 <ul class="grid mb-2">
255 <li>
256 <Link
257 data-testid="l-to-wildcard-foo"
258 to="/params-ps/wildcard/$"
259 params={{ _splat: 'foo' }}
260 >
261 /params-ps/wildcard/$
262 </Link>
263 </li>
264 <li>
265 <Link
266 data-testid="l-to-wildcard-prefixfoo"
267 to="/params-ps/wildcard/prefix{$}"
268 params={{ _splat: 'foo' }}
269 >
270 /params-ps/wildcard/{'prefix{$}'}
271 </Link>
272 </li>
273 <li>
274 <Link
275 data-testid="l-to-wildcard-foosuffix"
276 to="/params-ps/wildcard/{$}suffix"
277 params={{ _splat: 'foo' }}
278 >
279 /params-ps/wildcard/{'{$}suffix'}
280 </Link>
281 </li>
282 </ul>
283 </div>
284 )
285 },
286})
287β
288const paramsPsNamedRoute = createRoute({
289 getParentRoute: () => paramsPsRoute,
290 path: '/named',
291})
292β
293const paramsPsNamedIndexRoute = createRoute({
294 getParentRoute: () => paramsPsNamedRoute,
295 path: '/',
296 beforeLoad: () => {
297 throw redirect({ to: '/params-ps' })
298 },
299})
300β
301const paramsPsNamedFooRoute = createRoute({
302 getParentRoute: () => paramsPsNamedRoute,
303 path: '/$foo',
304 component: function ParamsNamedFoo() {
305 const p = paramsPsNamedFooRoute.useParams()
306 return (
307 <div>
308 <h3>ParamsNamedFoo</h3>
309 <div data-testid="params-output">{JSON.stringify(p())}</div>
310 </div>
311 )
312 },
313})
314β
315const paramsPsNamedFooPrefixRoute = createRoute({
316 getParentRoute: () => paramsPsNamedRoute,
317 path: '/prefix{$foo}',
318 component: function ParamsNamedFooMarkdown() {
319 const p = paramsPsNamedFooPrefixRoute.useParams()
320 return (
321 <div>
322 <h3>ParamsNamedFooPrefix</h3>
323 <div data-testid="params-output">{JSON.stringify(p())}</div>
324 </div>
325 )
326 },
327})
328β
329const paramsPsNamedFooSuffixRoute = createRoute({
330 getParentRoute: () => paramsPsNamedRoute,
331 path: '/{$foo}suffix',
332 component: function ParamsNamedFooSuffix() {
333 const p = paramsPsNamedFooSuffixRoute.useParams()
334 return (
335 <div>
336 <h3>ParamsNamedFooSuffix</h3>
337 <div data-testid="params-output">{JSON.stringify(p())}</div>
338 </div>
339 )
340 },
341})
342β
343const paramsPsWildcardRoute = createRoute({
344 getParentRoute: () => paramsPsRoute,
345 path: '/wildcard',
346})
347β
348const paramsPsWildcardIndexRoute = createRoute({
349 getParentRoute: () => paramsPsWildcardRoute,
350 path: '/',
351 beforeLoad: () => {
352 throw redirect({ to: '/params-ps' })
353 },
354})
355β
356const paramsPsWildcardSplatRoute = createRoute({
357 getParentRoute: () => paramsPsWildcardRoute,
358 path: '$',
359 component: function ParamsWildcardSplat() {
360 const p = paramsPsWildcardSplatRoute.useParams()
361 return (
362 <div>
363 <h3>ParamsWildcardSplat</h3>
364 <div data-testid="params-output">{JSON.stringify(p())}</div>
365 </div>
366 )
367 },
368})
369β
370const paramsPsWildcardSplatPrefixRoute = createRoute({
371 getParentRoute: () => paramsPsWildcardRoute,
372 path: 'prefix{$}',
373 component: function ParamsWildcardSplatPrefix() {
374 const p = paramsPsWildcardSplatPrefixRoute.useParams()
375 return (
376 <div>
377 <h3>ParamsWildcardSplatPrefix</h3>
378 <div data-testid="params-output">{JSON.stringify(p())}</div>
379 </div>
380 )
381 },
382})
383β
384const paramsPsWildcardSplatSuffixRoute = createRoute({
385 getParentRoute: () => paramsPsWildcardRoute,
386 path: '{$}suffix',
387 component: function ParamsWildcardSplatSuffix() {
388 const p = paramsPsWildcardSplatSuffixRoute.useParams()
389 return (
390 <div>
391 <h3>ParamsWildcardSplatSuffix</h3>
392 <div data-testid="params-output">{JSON.stringify(p())}</div>
393 </div>
394 )
395 },
396})
397β
398const routeTree = rootRoute.addChildren([
399 postsRoute.addChildren([postRoute, postsIndexRoute]),
400 layoutRoute.addChildren([
401 layout2Route.addChildren([layoutARoute, layoutBRoute]),
402 ]),
403 paramsPsRoute.addChildren([
404 paramsPsNamedRoute.addChildren([
405 paramsPsNamedFooPrefixRoute,
406 paramsPsNamedFooSuffixRoute,
407 paramsPsNamedFooRoute,
408 paramsPsNamedIndexRoute,
409 ]),
410 paramsPsWildcardRoute.addChildren([
411 paramsPsWildcardSplatRoute,
412 paramsPsWildcardSplatPrefixRoute,
413 paramsPsWildcardSplatSuffixRoute,
414 paramsPsWildcardIndexRoute,
415 ]),
416 paramsPsIndexRoute,
417 ]),
418 indexRoute,
419])
420β
421// Set up a Router instance
422const router = createRouter({
423 routeTree,
424 defaultPreload: 'intent',
425 defaultStaleTime: 5000,
426 scrollRestoration: true,
427})
428β
429// Register things for typesafety
430declare module '@tanstack/solid-router' {
431 interface Register {
432 router: typeof router
433 }
434}
435β
436const rootElement = document.getElementById('app')!
437β
438if (!rootElement.innerHTML) {
439 render(() => <RouterProvider router={router} />, rootElement)
440}