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

_tutorial._db.tsx

39 lines1.1 KB

1import { createFileRoute, Outlet } from "@tanstack/react-router";
2import { useEffect } from "react";
3import {
4 setupPGlite,
5 setupServiceWorkerHttpsProxy,
6} from "@/client/pgliteHelpers";
7import { userPreferencesCollection } from "@/collections/UserPreferences";
8import { FakeProgressIndicator } from "@/components/FakeProgressIndicator";
9import { USER_PLACEHOLDER } from "@/utils/USER_PLACEHOLDER_CONSTANT";
10​
11export const Route = createFileRoute("/_tutorial/_db")({
12 // the database is local to the browser, so no SSR
13 ssr: false,
14 beforeLoad: async () => {
15 // we don't need this to run
16 await setupServiceWorkerHttpsProxy();
17 await setupPGlite();
18 },
19 component: RouteComponent,
20 pendingComponent: FakeProgressIndicator,
21 // Show the pending component for at least 500ms
22 pendingMinMs: 500,
23});
24​
25function RouteComponent() {
26 useEffect(() => {
27 if (typeof window !== "undefined") {
28 const existingUser = userPreferencesCollection.get(USER_PLACEHOLDER.id);
29​
30 if (!existingUser) {
31 userPreferencesCollection.insert({
32 id: USER_PLACEHOLDER.id,
33 });
34 }
35 }
36 }, []);
37​
38 return <Outlet />;