nuxt-no-hydration-nondeterminism
Narrow trigger extracted from skills/vue-nuxt-vuetify-conventions section 9 (9.1 and 9.2), so a
non-deterministic or client-only read routes here directly instead of only through the whole
Nuxt/Vue block.
When
Writing or reviewing code that reads Date.now(), Math.random(), new Date(), the time zone, or
window/document/navigator/localStorage at the level of a synchronous setup() or inside a
computed getter.
Steps
Source marks [src N] are the rows of the table in references/origin.md.
- Never call these at the level of synchronous
setup(): the browser globals don't exist
server-side (they throw there), and a non-deterministic value diverges between the server render
and the client render, which is a hydration mismatch. [src 1,2]
- The same rule applies inside a
computed getter: a render on the server reads it too, so it
is not a safe place to reach for these either. (The Vue page says reactivity is switched off on the
server; it does not say computed values are skipped, and a value read by the template is
evaluated once there.) [src 2]
- Isolate the read in
onMounted (never called during server rendering) or inside <ClientOnly>
(its default slot is dropped from the server build; give it a #fallback for the server markup).
An import.meta.client guard keeps a server-unsafe call from running, but it is not enough in
setup() for a library that edits the DOM: the Nuxt guide moves that to onMounted. [src 1,3,4]
- Prefer a value both sides agree on over hiding it. For a stored preference,
useCookie is
readable on the server; for a random or time-based value that must be shown in the first paint,
useState keeps it as shared state (the useState page says the state is serialised to JSON, so the client reusing the server value follows from that; the hydration guide only shows the initialiser pattern); NuxtTime renders dates
consistently; for layout switches use CSS media queries instead of reading window.innerWidth. [src 3]
- Time zones are a source too. A timestamp turned into the user's local time differs when the
server and the browser sit in different zones: do the conversion client-only. [src 2]
- Other mismatch causes to rule out when the warning persists: HTML the browser rewrites
(
<div> inside <p>), phone numbers and dates that iOS Safari wraps in links before hydration
(disable detection with a format-detection meta tag, and wrap a number you want tappable in a
tel: link in the server markup), and third-party scripts such as tag managers that edit the DOM
(initialise them in onMounted). [src 2,3]
- Keep cleanup-needing side effects out of
setup(). onUnmounted never runs on the server, so
a timer started at the top level leaks there; start it in onMounted. [src 2]
Output / checkpoint
No direct Date.now()/Math.random()/new Date()/window./document./navigator./localStorage.
call sits at the top level of setup() or inside a computed getter: every such read is wrapped in
onMounted, moved inside <ClientOnly>, or replaced by useState/useCookie/NuxtTime.
Guardrails
- The failure is a hydration mismatch, not a crash: in development a console warning, in production
Vue patches the DOM to match the client, which costs rendering time and can show flicker; the Nuxt
page also lists dead event listeners and diverging state as possible consequences. Treat the warning
as a bug, not noise. [src 3]
- Suppressing a mismatch (
data-allow-mismatch, Vue 3.5+) is for unavoidable cases only; it does
not fix a divergent value. [src 2]
- The choice of data primitive (
useFetch/useAsyncData/$fetch/useState/useCookie) for actual
async or shared state lives in skills/vue-nuxt-vuetify-conventions section 9.3: read it once the
nondeterminism is isolated and the question becomes "where does this state live".
Origin
skills/vue-nuxt-vuetify-conventions section 9.1 and 9.2 extracted to its own trigger (2026-09-09), then checked
against the Nuxt and Vue documentation on 2026-10-20 (MIT and CC BY 4.0), rewritten in our own words.
🟡 never run. Details and the open points: references/origin.md.
1---2name: nuxt-no-hydration-nondeterminism3description: "Use when a synchronous setup() or a computed reads Date.now(), Math.random(), or window/document/navigator/localStorage: never — isolate it in onMounted, import.meta.client, or ClientOnly."4metadata:5 source: https://skillmd.com/skills/techmefr/nuxt-no-hydration-nondeterminism6---78# nuxt-no-hydration-nondeterminism910Narrow trigger extracted from `skills/vue-nuxt-vuetify-conventions` section 9 (9.1 and 9.2), so a11non-deterministic or client-only read routes here directly instead of only through the whole12Nuxt/Vue block.1314## When15Writing or reviewing code that reads `Date.now()`, `Math.random()`, `new Date()`, the time zone, or16`window`/`document`/`navigator`/`localStorage` at the level of a synchronous `setup()` or inside a17`computed` getter.1819## Steps20Source marks `[src N]` are the rows of the table in [`references/origin.md`](./references/origin.md).211. **Never call these at the level of synchronous `setup()`**: the browser globals don't exist22 server-side (they throw there), and a non-deterministic value diverges between the server render23 and the client render, which is a hydration mismatch. [src 1,2]242. **The same rule applies inside a `computed` getter**: a render on the server reads it too, so it25 is not a safe place to reach for these either. (The Vue page says reactivity is switched off on the26 server; it does not say computed values are skipped, and a value read by the template is27 evaluated once there.) [src 2]283. **Isolate the read** in `onMounted` (never called during server rendering) or inside `<ClientOnly>`29 (its default slot is dropped from the server build; give it a `#fallback` for the server markup).30 An `import.meta.client` guard keeps a server-unsafe call from running, but it is not enough in31 `setup()` for a library that edits the DOM: the Nuxt guide moves that to `onMounted`. [src 1,3,4]324. **Prefer a value both sides agree on over hiding it.** For a stored preference, `useCookie` is33 readable on the server; for a random or time-based value that must be shown in the first paint,34 `useState` keeps it as shared state (the useState page says the state is serialised to JSON, so the client reusing the server value follows from that; the hydration guide only shows the initialiser pattern); `NuxtTime` renders dates35 consistently; for layout switches use CSS media queries instead of reading `window.innerWidth`. [src 3]365. **Time zones are a source too.** A timestamp turned into the user's local time differs when the37 server and the browser sit in different zones: do the conversion client-only. [src 2]386. **Other mismatch causes to rule out when the warning persists:** HTML the browser rewrites39 (`<div>` inside `<p>`), phone numbers and dates that iOS Safari wraps in links before hydration40 (disable detection with a `format-detection` meta tag, and wrap a number you want tappable in a41 `tel:` link in the server markup), and third-party scripts such as tag managers that edit the DOM42 (initialise them in `onMounted`). [src 2,3]437. **Keep cleanup-needing side effects out of `setup()`.** `onUnmounted` never runs on the server, so44 a timer started at the top level leaks there; start it in `onMounted`. [src 2]4546## Output / checkpoint47No direct `Date.now()`/`Math.random()`/`new Date()`/`window.`/`document.`/`navigator.`/`localStorage.`48call sits at the top level of `setup()` or inside a `computed` getter: every such read is wrapped in49`onMounted`, moved inside `<ClientOnly>`, or replaced by `useState`/`useCookie`/`NuxtTime`.5051## Guardrails52- The failure is a hydration mismatch, not a crash: in development a console warning, in production53 Vue patches the DOM to match the client, which costs rendering time and can show flicker; the Nuxt54 page also lists dead event listeners and diverging state as possible consequences. Treat the warning55 as a bug, not noise. [src 3]56- Suppressing a mismatch (`data-allow-mismatch`, Vue 3.5+) is for unavoidable cases only; it does57 not fix a divergent value. [src 2]58- The choice of data primitive (`useFetch`/`useAsyncData`/`$fetch`/`useState`/`useCookie`) for actual59 async or shared state lives in `skills/vue-nuxt-vuetify-conventions` section 9.3: read it once the60 nondeterminism is isolated and the question becomes "where does this state live".6162## Origin63`skills/vue-nuxt-vuetify-conventions` section 9.1 and 9.2 extracted to its own trigger (2026-09-09), then checked64against the Nuxt and Vue documentation on 2026-10-20 (MIT and CC BY 4.0), rewritten in our own words.65🟡 never run. Details and the open points: [`references/origin.md`](./references/origin.md).