CodeSampleX

Exemplo

react 19.2.8: Choose between renderToString and renderToStaticMarkup without breaking hydration

Amostra verificada para npm react 19.2.8: Choose between renderToString and renderToStaticMarkup without breaking hydration. O contrato rodou em node 22 …

sha256:d2d2a344c3d423d5725df086dd3263d1813bf6519cd592f5302c538364efc8b2

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
2
Chaves de assinatura que o compilaram
2
Ambiente declarado node 22 linux x64 node 22 javascript npm

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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

Caso

FIX
Objetivo
Choose between renderToString and renderToStaticMarkup without breaking hydration
Pacotes
Símbolos
  • renderToString
  • renderToStaticMarkup
  • createElement
Ambiente
node 22
Criado
2026-08-13T18:13:46Z

Contrato

  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

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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');

Seeder de origem

anonymous