Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#1919·stylex

[unplugin] Vite dev: CSS for a lazily loaded module is applied after the module has evaluated

Author: tom-shermanCreated Sep 29, 2026Updated Sep 29, 2026
Labelsbug

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:

  1. transformIndexHtml injects the runtime script and a <link> to /virtual:stylex.css (vite.js#L122).
  2. The runtime fetches the collected CSS once on page load (consts.js#L64). It fetches again only when a stylex:css-update HMR event arrives (consts.js#L67).
  3. The server sends that event from a setInterval that 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 as useLayoutEffect, 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 play function read padding: 0px and an undeclared defineVars custom 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 play function 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.

  1. Clone https://github.com/tom-sherman/stylex-dev-css-race, then run pnpm install and pnpm exec playwright install chromium.

  2. Run pnpm dev, open the page and click the button. It lazily imports a module with one stylex.create style (paddingTop: '16px'), renders an element with it, reads padding-top straight away, then logs when the style actually lands. Restart the dev server before each attempt (see the note below).

  3. Or headless: pnpm repro 10 does 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 evaluated
  4. Or with Vitest browser mode: pnpm test fails 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 module
  • src/main.ts: lazy import on click, then checks the computed style
  • test/styled.test.ts: the same check as a Vitest browser test
  • repro.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

View original on GitHubView discussion on GitHub