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