Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(web): keep the dev-only TanStack devtools out of production bundles

Richie McIlroy03e1bc97fdb5
diff --git a/apps/web/app/Layout/Devtools.tsx b/apps/web/app/Layout/Devtools.tsx
new file mode 100644
index 000000000..60cf4c119
--- /dev/null
+++ b/apps/web/app/Layout/Devtools.tsx
@@ -0,0 +1,135 @@
+"use client";
+
+import {
+ TanStackDevtools,
+ type TanStackDevtoolsReactInit,
+} from "@tanstack/react-devtools";
+import { useQueryClient } from "@tanstack/react-query";
+import { ReactQueryDevtoolsPanel } from "@tanstack/react-query-devtools";
+import { useEffect, useState } from "react";
+import {
+ demoteFromPro,
+ promoteToPro,
+ restartOnboarding,
+} from "./devtoolsServer";
+
+type DevtoolsConfig = NonNullable<TanStackDevtoolsReactInit["config"]>;
+
+const devtoolsSettingsStorageKey = "tanstack_devtools_settings";
+
+const devtoolsConfig = {
+ hideUntilHover: false,
+ position: "top-left",
+ requireUrlFlag: false,
+} satisfies DevtoolsConfig;
+
+function getDevtoolsSettings(value: string | null): DevtoolsConfig {
+ if (!value) return devtoolsConfig;
+
+ try {
+ const parsed: unknown = JSON.parse(value);
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
+ return {
+ ...parsed,
+ ...devtoolsConfig,
+ };
+ }
+ } catch {
+ return devtoolsConfig;
+ }
+
+ return devtoolsConfig;
+}
+
+function persistDevtoolsSettings() {
+ if (typeof window === "undefined") return;
+
+ try {
+ window.localStorage.setItem(
+ devtoolsSettingsStorageKey,
+ JSON.stringify(
+ getDevtoolsSettings(
+ window.localStorage.getItem(devtoolsSettingsStorageKey),
+ ),
+ ),
+ );
+ } catch {
+ return;
+ }
+}
+
+export default function Devtools() {
+ const client = useQueryClient();
+ const [isReady, setIsReady] = useState(false);
+
+ useEffect(() => {
+ persistDevtoolsSettings();
+ setIsReady(true);
+ }, []);
+
+ if (!isReady) return null;
+
+ return (
+ <TanStackDevtools
+ config={devtoolsConfig}
+ plugins={[
+ {
+ id: "cap",
+ name: "Cap",
+ render: <CapDevtools />,
+ },
+ {
+ id: "tanstack-query",
+ name: "Tanstack Query",
+ render: <ReactQueryDevtoolsPanel client={client} />,
+ },
+ ]}
+ />
+ );
+}
+
+function CapDevtools() {
+ return (
+ <div className="flex flex-col p-4 space-y-4">
+ <h1 className="text-2xl font-semibold">Cap Devtools</h1>
+ <div className="space-y-2">
+ <h1 className="text-lg font-semibold">Cap Pro</h1>
+ <p className="text-xs text-muted-foreground">
+ Toggle the current user's Pro status (dev only)
+ </p>
+ <div className="flex items-center space-x-2">
+ <form action={promoteToPro}>
+ <button
+ type="submit"
+ className="px-2 py-1 text-xs font-medium text-white bg-green-600 rounded hover:bg-green-700"
+ >
+ Promote to Pro
+ </button>
+ </form>
+ <form action={demoteFromPro}>
+ <button
+ type="submit"
+ className="px-2 py-1 text-xs font-medium text-white bg-red-600 rounded hover:bg-red-700"
+ >
+ Demote from Pro
+ </button>
+ </form>
+ </div>
+ </div>
+ <div className="space-y-2">
+ <h1 className="text-lg font-semibold">Onboarding</h1>
+ <p className="text-xs text-muted-foreground">
+ Restart the onboarding process for the current user (dev only)
+ </p>
+ <form action={restartOnboarding}>
+ <button
+ type="submit"
+ className="px-2 py-1 text-xs font-medium text-white bg-blue-600 rounded hover:bg-blue-700"
+ >
+ Restart Onboarding
+ </button>
+ </form>
+ </div>
+ </div>
+ );
+}
diff --git a/apps/web/app/Layout/providers.tsx b/apps/web/app/Layout/providers.tsx
index 9f20885f8..b9439760f 100644
--- a/apps/web/app/Layout/providers.tsx
+++ b/apps/web/app/Layout/providers.tsx
@@ -1,16 +1,12 @@
"use client";
-import {
- TanStackDevtools,
- type TanStackDevtoolsReactInit,
-} from "@tanstack/react-devtools";
-import {
- QueryClient,
- QueryClientProvider,
- useQueryClient,
-} from "@tanstack/react-query";
-import { ReactQueryDevtoolsPanel } from "@tanstack/react-query-devtools";
-import { type PropsWithChildren, useEffect, useState } from "react";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import dynamic from "next/dynamic";
+import { SessionProvider as NASessionProvider } from "next-auth/react";
+import { type PropsWithChildren, useState } from "react";
+
+// Development-only, so it stays out of every production page's bundle.
+const Devtools = dynamic(() => import("./Devtools"), { ssr: false });
export function ReactQueryProvider({
children,
@@ -27,134 +23,6 @@ export function ReactQueryProvider({
);
}
-import { SessionProvider as NASessionProvider } from "next-auth/react";
-import {
- demoteFromPro,
- promoteToPro,
- restartOnboarding,
-} from "./devtoolsServer";
-
export function SessionProvider({ children }: PropsWithChildren) {
return <NASessionProvider>{children}</NASessionProvider>;
}
-
-type DevtoolsConfig = NonNullable<TanStackDevtoolsReactInit["config"]>;
-
-const devtoolsSettingsStorageKey = "tanstack_devtools_settings";
-
-const devtoolsConfig = {
- hideUntilHover: false,
- position: "top-left",
- requireUrlFlag: false,
-} satisfies DevtoolsConfig;
-
-function getDevtoolsSettings(value: string | null): DevtoolsConfig {
- if (!value) return devtoolsConfig;
-
- try {
- const parsed: unknown = JSON.parse(value);
- if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
- return {
- ...parsed,
- ...devtoolsConfig,
- };
- }
- } catch {
- return devtoolsConfig;
- }
-
- return devtoolsConfig;
-}
-
-function persistDevtoolsSettings() {
- if (typeof window === "undefined") return;
-
- try {
- window.localStorage.setItem(
- devtoolsSettingsStorageKey,
- JSON.stringify(
- getDevtoolsSettings(
- window.localStorage.getItem(devtoolsSettingsStorageKey),
- ),
- ),
- );
- } catch {
- return;
- }
-}
-
-export function Devtools() {
- const client = useQueryClient();
- const [isReady, setIsReady] = useState(false);
-
- useEffect(() => {
- persistDevtoolsSettings();
- setIsReady(true);
- }, []);
-
- if (!isReady) return null;
-
- return (
- <TanStackDevtools
- config={devtoolsConfig}
- plugins={[
- {
- id: "cap",
- name: "Cap",
- render: <CapDevtools />,
- },
- {
- id: "tanstack-query",
- name: "Tanstack Query",
- render: <ReactQueryDevtoolsPanel client={client} />,
- },
- ]}
- />
- );
-}
-
-function CapDevtools() {
- return (
- <div className="flex flex-col p-4 space-y-4">
- <h1 className="text-2xl font-semibold">Cap Devtools</h1>
- <div className="space-y-2">
- <h1 className="text-lg font-semibold">Cap Pro</h1>
- <p className="text-xs text-muted-foreground">
- Toggle the current user's Pro status (dev only)
- </p>
- <div className="flex items-center space-x-2">
- <form action={promoteToPro}>
- <button
- type="submit"
- className="px-2 py-1 text-xs font-medium text-white bg-green-600 rounded hover:bg-green-700"
- >
- Promote to Pro
- </button>
- </form>
- <form action={demoteFromPro}>
- <button
- type="submit"
- className="px-2 py-1 text-xs font-medium text-white bg-red-600 rounded hover:bg-red-700"
- >
- Demote from Pro
- </button>
- </form>
- </div>
- </div>
- <div className="space-y-2">
- <h1 className="text-lg font-semibold">Onboarding</h1>
- <p className="text-xs text-muted-foreground">
- Restart the onboarding process for the current user (dev only)
- </p>
- <form action={restartOnboarding}>
- <button
- type="submit"
- className="px-2 py-1 text-xs font-medium text-white bg-blue-600 rounded hover:bg-blue-700"
- >
- Restart Onboarding
- </button>
- </form>
- </div>
- </div>
- );
-}