className={undefined} leaves an empty class="" attribute after client rendering, so [class] selectors match
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 84/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- next.js, react, typescript
- Domain
- frontend
Research direction
Start in packages/redact/src/dom/dom.ts at the className assignment shown in the issue, then run the supplied Next.js reproduction and inspect the class attributes in the browser console. Done means null or undefined className leaves no class attribute, while an empty-string className still produces class="", matching React.
Written by the indexing model from the issue text.
Description
Version: @tanstack/redact 0.2.6 through withRedact, Next.js 16.4.4 (App Router, Turbopack), react/react-dom 19.3.1, Chrome 156, production build. dom.ts is unchanged on main (049de24f).
What happens
When an HTML element is created or updated in the browser with className={undefined}, Redact leaves an empty class="" attribute. React leaves no attribute at all.
The cause is in packages/redact/src/dom/dom.ts:
(el as HTMLElement).className = next == null ? '' : '' + next;
Assigning an empty string to className creates the attribute.
| Element | React 19.3.1 | Redact 0.2.6 |
|---|---|---|
className={undefined}, server-rendered and hydrated |
no attribute | no attribute |
className={undefined}, server-rendered, then re-rendered |
no attribute | no attribute |
className={undefined}, rendered only in the browser |
no attribute | class="" |
className changes from 'x' to undefined |
no attribute | class="" |
className="" |
class="" |
class="" |
Why it matters
Selectors that test whether the attribute exists, such as section[class] or :not([class]), now match differently. CSS Modules make this easy to hit, because styles.name is undefined when a class has no rules.
We found it by comparing screenshots of our app against React baselines. An A/A run of React against itself was clean. A global section[class] rule started to apply to one section, which gained 70 px of block padding and a background, and the page grew 140 px taller. Removing the empty attribute in the browser restored the React layout exactly.
Together with #85 and #81, this keeps us from using Redact in production on Next.js.
Reproduction
package.json dependencies: [email protected], [email protected], [email protected], @tanstack/[email protected].
next.config.mjs:
import { withRedact } from '@tanstack/redact/next'
export default withRedact({})
app/layout.js:
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
app/page.js:
'use client'
import { useEffect, useState } from 'react'
export default function Page() {
const [mounted, setMounted] = useState(false)
useEffect(() => setMounted(true), [])
return (
<main>
<section id="ssr-undefined" className={undefined}>a</section>
<section id="ssr-empty" className="">b</section>
<section id="rerender-undefined" className={undefined} data-n={mounted ? 1 : 0}>c</section>
<section id="toggle-to-undefined" className={mounted ? undefined : 'x'}>d</section>
{mounted ? <section id="client-undefined" className={undefined}>e</section> : null}
{mounted ? <section id="client-empty" className="">f</section> : null}
</main>
)
}
Run next build && next start, open /, and run this in the console:
Object.fromEntries([...document.querySelectorAll('section')].map((e) => [e.id, e.getAttribute('class')]))
Redact 0.2.6:
{"ssr-undefined":null,"ssr-empty":"","rerender-undefined":null,"toggle-to-undefined":"","client-undefined":"","client-empty":""}
React 19.3.1:
{"ssr-undefined":null,"ssr-empty":"","rerender-undefined":null,"toggle-to-undefined":null,"client-undefined":null,"client-empty":""}
Expected
The same result as React: a null or undefined className leaves no class attribute.
Suggested fix
if (next == null) el.removeAttribute('class');
else (el as HTMLElement).className = '' + next;
- Dominant language
- TypeScript
- Stars
- 581
- Forks
- 28
- Avg merge
- 13h 48m
- Merged PRs (30d)
- 45
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TanStack/redact
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 30/100
Maintainers usually reply within 1 day
-
`lazy(memo(Component))` doesn't re-render on store update when an ancestor reads the same storeOpen
Difficulty 1/5 Under an hour Newbie friendliness 25/100
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
TanStack/redact#166 · 1 reaction ·
Maintainers usually reply within 1 day
Similar issues
-
Type/Improvement
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
OpenNSW/nsw-srilanka#1259 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
oxc-project/tsrs#1010 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Fission-AI/OpenSpec#2260 ·
Maintainers usually reply within 1 day
-
Add: ESP 1 UKOpencheck:passed streams:add
Difficulty 1/5 1-3 hours Newbie friendliness 64/100
Maintainers usually reply within 1 day
-
area:infra area:storage bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
THU-MAIC/OpenMAIC#2311 · 1 comment ·
Maintainers usually reply within 1 day