Skip to content

perf: Load the Kapa.ai widget on demand - #20278

Merged
coolguyzone merged 3 commits into
masterfrom
coolguyzone/perf/defer-kapa
Nov 2, 2026
Merged

coolguyzone merged 3 commits into
masterfrom
coolguyzone/perf/defer-kapa

Conversation

@coolguyzone

@coolguyzone coolguyzone commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

DESCRIBE YOUR PR

Preview on: https://sentry-docs-git-coolguyzone-perfdefer-kapa.sentry.dev/
In preview the mobile Lighthouse performance score went from 55 to 88.

The Kapa.ai widget was loading on every page view. The bundle is ~600 KiB and pulls reCAPTCHA Enterprise in behind it — together roughly 1.3 MB of JavaScript and ~1.08s of script bootup on every visit, for a feature almost nobody opens. It was already strategy="lazyOnload", but that only defers past window.load; it still ran every time.

This replaces the eager <Script> in app/layout.tsx with an on-demand loader (src/lib/kapa.ts). The widget is fetched the first time someone reaches for Ask AI, and pre-warmed on hover/focus so the click still feels instant.

There are four Ask AI entry points, and all four needed rewiring. The first two previously worked through Kapa's own data-modal-override-open-class="kapa-ai-class" delegation, which can't fire if the widget was never loaded — so that class is gone and every entry point now calls openKapa() directly:

  • Header button (header.tsx)
  • Desktop search-bar button (search/index.tsx)
  • #ai-list-entry row in the search results, which passes {query, submit: true} (search/index.tsx)
  • The ?askAI=true deep link (askAiSearchParams.tsx)

This also fixes two latent bugs. The deep link waited a fixed 300ms and then silently did nothing if the widget wasn't ready yet; the search-results entry was wrapped in if (window.Kapa?.open) and was a no-op in the same situation. Both now await the loader, so neither races a timeout.

Verification

Tested in headless Chrome against a local dev server, all four paths confirmed with screenshots:

  • On page load: 0 Kapa requests, 0 reCAPTCHA requests, window.Kapa undefined, 0 console errors
  • On hover: bundle fetches and window.Kapa becomes available
  • On click: modal opens with branding, disclaimer, and example questions intact; the search path passes the query through and submits it; mobile works at 390px
  • On reopen: exactly one <script src*="kapa"> in the DOM, so no duplicate injection

Measured on master under identical local conditions, page load pulled 9 third-party requests / 764 KiB (6 of them reCAPTCHA, 730 KiB). On this branch that is zero until someone opens Ask AI.

pnpm lint:ts, ESLint and Prettier are clean, and all 517 tests pass.

Notes for reviewers

  • Please exercise all four entry points in the preview, including ?askAI=true and the "Ask Sentry about …" row in search results — those two are the ones a quick smoke test would miss.
  • I can't quote a new Lighthouse score from a dev build. Worth running Lighthouse against the Vercel preview to get the real TBT movement.
  • https://proxy.kapa.ai/ returns 404 on localhost. It is not a regression from this change — on master it fires on page load, here it only fires after Ask AI opens, and production doesn't 404 at all. It's Kapa's origin whitelist not covering localhost.

IS YOUR CHANGE URGENT?

Help us prioritize incoming PRs by letting us know when the change needs to go live.
Select exactly one option. For deadlines, replace YYYY-MM-DD with the due date. You can update this information later by editing the PR description.

  • Urgent deadline (GA date, etc.): YYYY-MM-DD
  • Other deadline: YYYY-MM-DD
  • No deadline: Not urgent, can wait up to 1 week+

SLA

  • Teamwork makes the dream work, so please add a reviewer to your PRs.
  • Please give the docs team up to 1 week to review your PR unless you've supplied a deadline.

Thanks in advance for your help!

PRE-MERGE CHECKLIST

Make sure you've checked the following before merging your changes:

  • Checked Vercel preview for correctness, including links
  • PR was reviewed and approved by any necessary SMEs (subject matter experts)
  • PR was reviewed and approved by a member of the Sentry docs team

EXTRA RESOURCES

🤖 Generated with Claude Code

The widget bundle is ~600 KiB and pulls reCAPTCHA Enterprise in behind it.
Together they accounted for roughly 1.3 MB of JavaScript and ~1.08s of script
bootup on every page load, even though almost nobody opens Ask AI. It was
already strategy="lazyOnload", which only defers it past window load; it still
ran on every visit.

Replace the eager <Script> with an on-demand loader. Each Ask AI entry point
now fetches the widget on first use and pre-warms on hover/focus, so the click
still feels instant.

This also removes two latent races. The ?askAI=true deep link previously waited
a fixed 300ms and silently did nothing if the widget wasn't ready; the search
results AI entry was guarded by `if (window.Kapa?.open)` and was a no-op in the
same case. Both now await the loader.

Verified in headless Chrome: zero kapa/reCAPTCHA requests on load, and all four
entry points (header, search bar, search results with query passthrough, and the
?askAI=true deep link) open the modal correctly on desktop and mobile.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@vercel

vercel Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
develop-docs Ready Ready Preview Nov 2, 2026 7:40pm UTC
sentry-docs Ready Ready Preview Nov 2, 2026 7:40pm UTC

Request Review

@github-actions github-actions Bot added the Priority: Normal Docs review has no urgent deadline label Oct 27, 2026
@cursor

cursor Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Services awaiting deployment · Rollout details

Status Service Environment
⚪ Not deployed yet develop-docs
⚪ Not deployed yet sentry-docs

Mention @rollout in a comment to update the plan.

@cursor cursor Bot left a comment

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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 18e8779. Configure here.

Comment thread src/components/askAiSearchParams.tsx Resolved
The ?askAI=true effect called openKapa with no cancellation. Since the
widget can take seconds to load, leaving the homepage mid-load would pop
the modal over the next page. openKapa now takes an AbortSignal and skips
the open once aborted, while still letting the load finish so the next
click stays fast.

The Ask AI Referrer event now fires only after the modal actually opens,
as it did before the lazy-load change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@sfanahata sfanahata left a comment

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.

Looks good!

@coolguyzone
coolguyzone merged commit 0787a43 into master Nov 2, 2026
31 checks passed
@coolguyzone
coolguyzone deleted the coolguyzone/perf/defer-kapa branch November 2, 2026 22:54
@cursor

cursor Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Rollouts can't watch this change after it deploys: every data source for this team needs to be signed in again, so a check has no telemetry to read. Sign in to your data sources, or ask a team admin to.

sentry-junior Bot added a commit that referenced this pull request Nov 3, 2026
Resolve the import conflict in src/components/search/index.tsx: keep
isDeveloperDocs from sentry-docs/docsSite.mjs and the new Kapa imports
from #20278.

Co-Authored-By: Sergiy Dybskiy <3760543+sergical@users.noreply.github.com>

This branch was successfully deployed

2 active deployments
Preview – sentry-docs — 8628ff23 Deployed Nov 2, 2026 by vercel[bot]
Preview – develop-docs — 8628ff23 Deployed Nov 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: Normal Docs review has no urgent deadline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants