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

__root.tsx

146 lines3.5 KB

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