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

__root.tsx

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