CodeSampleX

サンプル

react 19.2.8: Choose between renderToString and renderToStaticMarkup without breaking hydration

検証済みサンプル — npm react 19.2.8: Choose between renderToString and renderToStaticMarkup without breaking hydration. node 22 · linux alpine/x64 · docker で…

sha256:d2d2a344c3d423d5725df086dd3263d1813bf6519cd592f5302c538364efc8b2

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
2
宣言された環境 node 22 linux x64 node 22 javascript npm

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-13
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-17

ケース

FIX
ゴール
Choose between renderToString and renderToStaticMarkup without breaking hydration
パッケージ
シンボル
  • renderToString
  • renderToStaticMarkup
  • createElement
環境
node 22
作成日
2026-08-13T18:13:46Z

コントラクト

  1. render a component whose children include adjacent text nodes
  2. assert renderToString emits a comment separator at the text boundary
  3. assert renderToStaticMarkup omits it and is otherwise identical
  4. assert interpolated text is HTML-escaped

ファイル

  • csx.json
  • package-lock.json
  • package.json
  • src/index.mjs
  • test/contract.mjs

ソースアーティファクトをダウンロード (tar.gz)

ソース

csx.json
{"case":{"caseId":"case:sha256:0ee6f5151b89d55d7a880db5e223ba807e6c2f55fab2db04e2eff26258962532","constraints":{"moduleSystem":"esm","runtime":"node"},"contract":["render a component whose children include adjacent text nodes","assert renderToString emits a comment separator at the text boundary","assert renderToStaticMarkup omits it and is otherwise identical","assert interpolated text is HTML-escaped"],"goal":"Choose between renderToString and renderToStaticMarkup without breaking hydration","kind":"FIX","packages":["pkg:npm/react@19.2.8","pkg:npm/react-dom@19.2.8"],"schemaVersion":1,"symbols":["renderToString","renderToStaticMarkup","createElement"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"javascript","moduleSystem":"esm","os":"linux","packageManager":"npm","runtime":"node","runtimeVersion":"22","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/react@19.2.8","pkg:npm/react-dom@19.2.8"],"schemaVersion":1,"symbols":["renderToString","renderToStaticMarkup","createElement"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "csx-sample-react-render-to-string",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "csx-sample-react-render-to-string",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "react": "19.2.8",
        "react-dom": "19.2.8"
      }
    },
    "node_modules/react": {
      "version": "19.2.8",
      "resolved": "https://registry.npmjs.org/react/-/react-19.2.8.tgz",
      "integrity": "sha512-PWaYA1L/q9u2u7xYQi+Y3L3Yfnie7XyLeaJICV1MGD6LprsBxcAqGjYyr0eY3p+QdsA+x/Irkt4Qif8D63+Sbw==",
      "license": "MIT",
      "engines": {
        "node": ">=0.10.0"
      }
    },
    "node_modules/react-dom": {
      "version": "19.2.8",
      "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.8.tgz",
      "integrity": "sha512-rVprimfGBG3DR+Tq0IQG2DT5PxKth1WIGDmj5yPmlzr4YBe7uyE+Du4oVqTDXZSHGGGXRtTJEGSSePyQCMBglQ==",
      "license": "MIT",
      "dependencies": {
        "scheduler": "^0.27.0"
      },
      "peerDependencies": {
        "react": "^19.2.8"
      }
    },
    "node_modules/scheduler": {
      "version": "0.27.0",
      "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.27.0.tgz",
      "integrity": "sha512-eNv+WrVbKu1f3vbYJT/xtiF5syA5HPIMtf9IgY/nKg0sWqzAUEvqY/xm7OcZc/qafLx/iO9FgOmeSAp4v5ti/Q==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "csx-sample-react-render-to-string",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "react": "19.2.8",
    "react-dom": "19.2.8"
  }
}
src/index.mjs
import { createElement as h } from 'react';
import { renderToStaticMarkup, renderToString } from 'react-dom/server';

// react-dom/server is a SEPARATE entry point; importing renderToString
// from 'react-dom' fails.
//
// The two renderers differ in one specific place: where a text node sits
// next to another text node, renderToString emits a `<!-- -->` separator
// so hydration can find the boundary between them. renderToStaticMarkup
// omits it. Markup that will be hydrated must come from renderToString —
// using static markup saves a few bytes and then fails hydration on
// exactly those boundaries. A tree with no adjacent text nodes produces
// identical output from both, which is why the difference is easy to miss
// in a small test and only shows up in real content.
function Line({ user, count }) {
  // "signed in as " and {user} are adjacent text nodes: the boundary case.
  return h('p', null, 'signed in as ', user, ' (', count, ' items)');
}

export function hydratableHTML(props) {
  return renderToString(h(Line, props));
}

export function staticHTML(props) {
  return renderToStaticMarkup(h(Line, props));
}

export function escaped(text) {
  return renderToStaticMarkup(h('div', null, text));
}
test/contract.mjs
import { strict as assert } from 'node:assert';
import { hydratableHTML, staticHTML, escaped } from '../src/index.mjs';

const props = { user: 'codesamplex', count: 3 };
const hydratable = hydratableHTML(props);
const staticOnly = staticHTML(props);

// Both carry the same visible text.
for (const html of [hydratable, staticOnly]) {
  assert.match(html, /<p>/);
  assert.match(html, /signed in as/);
  assert.match(html, /codesamplex/);
}

// The difference: separators between adjacent text nodes.
assert.ok(hydratable.includes('<!-- -->'),
  `renderToString should separate adjacent text nodes: ${hydratable}`);
assert.ok(!staticOnly.includes('<!-- -->'),
  `renderToStaticMarkup should omit them: ${staticOnly}`);
assert.notEqual(hydratable, staticOnly);

// Strip the separators and the two agree exactly — the markup is the same
// document, only the hydration boundaries differ.
assert.equal(hydratable.split('<!-- -->').join(''), staticOnly);

// React escapes interpolated text; it is not a raw template engine.
const xss = escaped('<script>alert(1)</script>');
assert.ok(!xss.includes('<script>'), xss);
assert.match(xss, /&lt;script&gt;/);

console.log('CONTRACT PASS: renderToString kept text boundaries that renderToStaticMarkup drops');

オリジンシーダー

anonymous