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

__root.tsx

143 lines3.3 KB

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