Server render writes style="" for an empty style object

Abierto
#187 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
48/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
react, typescript
Área
web-dev

Línea de trabajo

Start with styleToString and attrToHtml in the server rendering code, which decide when a style attribute is written. Add the skip for true, false and '', and omit the attribute when nothing renders. Add the failing case to tests/ssr.test.tsx, then run pnpm test:ci. The size budget also needs SSR_STYLE_SIZE_ALLOWANCE in scripts/size-check.mjs approved by a maintainer before the change can merge.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

renderToString(<div style={{}} />) returns <div style=""></div>; React returns <div></div>. Under a CSP without 'unsafe-inline' for styles, the browser reports each empty style="" as a violation.

Redact also writes style values that React skips:

style React 19.3.1 Redact main
{} <div></div> <div style=""></div>
{ color: true } <div></div> <div style="color:true;"></div>
{ color: '' } <div></div> <div style="color:;"></div>
{ color: 'red', width: '' } <div style="color:red"></div> <div style="color:red;width:;"></div>

React's pushStyleAttribute skips null, boolean and '' values, and writes style=" only before the first value that renders (react-dom-server.node.development.js:1186, react-dom 19.3.1).

Fix

Branch with the fix and tests, ready to merge (I can't open a PR here): https://github.com/TanStack/redact/compare/main...WolfieLeader:redact:fix/ssr-empty-style

What changes
  • styleToString skips true, false and '' as well as null and undefined.
  • attrToHtml writes no style attribute when nothing renders.
Size

redact/server grows 18 B gzip in source and 16 B built, 7 B and 4 B over its budget. SSR_STYLE_SIZE_ALLOWANCE in scripts/size-check.mjs holds that measured increment and needs your approval. An empty-attribute-only fix still goes 2 B over in source.

Testing
  • The new case in tests/ssr.test.tsx fails on main and passes with the fix.
  • pnpm test:ci passes on Node 24.22.0.
Lenguaje dominante
TypeScript
Estrellas
640
Forks
32
Merge medio
1 h 15 min
PR fusionados (30 d)
7

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de TanStack/redact

Todos los issues de TanStack/redact

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.