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

__root.tsx

141 lines3.3 KB

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