Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(subscriptions): load the export nudge code after the shell renders (#127373)

Paul D'Ambra66e5c9f87279
diff --git a/frontend/bin/check-eager-graph.mjs b/frontend/bin/check-eager-graph.mjs
index 9553746e60d4..1d1aed72283a 100644
--- a/frontend/bin/check-eager-graph.mjs
+++ b/frontend/bin/check-eager-graph.mjs
@@ -108,8 +108,8 @@ const ROOTS = [
{
root: AUTHENTICATED_SHELL,
label: 'authenticated shell (every logged-in page)',
- // 2026-11-02: 4.02 MiB eager output, stable CSS included. ~0.3% headroom.
- budgetBytes: 4_230_000,
+ // 2026-11-02: 4.00 MiB eager output, stable CSS included. ~0.3% headroom.
+ budgetBytes: 4_203_000,
forbidden: [
'node_modules/monaco-editor/',
'products/product_analytics/frontend/insights/activityDescriptions',
@@ -140,6 +140,8 @@ const ROOTS = [
'src/layout/today/TodayTopBar.tsx',
// The rail's pane actions pull in the new-analytics menu and the canvas block library.
'src/layout/today/todayRailItems.tsx',
+ // The export nudge loads the subscriptions code after the shell. It pulls in the subscriptions logic.
+ 'products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeLogic.ts',
// The pay gate loads the payment logic on click. It pulls in the billing product logic.
'src/scenes/billing/paymentEntryLogic.ts',
// The rich text editor stack is for the scenes that edit text. Rendered markdown needs none of it.
@@ -201,8 +203,8 @@ const ROOTS = [
'src/scenes/project-homepage/today/TodayHome.tsx',
],
label: 'today home path',
- // 2026-11-02: 4.93 MiB, stable CSS included. ~0.3% headroom.
- budgetBytes: 5_186_000,
+ // 2026-11-02: 4.92 MiB, stable CSS included. ~0.3% headroom.
+ budgetBytes: 5_170_000,
forbidden: [
'src/scenes/project-homepage/ai-first/AiFirstHomepage.tsx',
'src/scenes/project-homepage/today/TodayReportPage.tsx',
diff --git a/frontend/bin/layer-baseline.txt b/frontend/bin/layer-baseline.txt
index 9c99ebfc834f..a6d6e5f1cf9b 100644
--- a/frontend/bin/layer-baseline.txt
+++ b/frontend/bin/layer-baseline.txt
@@ -177,9 +177,6 @@ frontend/src/lib/components/Errors/Exception/CollapsibleExceptionHeader.tsx -> p
frontend/src/lib/components/Errors/Frame/FrameVariables.tsx -> products/error_tracking/frontend/components/PropertiesTable.tsx
frontend/src/lib/components/EventPropertyTabs/EventPropertyTabs.tsx -> frontend/src/scenes/surveys/utils.ts
frontend/src/lib/components/EventPropertyTabs/EventPropertyTabs.tsx -> products/error_tracking/frontend/utils.ts
-frontend/src/lib/components/ExportButton/exportNudge.ts -> products/subscriptions/frontend/components/Subscriptions/exportNudge/ExportNudgeToast.tsx
-frontend/src/lib/components/ExportButton/exportNudge.ts -> products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeLogic.ts
-frontend/src/lib/components/ExportButton/exportNudge.ts -> products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeSubject.ts
frontend/src/lib/components/FlagCalledRebuildBanner/flagCalledDependencies.ts -> frontend/src/scenes/cohorts/CohortFilters/types.ts
frontend/src/lib/components/FlagCalledRebuildBanner/flagCalledDependencies.ts -> frontend/src/scenes/cohorts/cohortUtils.tsx
frontend/src/lib/components/FlagCalledRebuildBanner/flagCalledDependencies.ts -> frontend/src/scenes/feature-flags/flagEvaluationsTable.ts
diff --git a/frontend/src/lib/components/ExportButton/exportNudge.test.ts b/frontend/src/lib/components/ExportButton/exportNudge.test.ts
new file mode 100644
index 000000000000..2e9a7b5fd290
--- /dev/null
+++ b/frontend/src/lib/components/ExportButton/exportNudge.test.ts
@@ -0,0 +1,35 @@
+import { ExporterFormat } from '~/types'
+
+import type { ExportNudgeCandidate } from 'products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeLogic'
+
+jest.mock('products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeLogic', () => ({
+ lookUpExportNudge: jest.fn(),
+ resolveExportNudgeEligibility: jest.fn(async () => null),
+ captureExportNudgeCheckFailed: jest.fn(),
+ exportNudgeEventProperties: jest.fn(() => ({})),
+}))
+jest.mock('products/subscriptions/frontend/components/Subscriptions/exportNudge/ExportNudgeToast', () => ({
+ claimExportNudgeMessage: jest.fn(),
+}))
+
+const CANDIDATE: ExportNudgeCandidate = { subject: { kind: 'dashboard', dashboardId: 7 }, name: 'Weekly' }
+
+describe('startExportNudge before the subscriptions code loads', () => {
+ it.each([
+ ['an eligible dashboard export', ExporterFormat.PNG, { status: 'eligible', candidate: CANDIDATE }, CANDIDATE],
+ ['an ineligible dashboard export', ExporterFormat.PNG, { status: 'ineligible' }, null],
+ ['a CSV export', ExporterFormat.CSV, { status: 'eligible', candidate: CANDIDATE }, null],
+ ])('folds the answer for %s into the settled toast', async (_, format, lookup, expected) => {
+ await jest.isolateModulesAsync(async () => {
+ const { lookUpExportNudge } =
+ await import('products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeLogic')
+ jest.mocked(lookUpExportNudge).mockReturnValue(lookup as ReturnType<typeof lookUpExportNudge>)
+ const { startExportNudge } = await import('./exportNudge')
+
+ const nudge = startExportNudge({ export_format: format, dashboard: 7 })
+
+ expect(nudge.message).toBeNull()
+ await expect(nudge.resolving).resolves.toEqual(expected)
+ })
+ })
+})
diff --git a/frontend/src/lib/components/ExportButton/exportNudge.ts b/frontend/src/lib/components/ExportButton/exportNudge.ts
index 160b45ee2537..97fbe8b49934 100644
--- a/frontend/src/lib/components/ExportButton/exportNudge.ts
+++ b/frontend/src/lib/components/ExportButton/exportNudge.ts
@@ -2,25 +2,14 @@ import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { ToastButton } from 'lib/lemon-ui/LemonToast/LemonToast'
import { PromiseTimeoutError, withTimeout } from 'lib/utils/async'
import { uuid } from 'lib/utils/dom'
+import { retryImport } from 'lib/utils/retryImport'
import type { SessionRecordingPlayerMode } from 'scenes/session-recordings/player/sessionRecordingPlayerLogic'
import { ExportedAssetType } from '~/types'
-import {
- ExportNudgeCandidate,
- captureExportNudgeCheckFailed,
- exportNudgeEventProperties,
- lookUpExportNudge,
- resolveExportNudgeEligibility,
-} from 'products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeLogic'
-import {
- ExportNudgeSubject,
- exportNudgeSubjectFor,
-} from 'products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeSubject'
-import {
- ExportNudgeMessage,
- claimExportNudgeMessage,
-} from 'products/subscriptions/frontend/components/Subscriptions/exportNudge/ExportNudgeToast'
+import type { ExportNudgeCandidate } from 'products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeLogic'
+import type { ExportNudgeSubject } from 'products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeSubject'
+import type { ExportNudgeMessage } from 'products/subscriptions/frontend/components/Subscriptions/exportNudge/ExportNudgeToast'
import { TriggerExportProps } from './exporter'
@@ -36,6 +25,26 @@ export interface ExportNudge {
const NO_NUDGE: ExportNudge = { message: null, resolving: Promise.resolve(null) }
+interface NudgeModules {
+ logic: typeof import('products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeLogic')
+ subject: typeof import('products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeSubject')
+ toast: typeof import('products/subscriptions/frontend/components/Subscriptions/exportNudge/ExportNudgeToast')
+}
+
+let nudgeModules: NudgeModules | null = null
+
+// The subscriptions code loads after the shell, not on its critical path. It is usually ready long before
+// the first export. If it fails to load, exports go on without the offer.
+const nudgeModulesLoaded: Promise<NudgeModules | null> = Promise.all([
+ retryImport(() => import('products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeLogic')),
+ retryImport(
+ () => import('products/subscriptions/frontend/components/Subscriptions/exportNudge/exportNudgeSubject')
+ ),
+ retryImport(() => import('products/subscriptions/frontend/components/Subscriptions/exportNudge/ExportNudgeToast')),
+])
+ .then(([logic, subject, toast]) => (nudgeModules = { logic, subject, toast }))
+ .catch(() => null)
+
/** What an export that acknowledged its kickoff hands to the toast it finishes on. */
export interface KickoffToast {
toastId: string
@@ -49,27 +58,59 @@ export interface KickoffToast {
* check here and fold it in when the export settles.
*/
export const startExportNudge = (exportData: TriggerExportProps | ExportedAssetType): ExportNudge => {
- const subject = exportNudgeSubjectFor(exportData)
+ if (!nudgeModules) {
+ return {
+ message: null,
+ resolving: nudgeModulesLoaded.then((modules) =>
+ modules ? resolveNudge(modules, exportData) : Promise.resolve(null)
+ ),
+ }
+ }
+ const modules = nudgeModules
+ const subject = modules.subject.exportNudgeSubjectFor(exportData)
if (!subject) {
return NO_NUDGE
}
- const lookup = lookUpExportNudge(subject)
+ const lookup = modules.logic.lookUpExportNudge(subject)
if (lookup.status === 'ineligible') {
return NO_NUDGE
}
if (lookup.status === 'eligible') {
- return { message: claimExportNudgeMessage(lookup.candidate), resolving: Promise.resolve(null) }
+ return { message: modules.toast.claimExportNudgeMessage(lookup.candidate), resolving: Promise.resolve(null) }
+ }
+ return { message: null, resolving: resolveNudgeWhileExporting(modules, subject) }
+}
+
+// The same check as above for an export that started before the subscriptions code loaded. The offer
+// then folds in when the export settles.
+const resolveNudge = async (
+ modules: NudgeModules,
+ exportData: TriggerExportProps | ExportedAssetType
+): Promise<ExportNudgeCandidate | null> => {
+ const subject = modules.subject.exportNudgeSubjectFor(exportData)
+ if (!subject) {
+ return null
+ }
+ const lookup = modules.logic.lookUpExportNudge(subject)
+ if (lookup.status === 'ineligible') {
+ return null
+ }
+ if (lookup.status === 'eligible') {
+ return lookup.candidate
}
- return { message: null, resolving: resolveNudgeWhileExporting(subject) }
+ return await resolveNudgeWhileExporting(modules, subject)
}
// A stalled or failed check must never hold up the export's own feedback.
-const resolveNudgeWhileExporting = async (subject: ExportNudgeSubject): Promise<ExportNudgeCandidate | null> =>
- await withTimeout(resolveExportNudgeEligibility(subject), NUDGE_RESOLUTION_TIMEOUT_MS).catch((error) => {
+const resolveNudgeWhileExporting = async (
+ { logic }: NudgeModules,
+ subject: ExportNudgeSubject
+): Promise<ExportNudgeCandidate | null> =>
+ await withTimeout(logic.resolveExportNudgeEligibility(subject), NUDGE_RESOLUTION_TIMEOUT_MS).catch((error) => {
if (error instanceof PromiseTimeoutError) {
// Every other failure reports its own step. Without this one, a stalled check looks the
// same as an exporter who was ineligible.
- captureExportNudgeCheckFailed('timeout', exportNudgeEventProperties(subject))
+ logic.captureExportNudgeCheckFailed('timeout', logic.exportNudgeEventProperties(subject))
}
return null
})
@@ -113,14 +154,16 @@ export const foldNudgeIntoSettledToast = async (
action?: ToastButton
): Promise<void> => {
const candidate = await nudge.resolving
- if (!candidate) {
+ // A candidate exists only once the subscriptions code has loaded.
+ if (!candidate || !nudgeModules) {
return
}
+ const { logic, toast } = nudgeModules
if (!lemonToast.isActive(toastId)) {
- captureExportNudgeCheckFailed('toast-gone', exportNudgeEventProperties(candidate.subject))
+ logic.captureExportNudgeCheckFailed('toast-gone', logic.exportNudgeEventProperties(candidate.subject))
return
}
- const message = claimExportNudgeMessage(candidate)
+ const message = toast.claimExportNudgeMessage(candidate)
if (!message) {
return
}