Skip to content

fix: cache PostHog instance across AnalyticsProvider remounts (v0.1.39) - #61

Merged
akbarsaputrait merged 2 commits into
masterfrom
fix/analytics-provider-remount-cache
Jul 26, 2026
Merged

fix: cache PostHog instance across AnalyticsProvider remounts (v0.1.39)#61
akbarsaputrait merged 2 commits into
masterfrom
fix/analytics-provider-remount-cache

Conversation

@akbarsaputrait

Copy link
Copy Markdown
Member

Summary

  • Cache the built-in PostHog instance module-level in AnalyticsProvider so provider remounts (e.g. per-route layouts wrapping RozoPayProvider) reuse it instead of re-calling posthog-js init(), which was logging a spurious "already initialized" warning.
  • Bump @rozoai/intent-pay to 0.1.39 and add the CHANGELOG entry (already released to npm).

Test plan

  • pnpm build in packages/connectkit
  • lint-staged / husky pre-commit passed
  • Verify no duplicate PostHog init warning in a host app with route-level remounts

Host apps that remount the provider tree per-route (e.g. layouts wrapping
RozoPayProvider fresh on navigation) triggered posthog-js init() again,
logging a spurious "already initialized" warning. Cache the instance
module-level and reuse it instead.
Bump version and add changelog entry for the PostHog remount-cache fix.
@vercel

vercel Bot commented Jul 26, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
intent-example Ready Ready Preview, Comment Jul 26, 2026 4:08pm

Request Review

},
"rozo-sdk-telemetry",
);
cachedBuiltin = builtin;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 (nice to have): cachedBuiltin is only assigned after the async import("posthog-js") promise resolves, so the comment's claim that this "avoids the duplicate init call entirely" is slightly overstated. If two AnalyticsProvider instances mount before this first .then() runs (e.g. a fast double-remount, or two providers in the tree), both will see cachedBuiltin == null and both call ph.init(..., "rozo-sdk-telemetry"), re-triggering the exact warning this fixes.

The real target scenario — per-route remount after initial load — happens well after init resolves, so the fix works for it. But to fully close the window you could set a "pending" sentinel synchronously before the import, or cache the promise itself:

let cachedBuiltin: unknown = null;
let builtinInitPromise: Promise<unknown> | null = null;

Minor; not blocking.

@claude

claude Bot commented Jul 26, 2026

Copy link
Copy Markdown

Pre-merge review PR 61 (v0.1.39)

Verdict: no blockers (0 P0). Small, well-scoped analytics fix. Reviewed all four changed files.

Scope

  • AnalyticsProvider.tsx — module-level cache (cachedBuiltin) so the named PostHog instance is reused across provider remounts instead of re-calling init().
  • package.json / CHANGELOG.md — version bump to 0.1.39.
  • bundle-analysis.html — regenerated build artifact.

P0 (blocking): none

  • No secrets/keys leaked. POSTHOG_KEY (phc_...) is a client-side PostHog project key (public by design) and is pre-existing in rozoConfig.ts — not touched by this PR.
  • No auth/RLS changes, no edge functions, no mirror-table writes, no payment/payout/withdrawal logic touched. The change is analytics-only, so the "payment logic with zero tests" P0 rule does not apply.

P1 (should fix): none

P2 (nice to have)

  1. Cache-set race window — cachedBuiltin is assigned only after the async import("posthog-js") resolves, so concurrent/very-fast remounts before that can still double-init(). The real per-route-remount target is unaffected, but the inline comments claim that it "avoids the duplicate init entirely" is slightly overstated. (inline comment left)
  2. bundle-analysis.html — a generated build artifact tracked in git; it adds ~250KB of diff noise on every release. Pre-existing pattern, not introduced here, but worth gitignore-ing eventually.

Not approving or merging per instructions — leaving the verdict to the automated step.

@shawnmuggle shawnmuggle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review found no P0 blockers. Clean analytics-only PostHog remount-cache fix; no P0 blockers, no P1s, two minor P2s (init race window, committed bundle-analysis.html artifact).

@akbarsaputrait
akbarsaputrait merged commit bf93c0e into master Jul 26, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants