Prerendered page with `layout: false` + `setPageLayout()` is fully re-rendered on hydration when the URL has a query string
Environment
- Operating System: macOS 26.7
- Node Version: v22.22.2
- Nuxt Version: 4.5.2
- Vue Version: 3.5.43
- Vue Router Version: 5.3.1
- Nitro Version: 2.13.4
- Builder: vite 8.3.1
- Deployment: static (
nuxt generate) - Last working version: 4.4.8
Reproduction
https://stackblitz.com/edit/github-zkzjxhxn?file=app%2Fplugins%2Fssr-dom-check.client.ts
npm install
npm run generate
npm run previewhttp://localhost:3000/showsServer-rendered DOM reused on hydration: truehttp://localhost:3000/?foo=barshowsServer-rendered DOM reused on hydration: false
The whole reproduction is a plain layout and this page:
<!-- app/pages/index.vue -->
<template>
<main>Page content</main>
</template>
<script setup lang="ts">
definePageMeta({
layout: false,
middleware: [() => setPageLayout("default")],
});
</script>A client plugin (app/plugins/ssr-dom-check.client.ts) keeps a reference to the server-rendered <main> and compares it with the one on screen in onNuxtReady.
Describe the bug
When a prerendered page is opened with a query string (for example the UTM parameters of an ad campaign), the server-rendered DOM of the layout and of the page is discarded and rendered again on the client instead of being hydrated. Without a query string, the same page hydrates normally.
It only happens when the page opts out of the default layout with layout: false and picks its layout with setPageLayout() in a route middleware. With layout: "default" in definePageMeta, or with Nuxt 4.4.8, the DOM is reused.
It looks related to the deferred route restore introduced in 4.5 (hasDeferredRoute in pages/runtime/plugins/router.js, restore moved to the <NuxtPage> suspense onResolve in #35442): the app hydrates against the query-less payload.path, then restores the real route with router.replace({ force: true }) while hydration is still in progress. The middleware runs again during that navigation and the layout <Suspense> goes pending again (its pendingId ends at 5 instead of 1 on a static build of our site), which replaces the layout branch.
On a larger app the consequences are worse than a re-render. On our production site (static marketing pages opened from ads with UTM parameters), async components inside the layout (defineAsyncComponent, including ones using hydrateOnVisible) end up in one of two states:
- never mounted:
asyncDepstays unresolved, presumably becauseregisterDepdrops them once the suspensependingIdhas changed. The navigation stays dead and itsonMountedhooks never run. - stuck in an infinite microtask loop: Vue keeps re-scheduling the update of a parent through
asyncDep.then(() => queuePostRenderEffect(...))for a child that never finishes hydrating. The main thread never goes idle, so later client plugins (analytics, cookie banner) never run.
In our case, both states are only reached with a query string, and only since 4.5.
Additional context
Workaround
A client plugin running between nuxt:revive-payload:client (order -30) and nuxt:router (order -20) appends the query and hash to payload.path when the pathname matches. hasDeferredRoute is then false and hydration happens against the real route, as in 4.4:
import { isSamePath } from "ufo";
export default defineNuxtPlugin({
name: "prerendered-route",
order: -25,
setup(nuxtApp) {
const { path } = nuxtApp.payload;
if (!nuxtApp.isHydrating || !nuxtApp.payload.prerenderedAt || !path) return;
const { pathname, search, hash } = window.location;
if (!isSamePath(pathname, path)) return;
nuxtApp.payload.path = `${path}${search}${hash}`;
},
});It fixes both the reproduction and our production issue, but it relies on payload.path internals, so we would rather drop it once this is fixed upstream.
Possibly related: #34845 (setPageLayout props lost on query-only navigations), #35197, #35355.
Source: nuxt/nuxt