renderToStream: rendered content with $`, $& or $' is corrupted when a root hole is filled (String.replace patterns)

オープン 初心者向け
#5,275 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, typescript
領域
backend, testing

調査の方向性

packages/web/src/server.ts の resolveRootHoles() から始めてください。issue では、レンダリングされたコンテンツを挿入する 2 つの String.replace 呼び出しが特定されています。renderToStream root holes の既存テストを確認し、$`、$\u0026、$'、$$ を含むコンテンツのテストを追加してください。issue によると、置換コールバックはコンテンツをそのまま挿入します。ストリーム出力された HTML がこれらの文字を保持し、期待される出力と一致すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Describe the bug

renderToStream fills its root holes with String.prototype.replace and passes the rendered HTML as the replacement string:

// packages/web/src/server.ts, resolveRootHoles()
html = html.replace(marker, res.t[0]);
// …
html = html.replace(marker, out);

A replacement string is parsed for special patterns, so any $`, $&, $' or $$ in the rendered content is substituted instead of written. In the shipped 2.0.4 build that's dist/server.js, server.dev.js and server.observe.js. Each has two calls, in renderToStream → resolveRootHoles. Upstream's next branch has the same code.

The output is wrong whenever text in a root hole contains those characters. For example: docs or code samples that show `$`, prices, regexes, or a $' in user content.

  • $` inserts the HTML before the marker (here, the opening <p>).
  • $& inserts the marker itself (<!--rh0-->).
  • $' inserts everything after the marker, and with more content after it, that can duplicate a large part of the document.

The same applies to serialized data in a hole: in our case the hydration payload for a route loader carried a Markdown string with `$`. That payload reached the browser changed, so the client rendered different text from the server and hydration missed keys.

Your Example Website or App

Inline below (no deployed app needed).

Steps to Reproduce the Bug or Issue

App.jsx:

import { createMemo } from "solid-js";

// Text from an async source, so the root render leaves a hole that renderToStream fills later.
export function App() {
  const price = createMemo(async () => "Total: `$` 5 (`$&` and `$'` too)");
  return <p>{price()}</p>;
}

Compiled for the server with @solidjs/compiler (generate: "ssr", hydratable: false), then:

import { renderToStream } from "@solidjs/web";
import { App } from "./App.mjs";

// The text as HTML: escape() turns & into &amp;.
const expected = "Total: `$` 5 (`$&amp;` and `$'` too)";
let html = "";
await new Promise((resolve) =>
  renderToStream(() => App()).pipe({ write: (chunk) => (html += chunk), end: resolve }),
);
const p = /<p>([\s\S]*?)<\/p>/.exec(html)?.[1];
console.log("expected:", JSON.stringify(expected));
console.log("received:", JSON.stringify(p));

Output with solid-js 2.0.4, @solidjs/web 2.0.4 (Node 26):

expected: "Total: `$` 5 (`$&amp;` and `$'` too)"
received: "Total: `<p> 5 (`<!--rh0-->amp;` and `"
Expected behavior

The hole is filled with the rendered HTML as written:

received: "Total: `$` 5 (`$&amp;` and `$'` too)"

Passing a function as the replacement does that, because a function's return value is inserted literally:

-        html = html.replace(marker, res.t[0]);
+        html = html.replace(marker, () => res.t[0]);
 …
-        html = html.replace(marker, out);
+        html = html.replace(marker, () => out);

We've run with that change patched in (bun patch) and the repro above prints the expected text. replacePlaceholder already splices with slice and isn't affected.

Screenshots or Videos

No response

Platform
  • OS: macOS
  • Runtime: Node 26.13 (also seen in workerd through Vite's dev server)
  • Version: solid-js 2.0.4, @solidjs/web 2.0.4
Additional context

Found while clearing hydration diagnostics on an SSR docs site: the page with the `$` text was the only one whose loader payload differed between server and client.

主要言語
TypeScript
スター
37k
フォーク
1.2k
平均マージ
8時間 43分
マージ済み PR(30日)
365

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

solidjs/solid のほかの issue

solidjs/solid の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。