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

__root.tsx

140 lines3.2 KB

1import {
2 Link,
3 Outlet,
4 ScrollRestoration,
5 createRootRoute,
6} from '@tanstack/react-router'
7import { TanStackRouterDevtools } from '@tanstack/router-devtools'
8import { Meta, Scripts, createServerFn } from '@tanstack/start'
9import * as React from 'react'
10import { DefaultCatchBoundary } from '~/components/DefaultCatchBoundary.js'
11import { NotFound } from '~/components/NotFound.js'
12import appCss from '~/styles/app.css?url'
13import { seo } from '~/utils/seo.js'
14import { useAppSession } from '~/utils/session.js'
15​
16const fetchUser = createServerFn({ method: 'GET' }).handler(async () => {
17 // We need to auth on the server so we have access to secure cookies
18 const session = await useAppSession()
19​
20 if (!session.data.userEmail) {
21 return null
22 }
23​
24 return {
25 email: session.data.userEmail,
26 }
27})
28​
29export const Route = createRootRoute({
30 head: () => ({
31 meta: [
32 {
33 charSet: 'utf-8',
34 },
35 {
36 name: 'viewport',
37 content: 'width=device-width, initial-scale=1',
38 },
39 ...seo({
40 title:
41 'TanStack Start | Type-Safe, Client-First, Full-Stack React Framework',
42 description: `TanStack Start is a type-safe, client-first, full-stack React framework. `,
43 }),
44 ],
45 links: [
46 { rel: 'stylesheet', href: appCss },
47 {
48 rel: 'apple-touch-icon',
49 sizes: '180x180',
50 href: '/apple-touch-icon.png',
51 },
52 {
53 rel: 'icon',
54 type: 'image/png',
55 sizes: '32x32',
56 href: '/favicon-32x32.png',
57 },
58 {
59 rel: 'icon',
60 type: 'image/png',
61 sizes: '16x16',
62 href: '/favicon-16x16.png',
63 },
64 { rel: 'manifest', href: '/site.webmanifest', color: '#fffff' },
65 { rel: 'icon', href: '/favicon.ico' },
66 ],
67 }),
68 beforeLoad: async () => {
69 const user = await fetchUser()
70​
71 return {
72 user,
73 }
74 },
75 errorComponent: (props) => {
76 return (
77 <RootDocument>
78 <DefaultCatchBoundary {...props} />
79 </RootDocument>
80 )
81 },
82 notFoundComponent: () => <NotFound />,
83 component: RootComponent,
84})
85​
86function RootComponent() {
87 return (
88 <RootDocument>
89 <Outlet />
90 </RootDocument>
91 )
93​
94function RootDocument({ children }: { children: React.ReactNode }) {
95 const { user } = Route.useRouteContext()
96​
97 return (
98 <html>
99 <head>
100 <Meta />
101 </head>
102 <body>
103 <div className="p-2 flex gap-2 text-lg">
104 <Link
105 to="/"
106 activeProps={{
107 className: 'font-bold',
108 }}
109 activeOptions={{ exact: true }}
110 >
111 Home
112 </Link>{' '}
113 <Link
114 to="/posts"
115 activeProps={{
116 className: 'font-bold',
117 }}
118 >
119 Posts
120 </Link>
121 <div className="ml-auto">
122 {user ? (
123 <>
124 <span className="mr-2">{user.email}</span>
125 <Link to="/logout">Logout</Link>
126 </>
127 ) : (
128 <Link to="/login">Login</Link>
129 )}
130 </div>
131 </div>
132 <hr />
133 {children}
134 <ScrollRestoration />
135 <TanStackRouterDevtools position="bottom-right" />
136 <Scripts />
137 </body>
138 </html>
139 )