Skip to content

perf: lazy-load firebase auth and label dictionaries, memoize i18n context - #6

Merged
Sebas1705 merged 1 commit into
mainfrom
perf/phase2-frontend
Nov 12, 2026
Merged

Sebas1705 merged 1 commit into
mainfrom
perf/phase2-frontend

Conversation

@Sebas1705

Copy link
Copy Markdown
Contributor

Summary

Frontend performance, phase 2 (findings E1-E3 plus the diagnostics polling item from the runtime review).

  • Lazy Firebase auth: firebase/app and firebase/auth are loaded with dynamic import() only when VITE_AUTH_MODE=firebase and the config is complete. BackofficeAuth.mode stays synchronous (derived from config), so LoginGate is unchanged. In dev auth mode the SDK is never downloaded. A failed chunk load can be retried; unsubscribing before the SDK arrives cancels the subscription.
  • Lazy label dictionaries: labels.ts keeps only en (the language hard-coded in App.tsx, also the fallback). es, fr, de, it moved to i18n/dictionaries/* and load on demand through loadDictionary; I18nProvider shows English until the chunk arrives and caches loaded dictionaries. Every language in UiLanguage stays reachable through the provider; nothing was removed. The running app cannot switch language today (setLanguage is a no-op), so in production only English is ever requested.
  • Memoized i18n context: useMemo for the value, hoisted stable setLanguage.
  • AIDiagnosticsPanel: the raw 10 s setInterval is replaced by useVisibilityPolling (pauses while the tab is hidden, refreshes on return, no overlapping runs).

Bundle size (vite build, Vite 6.4.5)

main (before) this PR (after)
Entry index-*.js 467.88 kB (gzip 119.45 kB) 212.93 kB (gzip 66.30 kB)
Firebase app+auth inside entry separate chunks (197.37 + 50.52 + 1.31 kB; gzip 39.48 + 12.49 + 0.75), not preloaded, fetched only in firebase mode
es / fr / de / it inside entry 31.24 / 22.39 / 22.03 / 20.73 kB (gzip 8.86 / 6.36 / 6.55 / 6.02), fetched only if selected

Initial JS in dev auth mode: gzip 119.45 kB -> 66.30 kB (-53 kB, -45 %). Entry index.html has no modulepreload for the new chunks. In firebase mode the SDK now loads in a second request after the entry instead of inside it.

Behaviour notes

  • Fallback for missing keys is now English (previously Spanish). Only fr/de/it are partial and none is reachable in the app today.
  • With a non-English language there is a brief English flash until its chunk loads.

Tests

  • New auth-lazy-firebase.test.ts: dev mode never loads the SDK, firebase mode loads it on first use, session forwarding, unsubscribe, dispose-before-load.
  • i18n.smoke.test.tsx: stable context value across re-renders, async load of a language, English fallback, sync English.
  • ai-diagnostics-panel.integration.test.tsx: 10 s refresh, paused while hidden, immediate refresh on return.
  • test/setup.ts preloads the Spanish dictionary so the existing Spanish integration tests stay synchronous.
  • service-console-panel.integration ("summarizes AI telemetry metrics"): it also fails on pristine main under Node 24 on a machine with an es-ES locale, because the panel formats with the runtime locale (95,2%) while the test hard-coded 95.2%. Test bug, fixed here by building the expectation with the same toLocaleString.

Local results: npm ci, tsc -b, vite build OK; vitest 19 files / 154 tests pass (144 before; 143 passed + that 1 locale failure on main); coverage thresholds pass (lines 93.3, branches 85.4). There is no lint script in this package. The Playwright e2e could not run here (installed Chromium does not match the Playwright version, and downloading browsers was not done).

Overlaps with open PRs

None in source. #4 touches Dockerfile/CI files, #5 touches docs/PR template, #3 touches service-overview-panel.integration.test.tsx and .env.example (this PR does not edit those).

…ntext

- load firebase/app and firebase/auth with dynamic import only when real Firebase auth is configured
- keep English bundled and load es/fr/de/it dictionaries on demand
- memoize the I18nProvider context value and hoist the no-op setLanguage
- use useVisibilityPolling for the AI diagnostics generator status refresh
- make the service console metrics test independent of the runtime locale
@Sebas1705
Sebas1705 merged commit 2bb910b into main Nov 12, 2026
6 checks passed
@Sebas1705
Sebas1705 deleted the perf/phase2-frontend branch November 12, 2026 18:15
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.

1 participant