Skip to content
Source
Sign inSign up
TanStack/redact

className={undefined} leaves an empty class="" attribute after client rendering, so [class] selectors match #142

OpenagentHits(GitHub profile, opens in a new tab) opened this issue 4 comments

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.

ElementReact 19.3.1Redact 0.2.6
className={undefined}, server-rendered and hydratedno attributeno attribute
className={undefined}, server-rendered, then re-renderedno attributeno attribute
className={undefined}, rendered only in the browserno attributeclass=""
className changes from 'x' to undefinedno attributeclass=""
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: next@16.4.4, react@19.3.1, react-dom@19.3.1, @tanstack/redact@0.2.6.

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;
  1. I have a fix ready, but pull requests on this repo are limited to collaborators, so I can't open one. It's available as a single commit on my fork, based on main at 049de24f, if it's useful: c021bb0, branch fix/nullish-classname.

    git fetch https://github.com/agentHits/redact.git fix/nullish-classname
    git cherry-pick FETCH_HEAD

    What changes

    • setProp uses the className setter only for non-nullish values. Null and undefined fall through to the generic attribute path, which already removes the attribute. The class and htmlFor aliases and SVG elements already behave this way.
    • The integration test no className prop with null value checked for an attribute named className, which never exists, so it could not fail. It now checks class, and there is a new undefined case. Both run through the server, stream, client and hydration matrix.
    • dom-prop-reductions recorded the empty string as expected. It now expects no attribute, including when a class changes to undefined.
    • The commit includes a changeset.

    Verification

    • The tests fail on the client-render path before the fix and pass after it.
    • pnpm test:pr on Node 24.22.0: 192 test files passed and 5 skipped, 2070 tests passed and 91 skipped, types clean, all size budgets pass.
    • We ran our production Next.js 16.4.4 app with this commit and the fix for #85 together. All 107 e2e tests pass in Chrome 156 with no hydration errors, and its screenshots match React baselines within 0.1%, so the layout shift described above is gone. A React-against-React control run was clean.

    Things to check

    • scripts/test-react-compatibility-size.mjs fails without your approval. Minified output is 2 bytes smaller in the rows it changes, but gzip grows by up to 3 bytes and brotli by up to 52 bytes in some rows. I tried equivalent forms of the condition and brotli growth stayed between 22 and 75 bytes, so it looks like compression noise. I did not add an allowance to compatibility-size-budget.mjs, because those entries record maintainer approvals.
    • I did not run test:react18 or the Next and Vinext browser matrices locally.

    Feel free to take the commit as it is or reimplement it however you prefer.

  2. Thanks for putting this together! The regression and the fix both check out. There’s a small size allowance to settle before landing it.

    AI review notes: Independent validation of the offered commit on current main passed 2,070 tests, types, build and all 38 fixed limits. Native React, unchanged main and the fix were compared in Chromium, Firefox and WebKit, nine bounded browser records per report. The current-main failure reproduced and the fix matched the native behavior. The exact comparison allowance has been requested, up to 3 gzip or 52 Brotli bytes across 14 comparison rows. No fixed-limit increase was needed and no allowance was applied. CI and review still need to pass before merge, this is not shipped yet. These independent browser checks cover the reported DOM behavior, they do not replace the contributor’s larger Next.js app matrix.

  3. Wait 2 day ..... @tannerlinsley Honestly, I'm disappointed with this library. I've even fallen out of love with NextJS because of its slowness. I'm looking at Astro + Preact with islands—it means less unnecessary JS code goes to the client, and everything works faster. I don't understand the point of this library if everything is so slow and clumsy.

    I tried my best and found bugs, did a PR on my fork, but the admins are just dragging their feet and not really making fixes. Therefore, I conclude that this library is useless because it's too much work, and the end result is 25KB of JS code. Install Astro and you'll be happy.

  4. Stop posting the same hostile comments across multiple threads. Keep discussion respectful and relevant to the issue.

    This is your only warning. Further abusive or repetitive posting will result in a block from the TanStack organization.

Sign in to join this conversation.