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

未关闭
#105 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
领域
frontend

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
TypeScript
星标
640
派生
32
平均合并
1 小时 15 分钟
30 天内合并 PR
7

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/redact 的其他 Issue

查看 TanStack/redact 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。