Open History to see the last commit
1/// <reference types="vite/client" />
2import {
3 HeadContent,
4 Link,
5 Scripts,
6 createRootRoute,
7} from '@tanstack/react-router'
8import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
9import * as React from 'react'
10import { DefaultCatchBoundary } from '~/components/DefaultCatchBoundary'
11import { NotFound } from '~/components/NotFound'
12import { Test, startInstance } from '~/start'
13import appCss from '~/styles/app.css?url'
14import { seo } from '~/utils/seo'
15β
16export const testServerMw = startInstance
17 .createMiddleware()
18 .server(({ next, context }) => {
19 context.fromFetch
20 // ^?
21 context.fromServerMw
22 // ^?
23β
24 return next({
25 context: {
26 fromIndexServerMw: true,
27 },
28 })
29 })
30β
31export const testFnMw = startInstance
32 .createMiddleware({ type: 'function' })
33 .middleware([testServerMw])
34 .server(({ next, context }) => {
35 context.fromFetch
36 // ^?
37 context.fromServerMw
38 // ^?
39 context.fromFnMw
40 // ^?
41 context.fromIndexServerMw
42 // ^?
43β
44 return next({
45 context: {
46 fromIndexFnMw: true,
47 },
48 })
49 })
50β
51export const testGetMiddleware = startInstance
52 .createMiddleware()
53 .server(({ next, context }) => {
54 return next({
55 context: {
56 fromGetMiddleware: true,
57 },
58 })
59 })
60β
61export const Route = createRootRoute({
62 server: {
63 middleware: [testServerMw],
64 handlers: {
65 GET: ({ context, next }) => {
66 context.fromFetch
67 // ^?
68 context.fromServerMw
69 // ^?
70 context.fromIndexServerMw
71 // ^?
72 return next({
73 context: {
74 fromGet: true,
75 },
76 })
77 },
78 POST: ({ context, next }) => {
79 context.fromFetch
80 context.fromServerMw
81 context.fromIndexServerMw
82 return next({
83 context: {
84 fromPost: true,
85 },
86 })
87 },
88 },
89 // handlers: ({ createHandlers }) =>
90 // createHandlers({
91 // GET: {
92 // middleware: [testGetMiddleware],
93 // handler: ({ context, next }) => {
94 // context.fromFetch
95 // // ^?
96 // context.fromServerMw
97 // // ^?
98 // context.fromIndexServerMw
99 // // ^?
100 // context.fromGetMiddleware
101 // // ^?
102 // return next({
103 // context: {
104 // fromGet: true,
105 // fromPost: false,
106 // },
107 // })
108 // },
109 // },
110 // POST: {
111 // handler: ({ next }) => {
112 // return next({
113 // context: {
114 // fromGet: false,
115 // fromPost: true,
116 // },
117 // })
118 // },
119 // },
120 // }),
121 test: (test) => {},
122 },
123 beforeLoad: ({ serverContext }) => {
124 serverContext?.fromFetch
125 // ^?
126 serverContext?.fromServerMw
127 // ^?
128 serverContext?.fromIndexServerMw
129 // ^?
130 serverContext?.fromGet
131 // ^?
132 return serverContext
133 },
134 // ssr: false,
135 loader: ({ context }) => {
136 context?.fromFetch
137 // ^?
138 context?.fromServerMw
139 // ^?
140 context?.fromIndexServerMw
141 // ^?
142 context?.fromPost
143 // ^?
144 return new Test('test')
145 },
146 head: () => ({
147 meta: [
148 {
149 charSet: 'utf-8',
150 },
151 {
152 name: 'viewport',
153 content: 'width=device-width, initial-scale=1',
154 },
155 ...seo({
156 title:
157 'TanStack Start | Type-Safe, Client-First, Full-Stack React Framework',
158 description: `TanStack Start is a type-safe, client-first, full-stack React framework. `,
159 }),
160 ],
161 links: [
162 { rel: 'stylesheet', href: appCss },
163 {
164 rel: 'apple-touch-icon',
165 sizes: '180x180',
166 href: '/apple-touch-icon.png',
167 },
168 {
169 rel: 'icon',
170 type: 'image/png',
171 sizes: '32x32',
172 href: '/favicon-32x32.png',
173 },
174 {
175 rel: 'icon',
176 type: 'image/png',
177 sizes: '16x16',
178 href: '/favicon-16x16.png',
179 },
180 { rel: 'manifest', href: '/site.webmanifest', color: '#fffff' },
181 { rel: 'icon', href: '/favicon.ico' },
182 ],
183 scripts: [
184 {
185 src: '/customScript.js',
186 type: 'text/javascript',
187 },
188 ],
189 }),
190 errorComponent: DefaultCatchBoundary,
191 notFoundComponent: () => <NotFound />,
192 shellComponent: RootDocument,
193})
194β
195function RootDocument({ children }: { children: React.ReactNode }) {
196 return (
197 <html>
198 <head>
199 <HeadContent />
200 </head>
201 <body>
202 <div className="p-2 flex gap-2 text-lg">
203 <Link
204 to="/"
205 activeProps={{
206 className: 'font-bold',
207 }}
208 activeOptions={{ exact: true }}
209 >
210 Home
211 </Link>{' '}
212 <Link
213 to="/posts"
214 activeProps={{
215 className: 'font-bold',
216 }}
217 >
218 Posts
219 </Link>{' '}
220 <Link
221 to="/users"
222 activeProps={{
223 className: 'font-bold',
224 }}
225 >
226 Users
227 </Link>{' '}
228 <Link
229 to="/route-a"
230 activeProps={{
231 className: 'font-bold',
232 }}
233 >
234 Pathless Layout
235 </Link>{' '}
236 <Link
237 to="/deferred"
238 activeProps={{
239 className: 'font-bold',
240 }}
241 >
242 Deferred
243 </Link>{' '}
244 <Link
245 // @ts-expect-error
246 to="/this-route-does-not-exist"
247 activeProps={{
248 className: 'font-bold',
249 }}
250 >
251 This Route Does Not Exist
252 </Link>
253 </div>
254 <hr />
255 {children}
256 <TanStackRouterDevtools position="bottom-right" />
257 <Scripts />
258 </body>
259 </html>
260 )
261}