Skip to content

(perf) : defer secondary startup payloads#4345

Merged
koala73 merged 5 commits into
mainfrom
codex/issue-4339-defer-secondary-startup
Jun 22, 2026
Merged

(perf) : defer secondary startup payloads#4345
koala73 merged 5 commits into
mainfrom
codex/issue-4339-defer-secondary-startup

Conversation

@koala73

@koala73 koala73 commented Jun 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • defer dashboard Umami and Vercel Analytics until after load/paint/idle, with a bounded Umami queue and one script-load retry
  • remove eager dashboard head startup work for Umami, Sentry ingest preconnect, Clerk dns-prefetch, and Google Fonts
  • load narrowed dashboard fonts only when the variant/locale needs them, and add startup guards for analytics/auth/fonts/YouTube
  • bump scripts workspace undici to 7.28.0 to satisfy the production dependency audit gate

Fixes #4339

Validation

  • ./node_modules/.bin/tsx --test tests/secondary-startup.test.mts tests/signup-analytics-gate.test.mts tests/brief-thread-open-telemetry.test.mjs tests/sentry-defer-replay.test.mts tests/csp-filter.test.mjs tests/premium-fetch.test.mts tests/wm-session-interceptor-target.test.mts (147 tests)
  • npm run typecheck
  • npx playwright test e2e/secondary-startup.spec.ts
  • node .github/scripts/audit-production-dependencies.mjs --workspace "scripts" --package-json "scripts/package.json" --lockfile "scripts/package-lock.json"
  • git diff --check
  • pre-push hook via git push (typecheck/API typecheck/bundles/edge/markdown/MDX/proto/version sync)

Follow-up Notes

  • Greptile first-pass comments on the missing after-idle E2E assertion and Umami retry path were addressed in ac545f72a.
  • No screenshot attached because this is a startup/network behavior change; Playwright request assertions cover the visible-risk path.

@vercel

vercel Bot commented Jun 18, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
worldmonitor Ready Ready Preview, Comment Jun 22, 2026 6:53pm

Request Review

@greptile-apps

greptile-apps Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR defers all secondary dashboard startup work — Umami analytics, Vercel Analytics, Google Fonts, Sentry preconnect, and Clerk dns-prefetch — until after first paint and browser idle, so a slow or unreachable third-party origin cannot contend with the initial dashboard shell load. A bounded 50-item queue buffers track/identify calls made before the Umami script arrives, replaying them on script load.

  • src/bootstrap/secondary-startup.ts (new): double-rAF → requestIdleCallback scheduler; variant/locale-aware font URL builder that skips Google Fonts entirely for the default English dashboard.
  • src/services/analytics.ts: replaces the eager inline <script> with a deferred script injector and a pre-load queue; initAnalytics() is now async and safe to call before the DOM settles.
  • index.html: removes the Umami <script async> tag, Sentry preconnect, Clerk dns-prefetch, and the eager Google Fonts stylesheet; CSP entries for the deferred loaders are retained.

Confidence Score: 4/5

Safe to merge; the deferred-load architecture is sound and the existing analytics path is preserved through the pre-load queue.

The deferred scheduling and font narrowing logic are well-tested at the unit level. Two gaps remain: the E2E spec installs __wmRunDeferredIdle but never invokes it, so no test confirms deferred requests actually fire after idle; and the Umami script error handler resets umamiLoadStarted without any wired retry path, leaving queued events stranded on a script load failure.

e2e/secondary-startup.spec.ts (missing after-idle assertion) and src/services/analytics.ts (orphaned umamiLoadStarted reset in the error handler)

Important Files Changed

Filename Overview
src/bootstrap/secondary-startup.ts New module implementing deferred scheduling via double-rAF → requestIdleCallback; cleanly guards against double-scheduling; font narrowing logic and Vercel analytics lazy-import look correct.
src/services/analytics.ts Bounded queue + deferred Umami loader is a good pattern; the script-error handler resets umamiLoadStarted but no retry path is wired, leaving queued events stranded on load failure.
e2e/secondary-startup.spec.ts Only tests the "before idle" guard; __wmRunDeferredIdle() is wired but never called, so the "after idle" side of the contract is untested.
index.html Removes eager Umami script, Sentry preconnect, Clerk dns-prefetch, and Google Fonts head entries; CSP entries for the deferred loaders are retained; looks correct.
src/main.ts Replaces eager Vercel Analytics inject with deferred initVercelAnalytics(); adds initAnalytics() for Umami; straightforward change.
src/App.ts Adds initDeferredDashboardFonts() after initI18n() so lang/dir are resolved before the font URL is built; timing looks correct.
tests/secondary-startup.test.mts Unit tests cover font URL narrowing and the queue/flush deferred-Umami flow with a fake DOM; assertions are thorough for the unit-level behavior.

