Chat drawer: warm Element in the background so the first open is as fast as a reopen
Problem and intended result
Opening the chat drawer (Matrix mode, apps/blog/components/matrix-chat-widget.tsx) for the first time in a page session takes 5.5–10 s before Element shows #general. A reopen takes 0.5 s. Measured 2026-10-23 on the integration site, with guest4test and a cold cache (#1307 (closed)):
- SSO chain, 0.9–2 s:
denser-session.html→ Synapse SSO redirect → denser/oauth/authorize→ Synapse OIDC callback → Element?loginToken. - Element download and start, about 2.3 s. The cold load is 5.1 MB over 78 requests.
- Element crypto start, about 2 s. The rust-crypto wasm is 2 MB zstd.
- First sync and room, about 0.3 s.
From Poland (134 ms RTT) the same flow takes 9–10 s.
Intended result: for a signed-in, chat-eligible user, Element is already loaded and signed in when they open the drawer. The first open then takes about as long as a reopen (under 1 s), and a user who never opens chat doesn't pay an unreasonable cost.
Scope
- Warm-up. Mount the Element iframe hidden, ahead of the first open:
- when the browser is idle after the page has loaded (
requestIdleCallback, with a timeout fallback), a few seconds after load; - immediately on pointer-enter, focus or pointer-down of the chat button, whichever comes first.
- when the browser is idle after the page has loaded (
- Opening the drawer must reuse the warmed iframe, not reload it. Moving an iframe element in the DOM reloads it. Keep it in one place and toggle its visibility and position, or give the drawer a stable container for it. Element must lay out correctly when it's shown. A
display:noneiframe can give Element a zero-size layout, so prefer an off-screen orvisibility:hiddenframe of the drawer's size. - Setting
MATRIX_CHAT_PRELOAD(public env, read like the otherMATRIX_*settings inpackages/ui/config/chat.ts):idle(default): idle and button intent, as above;intent: on button intent only;off: today's behaviour.
- Don't warm when
navigator.connection.saveDatais true; button intent still loads. - Today's session rules stay as they are:
- the session page ends another user's session;
- a theme change reloads the session page;
- sign-out or an account switch loads the session page without a user.
- A warmed frame of a user who has since signed out or switched account must be dropped as today's opened frame is.
- The hidden frame must not be reachable by keyboard or screen readers until the drawer is open:
aria-hidden,tabIndex=-1,inertwhile hidden. - Cache headers in the repo's stack: put the long-lived caching now hand-applied on the host into
stack/matrix/caddy/Caddyfile. In thechat.site block:@hashed path /bundles/* /fonts/* header @hashed >Cache-Control "public, max-age=31536000, immutable"/,config.jsonanddenser-session.htmlkeep their current headers. Don't add the per-request JSON access log that is temporarily enabled on the host. - Add one line to
stack/matrix/README.md: the host uses BBR congestion control (/etc/sysctl.d/90-bbr.conf, plussysctls:on the router's haproxy service) because clients in Poland are 134 ms away.
Verification
- Fixture spec(s) in
apps/blog/playwright/tests/fixture/chatWidget.spec.ts(stub Element's origin as the existing specs do):- with the default setting, the iframe request starts without a click, after idle;
- with
intent, it starts on hover of the chat button and not before; - with
off, it starts only on opening the drawer; - opening the drawer after a warm-up makes no second request for the session page (same frame);
- sign-out drops the warmed frame and loads the session page without a user.
- Unit test for the
MATRIX_CHAT_PRELOADparsing. - After landing, the operator deploys
stack/matrixto the host and measures the drawer's first open live. Target: under 1 s after an idle warm-up.