Ejemplo
react-dom 19.2.8: Filter out empty src and href attributes on img, script, and link in React DOM SSR while preserving href on anchor elements
Muestra verificada para npm react-dom 19.2.8: Filter out empty src and href attributes on img, script, and link in React DOM SSR while preserving href on…
sha256:bd62309e1ce3c2b9e67b1e26ba98786541764f9cd3f732a521773fd3661c848f
Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido.
Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas.
MIT-0
Evidencia de ejecución
El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.
- Base de evidencia
- Contrato firmado aprobado
- Recibos de verificación
- 2
- Claves de firma que lo compilaron
- 2
Entorno declarado
node linux x64 node node npm
Entornos de las ejecuciones de verificación
| Entorno | Contrato | Etapas | Ejecución |
|---|---|---|---|
| node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-16 |
| node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-18 |
Caso
HOW- Objetivo
- Filter out empty src and href attributes on img, script, and link in React DOM SSR while preserving href on anchor elements
- Paquetes
- Símbolos
-
- renderToString
- renderToStaticMarkup
- Entorno
- node
- Creado
- 2026-08-16T07:54:05Z
Contrato
- assert renderToString omits empty src attribute on img elements
- assert renderToString preserves non-empty src attribute on img elements
- assert renderToString omits empty src attribute on script elements
- assert renderToString omits empty href attribute on link elements
- assert renderToString preserves empty href attribute on anchor elements
- assert renderToStaticMarkup applies identical empty attribute filtering rules
Archivos
- NOTES.md
- csx.json
- package-lock.json
- package.json
- src/index.mjs
- test/contract.mjs
Código fuente
# Notes on React DOM SSR Empty Attribute Filtering
## What `search_known_solution` answered
`search_known_solution` returned existing sample `sha256:d2d2a344c3d423d5725df086dd3263d1813bf6519cd592f5302c538364efc8b2` ("Choose between renderToString and renderToStaticMarkup without breaking hydration"), which addresses `<!-- -->` text boundary markers. To address a different trap, this sample proves React DOM SSR's behavioural fix for empty `src` and `href` attribute serialization on resource tags versus anchors (React PR #18513, #28124).
## What a model would have written instead
A model would assume that React's SSR HTML string serializer reflects all string props uniformly as HTML attributes, expecting `<img src="" />` and `<script src="" />` to serialize `src=""` into the markup.
## How the wrong version fails
Loudly: on React 18.x and earlier versions, `renderToString` and `renderToStaticMarkup` emit `src=""` on `<img>`/`<script>` and `href=""` on `<link>`, failing the assertions immediately with `AssertionError [ERR_ASSERTION]: Expected <img> with empty src to omit the src attribute`.
{"case":{"caseId":"case:sha256:f84a28501fa8f63efbbea6b89e18b73964e024671011613f1744281e9fa57bf6","contract":["assert renderToString omits empty src attribute on img elements","assert renderToString preserves non-empty src attribute on img elements","assert renderToString omits empty src attribute on script elements","assert renderToString omits empty href attribute on link elements","assert renderToString preserves empty href attribute on anchor elements","assert renderToStaticMarkup applies identical empty attribute filtering rules"],"goal":"Filter out empty src and href attributes on img, script, and link in React DOM SSR while preserving href on anchor elements","kind":"HOW","packages":["pkg:npm/react-dom@19.2.8","pkg:npm/react@19.2.8"],"schemaVersion":1,"symbols":["renderToString","renderToStaticMarkup"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/react-dom@19.2.8","pkg:npm/react@19.2.8"],"schemaVersion":1,"symbols":["renderToString","renderToStaticMarkup"],"verifierAdapter":"node-typescript@1"}
{
"name": "csx-sample-react-dom-empty-attributes",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "csx-sample-react-dom-empty-attributes",
"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"
}
}
}
{
"name": "csx-sample-react-dom-empty-attributes",
"version": "1.0.0",
"private": true,
"type": "module",
"license": "MIT-0",
"dependencies": {
"react": "19.2.8",
"react-dom": "19.2.8"
}
}
import { createElement as h } from 'react';
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
// In web browsers, rendering <img src="">, <script src="">, or <link href="">
// causes the browser to issue an immediate HTTP request to the base URL of
// the current document, effectively downloading the page a second time.
//
// In React 19, React DOM's SSR serializers (renderToString / renderToStaticMarkup)
// actively omit empty string `src=""` and `href=""` attributes on resource-loading
// elements (img, script, link, etc.) to eliminate redundant roundtrips.
//
// Crucially, React DOM PRESERVES empty `href=""` on anchor <a> elements,
// where empty href is a valid HTML pattern for linking to the current document.
export function renderUserAvatar({ src, alt }) {
return renderToString(h('img', { src, alt }));
}
export function renderScriptTag({ src }) {
return renderToString(h('script', { src }));
}
export function renderStylesheetLink({ href }) {
return renderToString(h('link', { rel: 'stylesheet', href }));
}
export function renderPageLink({ href, label }) {
return renderToString(h('a', { href }, label));
}
export function renderStaticCard({ imageSrc, linkHref, title }) {
return renderToStaticMarkup(
h('div', { className: 'card' },
h('img', { src: imageSrc, alt: title }),
h('a', { href: linkHref }, title)
)
);
}
import { strict as assert } from 'node:assert';
import {
renderUserAvatar,
renderScriptTag,
renderStylesheetLink,
renderPageLink,
renderStaticCard,
} from '../src/index.mjs';
// 1. Empty string `src` on <img> must be omitted completely from rendered HTML
// to prevent browsers from requesting the HTML document itself as an image.
const emptyAvatar = renderUserAvatar({ src: '', alt: 'User avatar' });
assert.ok(
!emptyAvatar.includes('src='),
`Expected <img> with empty src to omit the src attribute, got: ${emptyAvatar}`
);
assert.match(emptyAvatar, /alt="User avatar"/);
// 2. Non-empty `src` on <img> must be preserved.
const validAvatar = renderUserAvatar({ src: 'https://example.com/avatar.png', alt: 'User avatar' });
assert.ok(
validAvatar.includes('src="https://example.com/avatar.png"'),
`Expected <img> with valid src to preserve the src attribute, got: ${validAvatar}`
);
// 3. Empty string `src` on <script> must be omitted.
const emptyScript = renderScriptTag({ src: '' });
assert.ok(
!emptyScript.includes('src='),
`Expected <script> with empty src to omit the src attribute, got: ${emptyScript}`
);
assert.equal(emptyScript, '<script></script>');
// 4. Empty string `href` on <link> must be omitted.
const emptyLink = renderStylesheetLink({ href: '' });
assert.ok(
!emptyLink.includes('href='),
`Expected <link> with empty href to omit the href attribute, got: ${emptyLink}`
);
assert.equal(emptyLink, '<link rel="stylesheet"/>');
// 5. Empty string `href` on <a> MUST BE PRESERVED (valid self-link / refresh link).
const selfLink = renderPageLink({ href: '', label: 'Refresh Page' });
assert.ok(
selfLink.includes('href=""'),
`Expected <a> with empty href to preserve href="", got: ${selfLink}`
);
assert.match(selfLink, /<a href="">Refresh Page<\/a>/);
// 6. Static markup rendering (renderToStaticMarkup) must follow the exact same filtering rules.
const staticCard = renderStaticCard({
imageSrc: '',
linkHref: '',
title: 'Dashboard',
});
assert.ok(
!staticCard.includes('<img src='),
`Expected static <img> to omit empty src, got: ${staticCard}`
);
assert.ok(
staticCard.includes('<a href="">Dashboard</a>'),
`Expected static <a> to retain href="", got: ${staticCard}`
);
console.log('CONTRACT PASS: React DOM SSR filters empty src/href on resource tags while preserving href on anchors');