Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(web): start feature flags from PostHog's cache (#7027)
teod4ac28aceed9
diff --git a/apps/web/src/lib/analytics/posthog.test.tsx b/apps/web/src/lib/analytics/posthog.test.tsxindex 8de399e9c6..60c1c751d0 100644--- a/apps/web/src/lib/analytics/posthog.test.tsx+++ b/apps/web/src/lib/analytics/posthog.test.tsx@@ -8,6 +8,7 @@ import { PosthogProvider, ShowFeatureFlag, useFeatureFlag } from './posthog';const posthog = vi.hoisted(() => ({onFeatureFlags: vi.fn<PostHog['onFeatureFlags']>(),getFeatureFlagResult: vi.fn<PostHog['getFeatureFlagResult']>(),+ featureFlags: { getFlags: vi.fn<() => string[]>() },unsubscribe: vi.fn(),}));@@ -58,6 +59,7 @@ function renderFlag(flag: RemoteFlag = channelTags) {beforeEach(() => {vi.clearAllMocks();posthog.getFeatureFlagResult.mockReset();+ posthog.featureFlags.getFlags.mockReturnValue([]);posthog.onFeatureFlags.mockReturnValue(posthog.unsubscribe);});afterEach(cleanup);@@ -131,6 +133,24 @@ describe('reactive PostHog flags', () => {expect(screen.queryByText('Suspense fallback')).toBeNull();});+ it('serves cached flags before PostHog answers, then applies the answer', async () => {+ posthog.featureFlags.getFlags.mockReturnValue([channelTags.key]);+ posthog.getFeatureFlagResult.mockReturnValue(flagResult(true, 'cached'));+ const view = renderFlag();++ expect(view.result()).toEqual({+ enabled: true,+ payload: 'cached',+ loading: false,+ });+ expect(screen.getByRole('button', { name: 'New label' })).toBeTruthy();++ await receiveFlags(flagResult(false));+ expect(view.result().enabled).toBe(false);+ expect(screen.queryByRole('button', { name: 'New label' })).toBeNull();+ expect(view.mount).toHaveBeenCalledOnce();+ });+it.each([false, true])('keeps the explicit %s override authoritative',async (override) => {diff --git a/apps/web/src/lib/analytics/posthog.tsx b/apps/web/src/lib/analytics/posthog.tsxindex a01c1428fb..1131d2e485 100644--- a/apps/web/src/lib/analytics/posthog.tsx+++ b/apps/web/src/lib/analytics/posthog.tsx@@ -17,14 +17,20 @@ export const [PosthogProvider, usePosthog] = createAssertedContextProvider(() => {const analytics = useAnalytics();- const [featureFlags, setFeatureFlags] = createSignal<string[]>([], {- equals: false,- });+ // PostHog only reports flags once `/flags` answers, but it already serves+ // the flags it cached on the previous visit. Starting from that cache means+ // returning users paint flagged UI once, not flags-off first and then again+ // when the answer lands (e.g. soup lists reloading on a transport swap).+ const cachedFlags = analytics.posthog.featureFlags?.getFlags() ?? [];+ const [featureFlags, setFeatureFlags] = createSignal<string[]>(+ cachedFlags,+ { equals: false }+ );// Distinguishes "flags not fetched yet" from "no flags enabled": both// leave featureFlags empty, but destructive flag-off fallbacks (e.g.// RedirectSplit) must not fire before the answer arrives. Set even on// errorsLoading so a PostHog outage degrades to flags-off, not a hang.- const [flagsLoaded, setFlagsLoaded] = createSignal(false);+ const [flagsLoaded, setFlagsLoaded] = createSignal(cachedFlags.length > 0);const unsub = analytics.posthog.onFeatureFlags((flags, _, ctx) => {// Flags usually arrive after first paint and swap flagged subtrees,