Description
PostHog is statically imported in base-api.service.ts (line 1) and error.tsx (line 3). Since base-api.service.ts is imported by virtually every API call, PostHog ends up in the core bundle even though it's only needed for error tracking.
Vercel React Best Practices Rule: bundle-defer-third-party (2.3)
Files to change
surfsense_web/lib/apis/base-api.service.ts
surfsense_web/app/error.tsx
What to do
In base-api.service.ts:
- Remove the static import:
import posthog from "posthog-js";
- Replace direct
posthog.captureException(error) calls with a dynamic import:
import("posthog-js").then(({ default: posthog }) => {
posthog.captureException(error);
}).catch(() => {});
In error.tsx:
- Remove the static import:
import posthog from "posthog-js";
- Use the same dynamic import pattern for
captureException.
Note: Keep PostHogProvider.tsx as-is — that's the intended initialization point.
Acceptance criteria
posthog-js is not statically imported in base-api.service.ts or error.tsx
- Error tracking still works (errors are captured by PostHog)
- Core bundle size is reduced
Description
PostHog is statically imported in
base-api.service.ts(line 1) anderror.tsx(line 3). Sincebase-api.service.tsis imported by virtually every API call, PostHog ends up in the core bundle even though it's only needed for error tracking.Vercel React Best Practices Rule:
bundle-defer-third-party(2.3)Files to change
surfsense_web/lib/apis/base-api.service.tssurfsense_web/app/error.tsxWhat to do
In
base-api.service.ts:import posthog from "posthog-js";posthog.captureException(error)calls with a dynamic import:In
error.tsx:import posthog from "posthog-js";captureException.Note: Keep
PostHogProvider.tsxas-is — that's the intended initialization point.Acceptance criteria
posthog-jsis not statically imported inbase-api.service.tsorerror.tsx