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

__root.tsx

135 lines2.9 KB

1/// <reference types="vite/client" />
2import {
3 ClerkProvider,
4 SignInButton,
5 SignedIn,
6 SignedOut,
7 UserButton,
8} from '@clerk/tanstack-react-start'
9import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
10import { createServerFn } from '@tanstack/react-start'
11import { auth } from '@clerk/tanstack-react-start/server'
12import * as React from 'react'
13import {
14 HeadContent,
15 Link,
16 Outlet,
17 Scripts,
18 createRootRoute,
19} from '@tanstack/react-router'
20import { DefaultCatchBoundary } from '~/components/DefaultCatchBoundary.js'
21import { NotFound } from '~/components/NotFound.js'
22import appCss from '~/styles/app.css?url'
23​
24const fetchClerkAuth = createServerFn({ method: 'GET' }).handler(async () => {
25 const { userId } = await auth()
26​
27 return {
28 userId,
29 }
30})
31​
32export const Route = createRootRoute({
33 beforeLoad: async () => {
34 const { userId } = await fetchClerkAuth()
35​
36 return {
37 userId,
38 }
39 },
40 head: () => ({
41 meta: [
42 {
43 charSet: 'utf-8',
44 },
45 {
46 name: 'viewport',
47 content: 'width=device-width, initial-scale=1',
48 },
49 ],
50 links: [
51 { rel: 'stylesheet', href: appCss },
52 {
53 rel: 'apple-touch-icon',
54 sizes: '180x180',
55 href: '/apple-touch-icon.png',
56 },
57 {
58 rel: 'icon',
59 type: 'image/png',
60 sizes: '32x32',
61 href: '/favicon-32x32.png',
62 },
63 {
64 rel: 'icon',
65 type: 'image/png',
66 sizes: '16x16',
67 href: '/favicon-16x16.png',
68 },
69 { rel: 'manifest', href: '/site.webmanifest', color: '#fffff' },
70 { rel: 'icon', href: '/favicon.ico' },
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 <ClerkProvider>
87 <RootDocument>
88 <Outlet />
89 </RootDocument>
90 </ClerkProvider>
91 )
93​
94function RootDocument({ children }: { children: React.ReactNode }) {
95 return (
96 <html>
97 <head>
98 <HeadContent />
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 <SignedIn>
121 <UserButton />
122 </SignedIn>
123 <SignedOut>
124 <SignInButton mode="modal" />
125 </SignedOut>
126 </div>
127 </div>
128 <hr />
129 {children}
130 <TanStackRouterDevtools position="bottom-right" />
131 <Scripts />
132 </body>
133 </html>
134 )