#1515·ripple

SSR drops a style block's CSS when its $class or a class entry is read outside a render

Author: leonidazCreated Sep 24, 2026Updated Sep 24, 2026

Issue Category

Behavioral Bug

Bug Description

If a style block's $class or a class entry is read outside a render, the SSR response doesn't include that block's CSS. Module-level destructuring (const { $class: cls } = theme;) and module-level reads (const cls = theme.$class;) are the usual cases. The server-rendered element still carries the hash class, so the page renders unstyled until the client bundle loads the stylesheet, and a page served without client JS stays unstyled.

typescript
export const theme = <style>div { color: red; }</style>;
const { $class: cls } = theme;
export function App() @{ <div class={cls}>Probe</div> }

Expected: render(App) returns the theme's hash in css, and getCss(css) is div.<hash> { color: red; }. Actual: css is empty, while the body is <div class="tsrx-f9bda849">Probe</div>.

Reproduction Steps

At 83ed56159 (current main), after pnpm install --frozen-lockfile, run this from the repository root:

bash
mkdir -p tmp/ssr-module-read && cat > tmp/ssr-module-read/run.mjs <<'JS'
import { writeFileSync } from 'node:fs';
import { compile } from '../../packages/tsrx-ripple/src/index.js';
const { render, getCss } = await import('../../packages/ripple/src/server/index.js');
const cases = {
  'module-level $class destructure': `export const theme = <style>div { color: red; }</style>;
const { $class: cls } = theme;
export function App() @{ <div class={cls}>Probe</div> }`,
  'module-level $class read': `export const theme = <style>div { color: red; }</style>;
const cls = theme.$class;
export function App() @{ <div class={cls}>Probe</div> }`,
  'module-level class entry': `const styles = <style>.card { color: red; }</style>;
const { card } = styles;
export function App() @{ <div class={card}>Probe</div> }`,
  'control: $class read in the template': `export const theme = <style>div { color: red; }</style>;
export function App() @{ <div class={theme.$class}>Probe</div> }`,
};
let i = 0;
for (const [name, source] of Object.entries(cases)) {
  const { code } = compile(source, 'App.tsrx', { mode: 'server' });
  const file = new URL(`./App${i++}.server.mjs`, import.meta.url);
  writeFileSync(file, code.replaceAll("'ripple/internal/server'", "'../../packages/ripple/src/runtime/internal/server/index.js'"));
  const { App } = await import(file.href);
  const { body, css } = await render(App);
  console.log(name.padEnd(38), JSON.stringify({ body, cssHashes: [...css], cssText: getCss(css) }));
}
JS
node tmp/ssr-module-read/run.mjs

Output:

module-level $class destructure        {"body":"<!--[--><div class=\"tsrx-f9bda849\">Probe</div><!--]-->","cssHashes":[],"cssText":""}
module-level $class read               {"body":"<!--[--><div class=\"tsrx-f9bda849\">Probe</div><!--]-->","cssHashes":[],"cssText":""}
module-level class entry               {"body":"<!--[--><div class=\"tsrx-8b3ff9af card\">Probe</div><!--]-->","cssHashes":[],"cssText":""}
control: $class read in the template   {"body":"<!--[--><div class=\"tsrx-f9bda849\">Probe</div><!--]-->","cssHashes":["tsrx-f9bda849"],"cssText":"div.tsrx-f9bda849 { color: red; }"}

The theme is exported, so the current @tsrx/core keeps all of its selectors; pruning isn't why the CSS is missing.

Environment

  • OS: macOS 26.6.2
  • Browser: n/a (server render)
  • Ripple Version: 0.4.7 (main at 83ed56159, @tsrx/core 0.3.3)
  • Node Version: 24.18.0

Additional Context

Cause. On the server, an assigned block's class-map object registers CSS lazily. Every entry, $class and each class key, is a getter that calls _$_.output_register_css(hash) before returning the class string (build_style_class_map_expression). output_register_css drops the call when no render is active, and its comment says so. So a read at module scope runs once at import time and registers nothing, and later renders use the plain string without touching the getter. The same applies to a value read in one render and cached for later requests.

Why it matters more soon. tsrx-org/tsrx#251 makes every assigned block keep all of its selectors (tsrx-org/tsrx#248, #249), because $class is an ordinary string that JavaScript can carry anywhere. After Ripple adopts that @tsrx/core, the client CSS for these patterns will be correct, so this SSR gap becomes the remaining reason such pages render unstyled on first paint.

Fix direction. Tie registration to where the string can end up, not to the read:

  • Record hashes read outside a render in a module-level "always" set, and merge that set into every request's css. A string read at import time can reach any render, so each request needs the sheet.
  • Or have each component's registration prelude also register the sheets of the assigned blocks declared in its module (and of imported ones it references). This is cheaper to reason about, but it misses a string that crosses modules through a plain export (export const cls = theme.$class).

Checklist

  • I've searched for existing issues
  • I'm willing to help implement a fix