Skip to content

⚡ Perf - Defer Application Insights init out of the critical window - #4917

Draft
isaaclombardssw wants to merge 2 commits into
mainfrom
perf/4903-defer-app-insights
Draft

⚡ Perf - Defer Application Insights init out of the critical window#4917
isaaclombardssw wants to merge 2 commits into
mainfrom
perf/4903-defer-app-insights

Conversation

@isaaclombardssw

Copy link
Copy Markdown
Member

Closes #4903

What

context/app-insight-client.tsx built the full @microsoft/applicationinsights-web SDK and called loadAppInsights() synchronously in a mount effect on every page, landing in the hydration window (contributes to TBT / long tasks), and the SDK shipped in the route's initial chunk.

  • Defer init to idle time: requestIdleCallback with a 5s timeout, falling back to setTimeout for Safari (< 17) that lacks it. Effect cleanup cancels the scheduled callback and unloads the SDK.
  • Dynamically import("@microsoft/applicationinsights-web") inside the deferred callback, so the SDK is no longer in the route's initial JS entry.
  • Buffer web-vitals metrics reported before the SDK is ready, and flush them in order on init, so telemetry measured during hydration (LCP/FCP/TTFB) is not lost.

The ReactPlugin (from the lightweight applicationinsights-react-js, already in the graph via the error handlers) is still created eagerly and provided via context, so error-boundary / global-error-handler keep working; only the heavy -web SDK is deferred. Server-side telemetry (serverExternalPackages: ["applicationinsights"]) is untouched.

Files

  • context/app-insight-client.tsx — defer + dynamic import + flush on load.
  • context/app-insights-web-vitals-buffer.ts — small shared buffer (trackWebVital / flushWebVitals / resetWebVitalsSink).
  • app/components/web-vitals.tsx — route metrics through the buffer instead of the context plugin directly.
  • context/app-insights-web-vitals-buffer.test.ts — jest test asserting buffered metrics replay in order once, then pass straight through.

Verified

  • npx jest on the buffer test — passes.
  • npx tsc --noEmit -p tsconfig.json — no new type errors (Tina client generated locally via tinacms dev).
  • npx prettier --check and npx eslint on the changed files — clean.
  • pnpm dev serves the homepage 200. Note: NEXT_PUBLIC_APP_INSIGHT_CONNECTION_STRING is unset locally, so client telemetry no-ops here — the actual arrival of page views / exceptions / web vitals in App Insights needs confirming against a deployed environment with the connection string set, and TBT improvement confirmed with a fresh Lighthouse run.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q8genJyZboXaa4vgYhJF4P

Construct and load the @microsoft/applicationinsights-web SDK during idle
time (requestIdleCallback with a timeout, setTimeout fallback for Safari)
instead of synchronously in the mount effect, and dynamically import() it so
it is no longer in the route's initial chunk graph. This keeps its work out
of the hydration window (TBT / long tasks).

Web-vitals metrics reported before the SDK is ready are buffered and replayed
on load via a small shared buffer, so telemetry measured before init (LCP,
FCP, TTFB) is not lost. Adds a jest unit test for the buffer/flush ordering.

Server-side telemetry (serverExternalPackages) is untouched.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q8genJyZboXaa4vgYhJF4P
@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Coverage report

St.
Category Percentage Covered / Total
🔴 Statements 0.28% 129/46850
🔴 Branches 6.19% 31/501
🔴 Functions 1.47% 7/476
🔴 Lines 0.28% 129/46850

Test suite run success

15 tests passing in 2 suites.

Report generated by 🧪jest coverage report action from 2c398c8

Add a case exercising flush -> resetWebVitalsSink -> re-buffer -> flush to a
fresh sink, verifying the StrictMode/remount safety property: only post-reset
metrics replay, once, and the old sink is never re-fired.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q8genJyZboXaa4vgYhJF4P
@isaaclombardssw

Copy link
Copy Markdown
Member Author

/deploy

@github-actions

Copy link
Copy Markdown
Contributor

Deployed changes to https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net

ℹ️ Staging slots are no longer created automatically - comment /deploy to deploy new commits.

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Lighthouse Report

🌐 URL ⚡ Performance ♿ Accessibility ✅ Best Practices 🔍 SEO 📦 Bundle Size 🗑️ Unused Bundle
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/ 28 100 59 69 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/articles 87 97 59 57 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/company/about-us 61 100 59 69 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/company/clients 88 95 59 69 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/company/contact-us 69 92 59 61 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/consulting 43 92 56 69 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/consulting/net-upgrade 64 99 56 61 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/consulting/web-applications 64 99 59 61 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/employment 63 95 59 69 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/events/angular-superpowers-tour 71 97 56 69 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/events/ai-workshop 95 91 56 61 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/events/ai-workshop-essentials 53 98 59 69 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/netug/sydney 70 94 56 61 0.00 MB 0.00 MB
https://app-sswwebsite-9eb3-pr-4917.azurewebsites.net/training/internship-fullstack 77 91 59 61 0.00 MB 0.00 MB

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 - Defer Application Insights init out of the critical window

1 participant