Skip to content

Open source alternative to Semrush and Ahrefs

Keep charts and site-audit UI out of the shared entry bundle (#921)

Carter Smith4ef0bfa96f78
diff --git a/src/client/features/ai-visibility/VisibilityTrend.tsx b/src/client/features/ai-visibility/VisibilityTrend.tsx
index 08e525fc..5fd9d3b2 100644
--- a/src/client/features/ai-visibility/VisibilityTrend.tsx
+++ b/src/client/features/ai-visibility/VisibilityTrend.tsx
@@ -1,5 +1,5 @@
import { useState } from "react";
-import { queryOptions, useQuery } from "@tanstack/react-query";
+import { useQuery } from "@tanstack/react-query";
import { Info, TriangleAlert } from "lucide-react";
import { cn } from "cn";
import { SegmentedToggle } from "@/client/components/SegmentedToggle";
@@ -13,11 +13,10 @@ import {
TableHeader,
TableRow,
} from "@/client/components/ui/table";
-import { getAiVisibilityTrend } from "@/serverFunctions/ai-visibility";
import type { AiTrend, AiTrendMetric } from "@/shared/ai-visibility";
import { EngineLabel } from "./EngineLabel";
import { trendSeries, VisibilityTrendChart } from "./VisibilityTrendChart";
-import { AiQueryError, aiVisibilityKey } from "./shared";
+import { AiQueryError, type AiTrendDays, aiTrendQueryOptions } from "./shared";
const periods = [
{ value: "7", icon: null, label: "7d" },
@@ -44,14 +43,6 @@ function notice(trend: AiTrend, collecting: boolean) {
}
}
-type AiTrendDays = 7 | 28 | 90;
-
-export const aiTrendQueryOptions = (projectId: string, days: AiTrendDays) =>
- queryOptions({
- queryKey: [...aiVisibilityKey(projectId), "results", "trend", days],
- queryFn: () => getAiVisibilityTrend({ data: { projectId, days } }),
- });
-
/** The trend panel inside the Prompt tracking card, laid out like rank tracking's overview. */
export function VisibilityTrend({
projectId,
diff --git a/src/client/features/ai-visibility/shared.tsx b/src/client/features/ai-visibility/shared.tsx
index fc703d93..feb6612d 100644
--- a/src/client/features/ai-visibility/shared.tsx
+++ b/src/client/features/ai-visibility/shared.tsx
@@ -11,6 +11,7 @@ import {
getAiResearchSetup,
getAiVisibilityRun,
getAiVisibilityTracker,
+ getAiVisibilityTrend,
getAiVisibilityResults,
listAiResearchKeywords,
} from "@/serverFunctions/ai-visibility";
@@ -63,6 +64,14 @@ export const aiResearchKeywordsQueryOptions = (projectId: string) =>
queryFn: () => listAiResearchKeywords({ data: { projectId } }),
});
+export type AiTrendDays = 7 | 28 | 90;
+
+export const aiTrendQueryOptions = (projectId: string, days: AiTrendDays) =>
+ queryOptions({
+ queryKey: [...aiVisibilityKey(projectId), "results", "trend", days],
+ queryFn: () => getAiVisibilityTrend({ data: { projectId, days } }),
+ });
+
/** Every answer in a run, so each prompt can show its per-engine result. */
export const aiRunResultsQueryOptions = (
projectId: string,
diff --git a/src/routes/_app/p/$projectId/ai-visibility/index.tsx b/src/routes/_app/p/$projectId/ai-visibility/index.tsx
index ccb5fb33..b4c850c6 100644
--- a/src/routes/_app/p/$projectId/ai-visibility/index.tsx
+++ b/src/routes/_app/p/$projectId/ai-visibility/index.tsx
@@ -5,8 +5,8 @@ import { PROMPT_TRACKING_TABS } from "@/client/features/ai-visibility/PromptTrac
import {
aiRunResultsQueryOptions,
aiTrackerQueryOptions,
+ aiTrendQueryOptions,
} from "@/client/features/ai-visibility/shared";
-import { aiTrendQueryOptions } from "@/client/features/ai-visibility/VisibilityTrend";
import { queryClient } from "@/client/tanstack-db/queryClient";
const tabs = PROMPT_TRACKING_TABS.map((item) => item.value);
diff --git a/src/routes/_app/p/$projectId/audit/index.tsx b/src/routes/_app/p/$projectId/audit/index.tsx
index 4cd7d922..94e48a6b 100644
--- a/src/routes/_app/p/$projectId/audit/index.tsx
+++ b/src/routes/_app/p/$projectId/audit/index.tsx
@@ -4,9 +4,7 @@ import { auditSearchSchema } from "@/types/schemas/audit";
import { LaunchView } from "@/client/features/audit/launch/LaunchView";
import { AuditDetail } from "@/client/features/audit/AuditDetail";
-export const Route = createFileRoute<"/_app/p/$projectId/audit/">(
- "/_app/p/$projectId/audit/",
-)({
+export const Route = createFileRoute("/_app/p/$projectId/audit/")({
validateSearch: auditSearchSchema,
component: SiteAuditPage,
});
diff --git a/vite-plugin-lean-worker-bundle.ts b/vite-plugin-lean-worker-bundle.ts
index fe2236e2..95d502c2 100644
--- a/vite-plugin-lean-worker-bundle.ts
+++ b/vite-plugin-lean-worker-bundle.ts
@@ -64,6 +64,20 @@ const EAGER_DENYLIST: Array<{ pattern: RegExp; expected: string }> = [
},
];
+/**
+ * Dependencies that must stay out of the browser's entry chunk. The whole app
+ * renders inside <ClientOnly>, so every page waits for that chunk to load.
+ */
+const CLIENT_ENTRY_DENYLIST: Array<{ pattern: RegExp; expected: string }> = [
+ {
+ pattern: /node_modules\/recharts\//,
+ expected:
+ "imported only from route components, which are code-split. Route " +
+ "options like loader and validateSearch are not, so they must import " +
+ "from chart-free modules",
+ },
+];
+
/**
* Keeps the worker's eager server bundle lean, in three parts:
*
@@ -77,7 +91,8 @@ const EAGER_DENYLIST: Array<{ pattern: RegExp; expected: string }> = [
* 3. A `generateBundle` assertion that walks the static-import closure of the
* worker entry chunk and fails the build if any EAGER_DENYLIST module is
* reachable — turning "we verified the chunk by grepping a sourcemap once"
- * into a permanent regression test.
+ * into a permanent regression test. The client build gets the same check
+ * against CLIENT_ENTRY_DENYLIST.
*/
export function leanWorkerBundle(): Plugin {
return {
@@ -116,11 +131,18 @@ export function leanWorkerBundle(): Plugin {
}
},
generateBundle(_options, bundle) {
- // Only the worker builds matter for isolate memory; the client bundle
- // never contains these packages (and the zod swap applies everywhere).
- // "ssr" is the main worker; "open_seo_audit" is the site-audit aux
- // worker, which must stay lean for the same reason it exists.
- if (!["ssr", "open_seo_audit"].includes(this.environment.name)) {
+ // EAGER_DENYLIST only matters for the worker builds' isolate memory; the
+ // client bundle never contains these packages (and the zod swap applies
+ // everywhere). "ssr" is the main worker; "open_seo_audit" is the
+ // site-audit aux worker, which must stay lean for the same reason it
+ // exists.
+ const environment = this.environment.name;
+ let denylist: typeof EAGER_DENYLIST;
+ if (environment === "client") {
+ denylist = CLIENT_ENTRY_DENYLIST;
+ } else if (["ssr", "open_seo_audit"].includes(environment)) {
+ denylist = EAGER_DENYLIST;
+ } else {
return;
}
@@ -145,7 +167,7 @@ export function leanWorkerBundle(): Plugin {
}
const violations: string[] = [];
- for (const rule of EAGER_DENYLIST) {
+ for (const rule of denylist) {
for (const fileName of eager) {
const hits =
chunkAt(fileName)?.moduleIds.filter((id) =>
@@ -163,9 +185,9 @@ export function leanWorkerBundle(): Plugin {
if (violations.length > 0) {
this.error(
- `[lean-worker-bundle] denylisted module(s) in the worker's eager ` +
- `startup graph:\n\n${violations.join("\n\n")}\n\nRestore the ` +
- `lazy/stub boundary, or update EAGER_DENYLIST in ` +
+ `[lean-worker-bundle] denylisted module(s) in the ${environment} ` +
+ `build's eager startup graph:\n\n${violations.join("\n\n")}\n\n` +
+ `Restore the lazy/stub boundary, or update the denylist in ` +
`vite-plugin-lean-worker-bundle.ts if this is intentional.`,
);
}