perf(dev): opt-in production React under the Vite dev server + route warmup

Merged
AL
2mo ago
+167-3

Summary

Two experimental, env-gated dev-server knobs, both off by default, from profiling dev-mode runtime latency (not compile time) on a larger app: 68 nav links, six pages of 120 cards, a 300-row table. Full write-up with method, trace attribution and per-lever numbers: https://claude.ai/code/artifact/8d136694-b64f-487c-9de9-30b350d03778 (private artifact; ask Alek for access). The short version:

per navigation (medians, headless Chrome, M5 Pro)click → settlemain-thread CPUJS (trace)first visit to a route
dev, main (owner-stack pin on)31.9 ms54.1 ms33.8 ms105–131 ms
REFLEX_DEV_PROD_REACT=117.6 ms35.6 ms15.9 ms87–126 ms
REFLEX_VITE_WARMUP_ROUTES=129.6 ms51.6 ms(noise)43–69 ms
both18.6 ms36.2 ms11.6 ms20–32 ms
--env prod build9.6 ms23.5 ms9.6 ms18–25 ms

The backend is not on the path (1–2 ms round trip in both modes); the whole dev/prod gap is JavaScript on the renderer main thread, i.e. React's development build doing per-element work plus StrictMode's double render.

REFLEX_DEV_PROD_REACT=1

Prebundles the browser's React (react, react-dom, the JSX runtimes, scheduler) from the packages' production files, via a resolve plugin in optimizeDeps.rolldownOptions.plugins. That scope is deliberate; the obvious alternatives all fail:

  • NODE_ENV=production for the dev server: the server renderer crashes, because the dev JSX transform imports jsxDEV, which production React does not export.
  • NODE_ENV=production for prebundled deps only: nothing renders, because react-refresh also switches on it and becomes an empty module.
  • resolve.alias to the production files: SSR breaks, because Vite refuses to externalize any server import that matches an alias and then evaluates the CJS file as ESM. Per-environment aliases are overwritten by the global list (resolveEnvironmentResolveOptions).

optimizeDeps is a client-environment option, so only the browser prebundle sees the plugin; SSR keeps resolving React from Node and every other dependency keeps its development build. JSX is compiled with oxc.jsx.development: false so no source imports jsxDEV. Plugins are excluded from the optimizer's cache key, so a harmless define is added to invalidate prebundled deps when the knob toggles. Absolute paths because React's exports map does not expose cjs/.

Trade-off: Fast Refresh silently no-ops against a production renderer (react-refresh requires scheduleRefresh, which only dev react-dom injects), so the knob also enables the existing fullReload() plugin. Measured edit-to-DOM is unchanged (~1.9 s, dominated by the Python recompile) but it is a page reload rather than an in-place patch, so browser-only state such as scroll position is lost. StrictMode becomes a no-op under production React, so this knob also covers the react_strict_mode cost (−22% JS/nav on its own with dev React).

Verified live: served react.js / react-dom_client.js carry no dev markers, no console errors, navigation and events work, edit → full reload → new text in DOM.

REFLEX_VITE_WARMUP_ROUTES=1

server.warmup.clientFiles for app/routes/**/*.jsx, so Vite transforms route modules at startup instead of on first visit (~75 ms per route on demand). Opt-in because the work is proportional to page count; a route-count threshold would be a reasonable default later.

Tests

test_vite_config_template_prod_react / _warmup_routes (both states) and test_compile_vite_config_prod_react_forces_full_reload. Full unit suite green locally.

🤖 Generated with Claude Code