Next.js Cache Components: navigating between params of a root layout hides <html> and throws HierarchyRequestError (blank page)

Abierto
#105 1 comentario 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
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
48/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
next.js, react, typescript
Área
frontend

Línea de trabajo

Start with src/dom/reconcile.ts at placeDomsInOrder (around line 474), then trace the placement path through commitPlacements and the view transition handling in src/dom/features/view-transition/full.ts. Reproduce with the provided Next.js app using Cache Components and navigate from /it to /en; compare with native React. Done means both router.push and <Link> navigate without a HierarchyRequestError or hiding the document.

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

Descripción

Version: @tanstack/redact 0.2.2 (latest on npm) through withRedact, Next.js 16.4.3 (App Router, Turbopack), react/react-dom 19.3.1, Chromium, production build (next build && next start) and next dev.

What happens

With cacheComponents: true, the root layout lives under a dynamic segment (app/[lang]/layout.js renders <html lang={lang}>). A client navigation from /it to /en (router.push or <Link>) leaves a blank page:

  • HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': Only one element on document allowed. is thrown.
  • <html> gets style="display: none !important;", so the whole document is hidden.
  • The URL stays /it, while <html lang> becomes en.

Native React hydrates the same app and navigates with no error. It keeps the single <html>, hides only the outgoing route's <main> with display: none !important, and inserts the new <main> into the same <body>.

After clicking router.push('/en') on /it React 19.3.1 Redact 0.2.2
location.pathname /en /it
<html lang> en en
<html style> none display: none !important;
Outgoing <main> ("Ciao") display: none !important still in <body>, the document is hidden
Page errors 0 1 (HierarchyRequestError)
What you see "Hello" blank page

The same table holds for <Link href="/en">. With cacheComponents: false, Redact navigates correctly, like React. So the trigger seems to be the outgoing root layout being kept hidden (Activity) while the incoming one mounts its own <html>: the hide is applied to the <html> singleton itself, and the new <html> is placed as a second document element.

Reproduction

package.json dependencies: [email protected], [email protected], [email protected], @tanstack/[email protected].

next.config.mjs:

import { withRedact } from '@tanstack/redact/next'

const config = {
  cacheComponents: process.env.CACHE_COMPONENTS !== '0',
}

export default process.env.REDACT === '1' ? withRedact(config) : config

app/[lang]/layout.js:

export function generateStaticParams() {
  return [{ lang: 'en' }, { lang: 'it' }]
}

export default async function RootLayout({ children, params }) {
  const { lang } = await params
  return (
    <html lang={lang}>
      <body>{children}</body>
    </html>
  )
}

app/[lang]/page.js:

import { Switcher } from './switcher'

export default async function Page({ params }) {
  const { lang } = await params
  return (
    <main>
      <h1>{lang === 'it' ? 'Ciao' : 'Hello'}</h1>
      <Switcher lang={lang} />
    </main>
  )
}

app/[lang]/switcher.js:

'use client'

import Link from 'next/link'
import { useRouter } from 'next/navigation'

export function Switcher({ lang }) {
  const router = useRouter()
  const other = lang === 'it' ? 'en' : 'it'
  return (
    <p>
      <button id="push" onClick={() => router.push(`/${other}`)}>
        router.push(/{other})
      </button>{' '}
      <Link id="link" href={`/${other}`}>
        Link to /{other}
      </Link>
    </p>
  )
}

Steps:

  1. REDACT=1 next build && REDACT=1 next start
  2. Open /it, wait for hydration, click the button (or the link).
  3. The page goes blank and the error above is thrown.

Results across the four builds (production, Chromium, each run checked twice: router.push and <Link>):

Build Navigation /it → /en
React, cacheComponents: true works
Redact, cacheComponents: true blank page, HierarchyRequestError
React, cacheComponents: false works
Redact, cacheComponents: false works

Stack (development, next dev)

HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': Only one element on document allowed.
    at placeDomsInOrder (node_modules/@tanstack/redact/dist/…)
    at commitPlacements
    at commitMutation
    at mutation
    at within
    at consume
    at cancel

Mapped to source in a Vinext dev server (same package version):

> placeDomsInOrder   src/dom/reconcile.ts:474:22
> commitPlacements   src/dom/reconcile.ts:1203:8
> commitMutation     src/dom/commit.ts:112:24
> mutation           src/dom/features/view-transition/full.ts:91:36
>                    src/dom/features/view-transition/full.ts:74:4
> within             src/dom/features/view-transition/full.ts:147:8
> consume            src/dom/features/view-transition/full.ts:72:2
> cancel             src/dom/features/view-transition/full.ts:63:4
>                    src/dom/features/view-transition/full.ts:234:8

Also seen with Vinext

We first hit this on a production landing site that builds with Vinext 1.3.0 and redact() from @tanstack/redact/vinext, with one root layout per locale and Cache Components on. The language picker (router.push) failed in 8 of 8 runs on vite build + vite preview and also failed in vite dev, while native React passed 8 of 8. That app uses @cloudflare/vite-plugin 2.0.0-beta, outside the Vinext matrix, so the Next.js reproduction above is the one to use.

A document navigation (window.location.assign) avoids the bug, at the cost of a full page load.

Not tested against main after the 0.2.2 release (#94 landed afterwards).


Found and reduced with Claude Code; every result above was run and checked against native React before filing.

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.