Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

__root.tsx

156 lines4.7 KB

1import type { QueryClient } from "@tanstack/react-query";
2import {
3 createRootRouteWithContext,
4 HeadContent,
5 Scripts,
6} from "@tanstack/react-router";
7import { ThemeProvider } from "@/components/theme-provider";
8import appCss from "../styles.css?url";
9import "prismjs/themes/prism-tomorrow.css"; // or any other Prism theme
10import { ScriptOnce } from "@tanstack/react-router";
11import { Card, CardContent, CardHeader } from "@/components/ui/card";
12import { Toaster } from "@/components/ui/sonner";
13import { seo } from "@/utils/seo";
14​
15interface MyRouterContext {
16 queryClient: QueryClient;
18​
19export const Route = createRootRouteWithContext<MyRouterContext>()({
20 head: () => ({
21 meta: [
22 {
23 charSet: "utf-8",
24 },
25 {
26 name: "viewport",
27 content: "width=device-width, initial-scale=1",
28 },
29 ...seo({
30 title: "trytanstackdb | The interactive guide",
31 description: `An interactive guide for getting started with TanStack DB.`,
32 image: `https://trytanstackdb.com/og-image.png`,
33 keywords: "tanstack,tanstack db,reactjs,tutorial",
34 }),
35 ],
36 links: [
37 {
38 rel: "preconnect",
39 href: "https://fonts.googleapis.com",
40 },
41 {
42 rel: "preconnect",
43 href: "https://fonts.gstatic.com",
44 crossOrigin: "anonymous",
45 },
46 {
47 rel: "stylesheet",
48 href: "https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300;400;500;600;700&display=swap",
49 },
50 {
51 rel: "stylesheet",
52 href: appCss,
53 },
54 ],
55 }),
56 shellComponent: RootDocument,
57 errorComponent: ErrorComponent,
58});
59​
60function ErrorComponent({ error }: { error: unknown }) {
61 const isServiceWorkerError =
62 error instanceof Error &&
63 error.message.includes("navigator.serviceWorker.addEventListener");
64​
65 return (
66 <div className="fixed inset-0 w-screen h-screen flex justify-center items-center p-6 bg-background">
67 <Card className="max-w-2xl w-full">
68 <CardHeader>
69 <h2 className="text-2xl font-bold text-destructive-foreground text-center">
70 An error occurred
71 </h2>
72 </CardHeader>
73​
74 <CardContent className="flex flex-col gap-4">
75 {isServiceWorkerError ? (
76 <>
77 <p className="block sm:hidden">
78 Embedded browsers (like those in some social media apps) might
79 not support Service Workers properly.
80 </p>
81 <p className="hidden sm:block">
82 It seems like your browser is having issues with Service
83 Workers. Please try using a different browser or updating your
84 current one.
85 </p>
86 </>
87 ) : null}
88 <p className="font-bold">Error message:</p>
89 <pre className="wrap-break-word whitespace-pre-wrap text max-h-40 text-muted-foreground overflow-y-auto">
90 {error instanceof Error ? error.message : String(error)}
91 </pre>
92 </CardContent>
93 </Card>
94 </div>
95 );
97​
98function ServiceWorkerLoader() {
99 const serviceWorkerScript = (() => {
100 const registerServiceWorker = async () => {
101 const serviceWorkerLoadedVarName = "__SERVICE_WORKER_LOADED__";
102 // @ts-expect-error
103 if (!window[serviceWorkerLoadedVarName]) {
104 if ("serviceWorker" in navigator) {
105 try {
106 const registration = await navigator.serviceWorker.register(
107 "/sw.js",
108 {
109 scope: "/",
110 type: "module",
111 },
112 );
113 if (registration.installing) {
114 console.log("Service worker installing");
115 } else if (registration.waiting) {
116 console.log("Service worker installed");
117 } else if (registration.active) {
118 console.log("Service worker active");
119 }
120 } catch (error) {
121 console.error(`Registration failed with ${error}`);
122 }
123 }
124 // @ts-expect-error
125 window[serviceWorkerLoadedVarName] = true;
126 }
127 };
128​
129 registerServiceWorker();
130 return `(${registerServiceWorker.toString()})();`;
131 })();
132​
133 return <ScriptOnce children={serviceWorkerScript} />;
135​
136function RootDocument({ children }: { children: React.ReactNode }) {
137 return (
138 <html lang="en" suppressHydrationWarning>
139 <head>
140 <HeadContent />
141 </head>
142 <body>
143 <ServiceWorkerLoader />
144 <ThemeProvider>
145 {children}
146 <Toaster position="top-right" />
147 </ThemeProvider>
148 <script
149 defer
150 src="https://assets.onedollarstats.com/stonks.js"
151 ></script>
152 <Scripts />
153 </body>
154 </html>
155 );