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