Skip to content

Defer PostHog loading from the critical bundle path #1044

@MODSetter

Description

@MODSetter

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:

  1. Remove the static import: import posthog from "posthog-js";
  2. Replace direct posthog.captureException(error) calls with a dynamic import:
import("posthog-js").then(({ default: posthog }) => {
  posthog.captureException(error);
}).catch(() => {});

In error.tsx:

  1. Remove the static import: import posthog from "posthog-js";
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions