Open History to see the last commit
1/// <reference types="vite/client" />
2import {
3 HeadContent,
4 Link,
5 Outlet,
6 Scripts,
7 createRootRoute,
8} from '@tanstack/react-router'
9import {
10 ClerkProvider,
11 SignInButton,
12 SignedIn,
13 SignedOut,
14 UserButton,
15} from '@clerk/tanstack-react-start'
16import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
17import { createServerFn } from '@tanstack/react-start'
18import * as React from 'react'
19import { getAuth } from '@clerk/tanstack-react-start/server'
20import { getRequest } from '@tanstack/react-start/server'
21import { DefaultCatchBoundary } from '~/components/DefaultCatchBoundary.js'
22import { NotFound } from '~/components/NotFound.js'
23import appCss from '~/styles/app.css?url'
24β
25const fetchClerkAuth = createServerFn({ method: 'GET' }).handler(async () => {
26 const user = await getAuth(getRequest()!)
27β
28 return {
29 user,
30 }
31})
32β
33export const Route = createRootRoute({
34 beforeLoad: async () => {
35 const { user } = await fetchClerkAuth()
36β
37 return {
38 user,
39 }
40 },
41 head: () => ({
42 meta: [
43 {
44 charSet: 'utf-8',
45 },
46 {
47 name: 'viewport',
48 content: 'width=device-width, initial-scale=1',
49 },
50 ],
51 links: [
52 { rel: 'stylesheet', href: appCss },
53 {
54 rel: 'apple-touch-icon',
55 sizes: '180x180',
56 href: '/apple-touch-icon.png',
57 },
58 {
59 rel: 'icon',
60 type: 'image/png',
61 sizes: '32x32',
62 href: '/favicon-32x32.png',
63 },
64 {
65 rel: 'icon',
66 type: 'image/png',
67 sizes: '16x16',
68 href: '/favicon-16x16.png',
69 },
70 { rel: 'manifest', href: '/site.webmanifest', color: '#fffff' },
71 { rel: 'icon', href: '/favicon.ico' },
72 ],
73 }),
74 errorComponent: (props) => {
75 return (
76 <RootDocument>
77 <DefaultCatchBoundary {...props} />
78 </RootDocument>
79 )
80 },
81 notFoundComponent: () => <NotFound />,
82 component: RootComponent,
83})
84β
85function RootComponent() {
86 return (
87 <ClerkProvider>
88 <RootDocument>
89 <Outlet />
90 </RootDocument>
91 </ClerkProvider>
92 )
93}
94β
95function RootDocument({ children }: { children: React.ReactNode }) {
96 return (
97 <html>
98 <head>
99 <HeadContent />
100 </head>
101 <body>
102 <div className="p-2 flex gap-2 text-lg">
103 <Link
104 to="/"
105 activeProps={{
106 className: 'font-bold',
107 }}
108 activeOptions={{ exact: true }}
109 >
110 Home
111 </Link>{' '}
112 <Link
113 to="/posts"
114 activeProps={{
115 className: 'font-bold',
116 }}
117 >
118 Posts
119 </Link>
120 <div className="ml-auto">
121 <SignedIn>
122 <UserButton />
123 </SignedIn>
124 <SignedOut>
125 <SignInButton mode="modal" />
126 </SignedOut>
127 </div>
128 </div>
129 <hr />
130 {children}
131 <TanStackRouterDevtools position="bottom-right" />
132 <Scripts />
133 </body>
134 </html>
135 )
136}