Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

__root.tsx

145 lines3.4 KB

1import {
2 Link,
3 Outlet,
4 ScrollRestoration,
5 createRootRouteWithContext,
6} from '@tanstack/react-router'
7import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
8import { TanStackRouterDevtools } from '@tanstack/router-devtools'
9import { Meta, Scripts } from '@tanstack/start'
10import * as React from 'react'
11import type { QueryClient } from '@tanstack/react-query'
12import { DefaultCatchBoundary } from '~/components/DefaultCatchBoundary'
13import { NotFound } from '~/components/NotFound'
14import appCss from '~/styles/app.css?url'
15import { seo } from '~/utils/seo'
16​
17export const Route = createRootRouteWithContext<{
18 queryClient: QueryClient
19}>()({
20 head: () => ({
21 meta: [
22 {
23 charSet: 'utf-8',
24 },
25 {
26 name: 'viewport',
27 content: 'width=device-width, initial-scale=1',
28 },
29 ...seo({
30 title:
31 'TanStack Start | Type-Safe, Client-First, Full-Stack React Framework',
32 description: `TanStack Start is a type-safe, client-first, full-stack React framework. `,
33 }),
34 ],
35 links: [
36 { rel: 'stylesheet', href: appCss },
37 {
38 rel: 'apple-touch-icon',
39 sizes: '180x180',
40 href: '/apple-touch-icon.png',
41 },
42 {
43 rel: 'icon',
44 type: 'image/png',
45 sizes: '32x32',
46 href: '/favicon-32x32.png',
47 },
48 {
49 rel: 'icon',
50 type: 'image/png',
51 sizes: '16x16',
52 href: '/favicon-16x16.png',
53 },
54 { rel: 'manifest', href: '/site.webmanifest', color: '#fffff' },
55 { rel: 'icon', href: '/favicon.ico' },
56 ],
57 }),
58 errorComponent: (props) => {
59 return (
60 <RootDocument>
61 <DefaultCatchBoundary {...props} />
62 </RootDocument>
63 )
64 },
65 notFoundComponent: () => <NotFound />,
66 component: RootComponent,
67})
68​
69function RootComponent() {
70 return (
71 <RootDocument>
72 <Outlet />
73 </RootDocument>
74 )
76​
77function RootDocument({ children }: { children: React.ReactNode }) {
78 return (
79 <html>
80 <head>
81 <Meta />
82 </head>
83 <body>
84 <div className="p-2 flex gap-2 text-lg">
85 <Link
86 to="/"
87 activeProps={{
88 className: 'font-bold',
89 }}
90 activeOptions={{ exact: true }}
91 >
92 Home
93 </Link>{' '}
94 <Link
95 to="/posts"
96 activeProps={{
97 className: 'font-bold',
98 }}
99 >
100 Posts
101 </Link>{' '}
102 <Link
103 to="/users"
104 activeProps={{
105 className: 'font-bold',
106 }}
107 >
108 Users
109 </Link>{' '}
110 <Link
111 to="/layout-a"
112 activeProps={{
113 className: 'font-bold',
114 }}
115 >
116 Layout
117 </Link>{' '}
118 <Link
119 to="/deferred"
120 activeProps={{
121 className: 'font-bold',
122 }}
123 >
124 Deferred
125 </Link>{' '}
126 <Link
127 // @ts-expect-error
128 to="/this-route-does-not-exist"
129 activeProps={{
130 className: 'font-bold',
131 }}
132 >
133 This Route Does Not Exist
134 </Link>
135 </div>
136 <hr />
137 {children}
138 <ScrollRestoration />
139 <TanStackRouterDevtools position="bottom-right" />
140 <ReactQueryDevtools buttonPosition="bottom-left" />
141 <Scripts />
142 </body>
143 </html>
144 )