#1521·ripple

[Bug]: An inline `<script>` in `<head>` runs again after hydration, and the client drops its attributes (`type="module"`, `type="application/json"`)

Author: leonidazCreated Sep 26, 2026Updated Sep 26, 2026
Labelsbug

[!NOTE] This is a bug today, with Ripple's pinned @tsrx/core 0.3.3. It doesn't depend on the upgrade to @tsrx/core 0.5.0 (#1520) and can be fixed now.

Issue Category

Behavioral Bug

Bug Description

A <script> is only allowed in <head>. The client adds one that has a body with _$_.script, which creates a new <script> element and inserts it, so the browser runs it. That path has two bugs:

  • (a) After server rendering, hydration adds a second copy of the script and runs it again. The server's <script> already ran when the browser parsed the page. Hydration leaves it in place and inserts a new one before it, so the code runs twice and <head> ends up with both scripts.

  • (b) The client drops the script's attributes. Only the body reaches _$_.script, so the script the client inserts has no type, id, nonce, or any other attribute, static or dynamic:

    • type="module" runs as a classic script and fails with Cannot use 'import.meta' outside a module;
    • type="application/json" is executed as JavaScript and fails with Unexpected token ':';
    • nonce={nonce} is dropped as well (checked in the compiled output only), which matters for a page with a nonce-based CSP.

    The server output keeps all of them.

(b) applies to a client-only mount and to hydration alike.

Expected:

  • Hydration adopts the server's <script> without inserting or running a copy: <head> keeps one script per <script> element, and its code runs once.
  • The script the client inserts has the authored attributes, as the server's does: a module runs as a module, and a JSON script is data, not code.

Reproduction Steps

From the repository root at 83ed5615968364a077cbd39bc26a74e4d8c12b11, after pnpm install:

bash
cd packages/tsrx-ripple
node --input-type=module <<'JS'
import { compile } from './src/index.js';

const source = `export function App() @{
	<>
		<head>
			<script>window.runs = (window.runs ?? 0) + 1;</script>
			<script type="module">window.meta = typeof import.meta;</script>
			<script type="application/json" id="data">{"a": 1}</script>
		</head>
		<p>{'body'}</p>
	</>
}`;

for (const mode of ['client', 'server']) {
	const { code } = compile(source, 'App.tsrx', { mode });
	const lines = code.split('\n').filter((line) => line.includes('_$_.script(') || line.includes('<script'));
	console.log(`// ${mode}`);
	console.log(lines.map((line) => line.trim()).join('\n'));
}
JS

Output:

// client
_$_.script(script, 'window.runs = (window.runs ?? 0) + 1;');
_$_.script(script_1, 'window.meta = typeof import.meta;');
_$_.script(script_2, '{"a": 1}');
// server
__out += '<!--3b9209d9--><script>window.runs = (window.runs ?? 0) + 1;</script><script type="module">window.meta = typeof import.meta;</script><script type="application/json" id="data">{"a": 1}</script>';

The client passes only each body; the server writes each script with its attributes.

In the browser. I measured the same component (saved as App.tsrx) in Chromium with Playwright, served from packages/ripple by a Vite dev server with Ripple's Vite plugin, ripple({ excludeRippleExternalModules: true }). I loaded it in two ways.

  • Client only: an empty page runs mount(App, { target: document.getElementById('root') }).

  • Server, then hydration: the page is the server output, and its client entry hydrates it:

    javascript
    // server
    import { render } from 'ripple/server';
    import { App } from './App.tsrx';
    
    const { head, body } = await render(App);
    const html = `<!doctype html>
    <html>
        <head>${head}</head>
        <body>
            <div id="root">${body}</div>
            <script type="module" src="/client.js"></script>
        </body>
    </html>`;
    javascript
    // client.js
    import { hydrate } from 'ripple';
    import { App } from './App.tsrx';
    
    hydrate(App, { target: document.getElementById('root') });

After each, I read window.runs, window.meta, the inline scripts in <head>, and the page errors.

Client only:

window.runs: 1
window.meta: undefined
<head> scripts:
  <script>window.runs = (window.runs ?? 0) + 1;</script>
  <script>window.meta = typeof import.meta;</script>
  <script>{"a": 1}</script>
errors:
  Uncaught SyntaxError: Failed to execute 'before' on 'CharacterData': Cannot use 'import.meta' outside a module
  Uncaught SyntaxError: Failed to execute 'before' on 'CharacterData': Unexpected token ':'

Server, then hydration:

window.runs: 1 before hydrate(), 2 after
window.meta: "object" (set by the server's module script)
<head> scripts before hydrate():
  <script>window.runs = (window.runs ?? 0) + 1;</script>
  <script type="module">window.meta = typeof import.meta;</script>
  <script type="application/json" id="data">{"a": 1}</script>
<head> scripts after hydrate():
  <script>window.runs = (window.runs ?? 0) + 1;</script>
  <script>window.runs = (window.runs ?? 0) + 1;</script>
  <script>window.meta = typeof import.meta;</script>
  <script type="module">window.meta = typeof import.meta;</script>
  <script>{"a": 1}</script>
  <script type="application/json" id="data">{"a": 1}</script>
errors:
  Uncaught SyntaxError: Failed to execute 'before' on 'Element': Cannot use 'import.meta' outside a module
  Uncaught SyntaxError: Failed to execute 'before' on 'Element': Unexpected token ':'

Each server script now has a copy without attributes right before it. The classic script ran twice. The module ran once from the server's script and failed from the client's copy, and the JSON was executed as code.

Cause

  • The client transform passes only the body: _$_.script(id, content) returns before the element's attributes are handled. The server transform writes the body into the generic element output, so the opening tag keeps its attributes.
  • The runtime's script always creates a new <script>, sets its textContent, and inserts it before the anchor, in an effect. It has no hydration branch.
  • While hydrating, the anchor is the server's <script> itself: the compiled root() for the <!> placeholder returns the current hydration node (template, hydrate_template). So the new script goes in before the server's one, and the server's one stays. That's why the errors above say on 'Element' after hydration and on 'CharacterData' (a placeholder node) on a client-only mount.

Suggested fix

  • (b) Give the client script its attributes, static and dynamic, before it's inserted. For example, compile them the way other elements' attributes are compiled and pass them to _$_.script, or create the element with them. type and the text have to be set before the script is connected, because the browser decides whether and how to run it on insertion.
  • (a) When hydrating, adopt the server's <script> in script(): don't create or insert a copy, and remove the adopted element on unmount as the effect does today.
  • Tests: packages/ripple/tests/hydration/head.test.js covers <title> and <meta> but no inline script. A hydration test with an inline script could check that <head> has one copy and that its code ran once. A client test could check that type and id are kept.

Environment

  • Ripple main at 83ed5615968364a077cbd39bc26a74e4d8c12b11: ripple 0.4.7, @ripple-ts/vite-plugin 0.4.7, @tsrx/ripple 0.2.4, @tsrx/core 0.3.3
  • Vite 8.1.5 dev server, Chromium 153.0.8010.12 through Playwright 1.63.0
  • Node 24.18.0, macOS 26.6.2

Additional Context

  • Found while comparing how Ripple, Octane and TSRX's own targets compile <script>, for tsrx-org/tsrx#708. #708 makes <script> a raw-text element everywhere, with its body on content as written, and core's targets will print from content. Ripple already emits content on the client and the server, so this bug is independent of that change.
  • The existing tests check only the body text: client and server.
  • Separate from #1519, which is about the TS view printing template text decoded.
  • No existing issue: I searched open and closed issues and pull requests for script, <script>, head, hydration, and import.meta. The closest are #615 (intellisense in <script>), #645 and #660 (hydration of <head>), and the closed #1367 (a dynamic <script> grammar). None covers these two bugs.

Checklist

  • I've searched for existing issues