Reviews (1): Last reviewed commit: "perf(dashboard): defer secondary startup..." | Re-trigger Greptile

Comment on lines +29 to +39
page.on('request', (request) => {
const url = request.url();
if (SECONDARY_STARTUP_REQUEST.test(url)) secondaryRequests.push(url);
});

await page.goto('/');
await page.locator('.auth-signin-btn, .panel').first().waitFor({ timeout: 20000 });
await page.waitForTimeout(250);

expect(secondaryRequests).toEqual([]);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 __wmRunDeferredIdle is set up but never called — test only verifies half the contract

The init script installs __wmRunDeferredIdle as a manual trigger for queued idle callbacks, but no test case ever calls page.evaluate(() => window.__wmRunDeferredIdle()). The single test verifies that secondary requests are not made before idle runs, but there is no corresponding assertion that the analytics, fonts, Clerk, and YouTube requests are made once idle fires. A regression that silently dropped the deferred loader (e.g. an uncaught promise rejection in scheduleAfterFirstPaint) would leave this test green while the feature stops working entirely.

Comment thread src/services/analytics.ts
Comment on lines +140 to +143
script.addEventListener('error', () => {
umamiLoadStarted = false;
}, { once: true });
document.head.appendChild(script);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Script error handler resets umamiLoadStarted but no retry path exists

When the Umami script fails to load, umamiLoadStarted is reset to false — which allows loadUmamiScript() to run again — but nothing will ever call it a second time: umamiLoadScheduled stays true, so initAnalytics() treats the loader as already wired and returns early. The pendingUmamiCalls queue accumulates events that will never be drained until a page reload. The reset of umamiLoadStarted appears to signal a retry path that was not completed.

@koala73 koala73 changed the title perf(dashboard): defer secondary startup payloads (perf) : defer secondary startup payloads Jun 19, 2026
…alic

- sendUmamiCall: return false in the catch so a throwing window.umami
  defers to the bounded queue/flush path instead of silently dropping the
  event (callers only queue when sendUmamiCall returns false).
- buildDashboardFontStylesheetHref: restore the Nunito italic axis
  (ital,wght@0,400;0,600;0,700;1,400) the narrowed request dropped;
  happy-theme.css uses font-style:italic on Nunito-bodied elements, so the
  weight-only request regressed those to synthesized oblique. Weight
  narrowing (no 300) preserved.
- Update the two font-href assertions that had locked in the regression.

Addresses ce-code-review findings on PR #4345.

Claude-Session: https://claude.ai/code/session_0187W7sB8cdp8sYK7J7qDNLn
…up tests

Code-review follow-ups for #4345:
- scripts/package.json: re-add overrides.undici "$undici" to match main
  (post-#4350). scripts/ is now byte-identical to main, clearing the husky
  pre-push lockfile-sync gate and re-pinning transitive undici. Lockfile is
  unchanged (already dedupes to 7.28.0).
- analytics.ts: fix the loadUmamiScript existing-script branch (flush only
  when window.umami is set, else attach the load listener; set
  umamiLoadStarted to close the re-entry gap); make initAnalytics synchronous
  (no await, sole caller voids it); add resetAnalyticsForTesting; move the
  #4183 data-domains note next to UMAMI_DOMAINS as the single source of truth.
- index.html: trim the Umami comment to a pointer at the new source of truth.
- tests: add coverage for scheduleAfterFirstPaint load-listener + rIC-absent
  fallback branches, Umami retry-exhaustion, 50-cap queue overflow,
  throw-then-queue-then-flush, and the existing-script branch.
- e2e: replace the flaky 250ms wait with the deterministic
  wmEventHandlersReady readiness gate.

Claude-Session: https://claude.ai/code/session_015dspobi7rUanvd9zSamz8a
@koala73
koala73 merged commit 00676f0 into main Jun 22, 2026
22 of 23 checks passed
@koala73
koala73 deleted the codex/issue-4339-defer-secondary-startup branch June 22, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(dashboard): defer secondary auth, analytics, and font payloads

1 participant