fix: cache PostHog instance across AnalyticsProvider remounts (v0.1.39) - #61
Conversation
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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| }, | ||
| "rozo-sdk-telemetry", | ||
| ); | ||
| cachedBuiltin = builtin; |
There was a problem hiding this comment.
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.
|
Pre-merge review PR 61 (v0.1.39) Verdict: no blockers (0 P0). Small, well-scoped analytics fix. Reviewed all four changed files. Scope
P0 (blocking): none
P1 (should fix): none P2 (nice to have)
Not approving or merging per instructions — leaving the verdict to the automated step. |
shawnmuggle
left a comment
There was a problem hiding this comment.
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).
Summary
AnalyticsProviderso provider remounts (e.g. per-route layouts wrappingRozoPayProvider) reuse it instead of re-callingposthog-jsinit(), which was logging a spurious "already initialized" warning.@rozoai/intent-payto0.1.39and add the CHANGELOG entry (already released to npm).Test plan
pnpm buildinpackages/connectkit