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

__root.tsx

238 lines5.7 KB

1/// <reference types="vite/client" />
2import * as React from 'react'
3import {
4 ClientOnly,
5 HeadContent,
6 Link,
7 Outlet,
8 Scripts,
9 createRootRoute,
10 useRouterState,
11} from '@tanstack/react-router'
12​
13import { DefaultCatchBoundary } from '~/components/DefaultCatchBoundary'
14import { NotFound } from '~/components/NotFound'
15import '~/styles/app.css'
16import { seo } from '~/utils/seo'
17​
18export const Route = createRootRoute({
19 head: () => ({
20 meta: [
21 {
22 charSet: 'utf-8',
23 },
24 {
25 name: 'viewport',
26 content: 'width=device-width, initial-scale=1',
27 },
28 ...seo({
29 title:
30 'TanStack Start | Type-Safe, Client-First, Full-Stack React Framework',
31 description: `TanStack Start is a type-safe, client-first, full-stack React framework. `,
32 }),
33 ],
34 links: [
35 {
36 rel: 'apple-touch-icon',
37 sizes: '180x180',
38 href: '/apple-touch-icon.png',
39 },
40 {
41 rel: 'icon',
42 type: 'image/png',
43 sizes: '32x32',
44 href: '/favicon-32x32.png',
45 },
46 {
47 rel: 'icon',
48 type: 'image/png',
49 sizes: '16x16',
50 href: '/favicon-16x16.png',
51 },
52 { rel: 'manifest', href: '/site.webmanifest', color: '#fffff' },
53 { rel: 'icon', href: '/favicon.ico' },
54 ],
55 styles: [
56 {
57 media: 'all and (min-width: 500px)',
58 children: `
59 .inline-div {
60 color: white;
61 background-color: gray;
62 max-width: 250px;
63 }`,
64 },
65 ],
66 }),
67 errorComponent: (props) => {
68 return (
69 <RootDocument>
70 <DefaultCatchBoundary {...props} />
71 </RootDocument>
72 )
73 },
74 notFoundComponent: () => <NotFound />,
75 component: RootComponent,
76})
77​
78function RootComponent() {
79 return (
80 <RootDocument>
81 <Outlet />
82 </RootDocument>
83 )
85​
86const RouterDevtools =
87 process.env.NODE_ENV === 'production'
88 ? () => null // Render nothing in production
89 : React.lazy(() =>
90 // Lazy load in development
91 import('@tanstack/react-router-devtools').then((res) => ({
92 default: res.TanStackRouterDevtools,
93 })),
94 )
95​
96function RootDocument({ children }: { children: React.ReactNode }) {
97 const { isLoading, status } = useRouterState({
98 select: (state) => ({ isLoading: state.isLoading, status: state.status }),
99 structuralSharing: true,
100 })
101​
102 return (
103 <html>
104 <head>
105 <HeadContent />
106 <script src="/head-script.js" />
107 <script src="/head-async-script.js" async={true} />
108 </head>
109 <body>
110 <div className="p-2 flex gap-2 text-lg">
111 <Link
112 to="/"
113 activeProps={{
114 className: 'font-bold',
115 }}
116 activeOptions={{ exact: true }}
117 >
118 Home
119 </Link>{' '}
120 <Link
121 to="/posts"
122 activeProps={{
123 className: 'font-bold',
124 }}
125 >
126 Posts
127 </Link>{' '}
128 <Link
129 to="/users"
130 activeProps={{
131 className: 'font-bold',
132 }}
133 >
134 Users
135 </Link>{' '}
136 <Link
137 to="/layout-a"
138 activeProps={{
139 className: 'font-bold',
140 }}
141 >
142 Layout
143 </Link>{' '}
144 <Link
145 to="/scripts"
146 activeProps={{
147 className: 'font-bold',
148 }}
149 >
150 Scripts
151 </Link>{' '}
152 <Link
153 to="/async-scripts"
154 activeProps={{
155 className: 'font-bold',
156 }}
157 >
158 Async Scripts
159 </Link>{' '}
160 <Link
161 to="/inline-scripts"
162 activeProps={{
163 className: 'font-bold',
164 }}
165 >
166 Inline Scripts
167 </Link>{' '}
168 <Link
169 to="/deferred"
170 activeProps={{
171 className: 'font-bold',
172 }}
173 >
174 Deferred
175 </Link>{' '}
176 <Link
177 to="/redirect"
178 activeProps={{
179 className: 'font-bold',
180 }}
181 >
182 redirect
183 </Link>{' '}
184 <Link
185 to="/not-found"
186 activeProps={{
187 className: 'font-bold',
188 }}
189 >
190 not-found
191 </Link>{' '}
192 <Link
193 to="/client-only"
194 activeProps={{
195 className: 'font-bold',
196 }}
197 >
198 Client Only
199 </Link>{' '}
200 <Link
201 to="/raw-stream"
202 activeProps={{
203 className: 'font-bold',
204 }}
205 >
206 Raw Stream
207 </Link>{' '}
208 <Link
209 // @ts-expect-error
210 to="/this-route-does-not-exist"
211 activeProps={{
212 className: 'font-bold',
213 }}
214 >
215 This Route Does Not Exist
216 </Link>
217 </div>
218 <hr />
219 <ClientOnly>
220 <div hidden>
221 <b data-testid="router-isLoading">{isLoading ? 'true' : 'false'}</b>
222 <b data-testid="router-status">{status}</b>
223 </div>
224 </ClientOnly>
225 {children}
226 <div className="inline-div">This is an inline styled div</div>
227 <React.Suspense fallback={null}>
228 <RouterDevtools position="bottom-right" />
229 </React.Suspense>
230 <script src="/before-scripts-script.js" />
231 <script src="/before-scripts-async-script.js" async={true} />
232 <Scripts />
233 <script src="/user-script.js" />
234 <script src="/async-user-script.js" async={true} />
235 </body>
236 </html>
237 )