[unplugin] Vite dev: CSS for a lazily loaded module is applied after the module has evaluated
Describe the issue
With @stylexjs/unplugin under the Vite dev server, a module that uses StyleX and is loaded after the page has started does not have its CSS applied when it evaluates. The styles show up somewhere between a few milliseconds and ~150ms later.
This is because in dev the CSS is delivered outside Vite's module graph:
transformIndexHtmlinjects the runtime script and a<link>to/virtual:stylex.css(vite.js#L122).- The runtime fetches the collected CSS once on page load (
consts.js#L64). It fetches again only when astylex:css-updateHMR event arrives (consts.js#L67). - The server sends that event from a
setIntervalthat checks the rule store every 150ms (vite.js#L80).
Vite compiles modules when the browser first requests them. So a module loaded after the page starts has its rules collected after the runtime's first fetch. Those rules reach the page one poll tick, one WebSocket message and one fetch later, by which point the module has already evaluated and rendered.
In practice:
- Apps: lazily loaded routes and components (
React.lazy, code-split chunks) flash unstyled. Anything that measures layout on mount, such asuseLayoutEffect, positioning or virtualisation, reads unstyled values. - Browser tests (Vitest browser mode, Storybook interaction tests) intermittently assert against missing styles. We hit this as a flaky CI failure: a Storybook
playfunction readpadding: 0pxand an undeclareddefineVarscustom property. - Storybook: switching to a story whose file hasn't loaded yet renders it without its StyleX CSS. In a real Storybook 10 project this happened in 4 of 5 cold starts, unstyled for 11–122ms. The
playfunction runs inside that window.
Expected behavior
Once a module that calls stylex.create has evaluated, its styles are applied. That's what Vite's own CSS handling guarantees: an imported stylesheet is injected before the importing module runs.
Steps to reproduce
@stylexjs/unplugin 0.19.1 and @stylexjs/stylex 0.19.1. The dev runtime on main as of fe0be7f is unchanged. Also: Vite 8.3.1, Vitest 5.0.2 with @vitest/browser-playwright, Chromium (Playwright headless shell 153), Node 26.7.0, macOS.
Clone https://github.com/tom-sherman/stylex-dev-css-race, then run
pnpm installandpnpm exec playwright install chromium.Run
pnpm dev, open the page and click the button. It lazily imports a module with onestylex.createstyle (paddingTop: '16px'), renders an element with it, readspadding-topstraight away, then logs when the style actually lands. Restart the dev server before each attempt (see the note below).Or headless:
pnpm repro 10does the same against a fresh dev server in a fresh Node process each time:run 1: padding-top when the module evaluated: 0px FAIL (styles applied 149ms after the import started) run 2: padding-top when the module evaluated: 0px FAIL (styles applied 142ms after the import started) run 3: padding-top when the module evaluated: 16px ok ... 6/10 runs had no StyleX CSS when the module evaluatedOr with Vitest browser mode:
pnpm testfails every time:AssertionError: expected '0px' to be '16px' // Object.is equality
Note: the rule store lives on globalThis.__stylex_unplugin_store (core.js#L259) and survives server.close(). A second dev server in the same Node process already knows the module's rules, so it never shows the race. That's why the repro script uses a new process for every run.
Test case
https://github.com/tom-sherman/stylex-dev-css-race
src/styled.ts: the StyleX modulesrc/main.ts: lazy import on click, then checks the computed styletest/styled.test.ts: the same check as a Vitest browser testrepro.mjs: headless runner, one cold server per run
Additional comments
Pushing stylex:css-update right after a transform, instead of polling every 150ms, would narrow the window but not close it, because the fetch is still asynchronous relative to module evaluation. Closing it needs the dev CSS to go through Vite's module graph. For example, a StyleX-transformed module could import a virtual CSS module, so that Vite injects the styles before the importer runs, just as it does for any imported .css file.
Our workaround for now: before running stories, the test setup fetches /virtual:stylex.css and waits until #__stylex_virtual__ contains the same CSS. That depends on plugin internals, and it doesn't help apps or the Storybook UI.
Source: facebook/stylex