article · 2026-09-03
The Double Root
How embedding Payload CMS's admin panel produced two competing <html> tags in a Next.js 16 App Router site, and the three-layer fix.
Embedding Payload's admin panel in a Next.js 16 App Router site gave the page
tree two owners for the same <html> element — six console errors, one
shared root layout, and a fix that meant giving Payload its own.
Stack: Next.js 16.2.10 · Payload 3.88.0 · Node 24.20.0
Surfaced at: /admin · Ref: 8be89b9
Summary
Symptom. Visiting /admin threw six distinct browser errors — a
missing-component error, then five variations on "you cannot render two
<html> elements."
Root cause. The app had exactly one root layout
(src/app/layout.tsx), shared by every route including the newly-added
Payload admin group. Payload's own <RootLayout> component also renders
an <html>/<body> pair — so /admin tried to mount two of each, nested
inside one another.
Fix. Give Payload's route group its own independent root layout, using Next's multiple root layouts pattern — plus two smaller, unrelated bugs in the Payload CLI tooling that surfaced along the way.
1. Six errors, one cause
The first error looked like a Payload config problem. The other five, read in order, are React narrowing down on the same fact from five different angles.
1. getFromImportMap: PayloadComponent not found in importMap
{ key: "@payloadcms/next/rsc#CollectionCards" }
2. Hydration failed because the server rendered text didn't match the
client — Invalid HTML tag nesting.
3. In HTML, <html> cannot be a child of <body>. This will cause a
hydration error.
4. <body> cannot contain a nested <html>. See this log for the ancestor
stack trace.
5. You are mounting a new html component when a previous one has not
first unmounted.
6. You are mounting a new body component when a previous one has not
first unmounted.Every stack trace bottomed out in the same two frames: AdminLayout (this
repo's src/app/(admin)/layout.tsx) calling into RootLayout /
RootLayoutContent — internals of @payloadcms/next.
2. Two components, one document
Next's App Router allows exactly one component in the render tree to own
<html> and <body> per request: the
root layout
at the top of the matched segment tree. This app had one —
src/app/layout.tsx — and every route group nested under it, admin
included.
But Payload's embedded admin panel isn't a page that renders inside your
layout. It ships its own <RootLayout>, imported from
@payloadcms/next/layouts, which independently renders <html data-theme>, its own <head> (Payload's @layer payload-default, payload; stylesheet), and its own <body>. It expects to be the root,
not sit inside one.
flowchart TD
A["<html> — app/layout.tsx"] --> B["<body>"]
B --> C["ThemeProvider"]
C --> D["AdminLayout"]
D --> E["<html> — 2nd owner"]
E --> F["<body> — 2nd owner"]
F --> G["Payload UI"]
style E fill:#f2ddd6,stroke:#9c4a36,color:#9c4a36
style F fill:#f2ddd6,stroke:#9c4a36,color:#9c4a36/admin, before — a second <html>/<body> pair nested inside the
first.
flowchart TD
subgraph SITE["(site)/layout.tsx"]
S1["<html><body>"] --> S2["marketing / content / legal"]
end
subgraph ADMIN["(admin)/layout.tsx"]
A1["Payload <RootLayout>"] --> A2["<html><body>"] --> A3["Payload UI"]
end/admin, after — two independent trees, two independent document owners,
siblings under src/app/, never nested inside each other.
3. Two more gremlins
Fixing the layout conflict needed a regenerated importMap.js — the file
behind error 1. Getting there surfaced two smaller, unrelated bugs in the
Payload tooling itself.
3.1 — The import map file was never actually generated
src/app/(admin)/admin/importMap.js was a hand-written stub —
export const importMap = {}; — committed in place of running payload generate:importmap. Running that command failed with "Could not find
Payload import map folder": Payload's resolver only auto-discovers the
file under a literally-named (payload) route group. This repo's group is
(admin), so the config needs to say so explicitly — a second key,
distinct from the baseDir already set.
admin: {
user: Users.slug,
importMap: {
baseDir: path.resolve(dirname, "app", "(admin)"),
+ importMapFile: path.resolve(
+ dirname, "app", "(admin)", "admin", "importMap.js",
+ ),
},
},3.2 — The CLI itself wouldn't run on a current Node
Even with that fixed, payload generate:importmap crashed outright:
Error [ERR_REQUIRE_ASYNC_MODULE]: require() cannot be used on an ESM
graph with top-level await. Use import() instead.
Required module: .../@payloadcms/richtext-lexical/dist/index.jsPayload's CLI loads payload.config.ts through
tsx. Without an explicit ESM
marker, tsx treats the config as CommonJS and compiles its imports down to
synchronous require() calls — which cannot load
@payloadcms/richtext-lexical, a genuine ESM module that uses top-level
await. Node has no flag that makes that safe; a synchronous call can't
wait on an asynchronous module graph. The
documented fix
is "type": "module" — but setting that at the project root would break
every CommonJS config file already in this repo (commitlint.config.js
included). Node also respects nested package.json files scoped to a
subtree, so the marker goes only where it's needed:
// src/package.json — new
{
"type": "module"
}Root package.json stays untouched. Only files under src/ — where
payload.config.ts lives — resolve as ESM.
4. Giving Payload its own root
Next.js supports exactly this — more than one root layout — through
route groups:
remove the single shared app/layout.tsx, and give every top-level group
its own. (marketing), (content), and (legal) were nested one level
deeper, inside a new (site) group that became the one real document
owner for the public site. (admin) needed no changes at all — it already
had its own layout; it just stopped being wrapped by someone else's.
src/app/ — before src/app/ — after
layout.tsx [removed] fonts.ts [new]
error.tsx [moved] (site)/ [new]
not-found.tsx [moved] layout.tsx [new]
loading.tsx [moved] error.tsx
(marketing)/ [moved] not-found.tsx
(content)/ [moved] loading.tsx
(legal)/ [moved] (marketing)/
(admin)/ (content)/
api/health/ (legal)/
(admin)/
api/health/The font loaders, theme provider, skip-link, and analytics tag that used
to live in the one shared layout moved into a dedicated shell component,
so (site)/layout.tsx stays a two-line wrapper rather than tripling that
logic across three route groups:
// src/components/SiteHtmlShell.tsx — new
export default function SiteHtmlShell({ children }) {
return (
<html lang="en" className="scroll-smooth" data-scroll-behavior="smooth" suppressHydrationWarning>
<body className={fontVariables + " ...(existing theme classes)"}>
<ThemeProvider>
<a href="#main-content">Skip to content</a>
{children}
<Analytics />
</ThemeProvider>
</body>
</html>
);
}// src/app/(site)/layout.tsx — new
export default function SiteLayout({ children }) {
return <SiteHtmlShell>{children}</SiteHtmlShell>;
}Why not global-not-found?
Next also ships an experimental global-not-found.js convention for exactly
this multi-root scenario — a branded 404 for URLs that don't resolve under
any root. Skipped here on purpose: (site) already sits at the URL root,
so its own not-found.tsx already covers effectively every non-admin path,
and the experimental flag's own source carries a TODO: remove once stable.
Not worth the provisional API for an edge case that's already covered.
5. Verification
This repo runs Turbopack in development but forces webpack in production
(a separate, pre-existing Turbopack/next/font/google bug) —
so the fix had to hold under both bundlers, not just the one running in
the terminal during debugging.
| Check | Result |
| --- | --- |
| Console errors resolved | 6 / 6 |
| Bundlers verified (Turbopack + webpack) | 2 / 2 |
| <html> elements rendered on /admin | 1 |
| Changes to root package.json's module type | 0 |
The one build warning seen during verification was reproduced against the
pre-fix commit too, via git stash — pre-existing, unrelated to this
change.
References
- Next.js Docs — File Conventions: layout.js — root layout requirements and the multiple-root-layouts pattern via route groups.
- tsx — Issue #552 — top-level await in an ESM package under
node_modulesbreaks when tsx compiles the importing file to CommonJS. - Payload Community Help — Top-level await in a Payload project — the
"type": "module"workaround this repo scoped tosrc/instead of the project root. - payloadcms/payload — Issue #16949 — cited directly in Payload's own CLI source, a related tsx/
registerHooksincompatibility Payload already works around inbin.js.
