JavaScript is required for this page to work.
⤎ Back to blog overview

Nuxt hydration issues and date formatting

I am writing this because it took me annoyingly long to figure this out a couple of times before. I was seeing hydration issues, related to date formatting, come up in my app running on the server, but not when developing locally.

If this is happening to you, you might want to explicitly specify the time zone in the formatting options to ensure consistency. The server and client could have time zone differences.

It is worth noting that this is really framework agnostic and could be applied anywhere where you have a hydration process.

TypeScript
export default function (inputDate: string, locale: string) {
  const dateObject = new Date(inputDate)

  const options: Intl.DateTimeFormatOptions = {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    timeZone: 'UTC', // Explicitly set.
  }

  return new Intl.DateTimeFormat(locale, options).format(dateObject)
}

Other ways of dealing with hydration mismatches

If you are using Vue 3.5+ (or a Nuxt version that uses Vue 3.5+ under the hood) you can explicitly allow mismatches with a special attribute called data-allow-mismatch. In a recent episode of DejaVue, Michael and Alex speak briefly about this addition.

It works the following way:

<div data-allow-mismatch>{{ data.toLocaleString() }}</div>

The data-allow-mismatch attribute takes one of the following optional parameters, but allows all mismatches if no value is provided: text, children (only allows mismatch for direct children), class, style, attribute.

<div data-allow-mismatch="text">{{ data.toLocaleString() }}</div>

Another way would be to use the <ClientOnly> component, but that would of course mean that you are skipping server-side rendering altogether for what you wrap with it.

<ClientOnly fallback-tag="span" fallback="Loading data...">
    <div>{{ data.renderMeOnTheClientOnly }}</div>
</ClientOnly>

The useHydration() composable

There is also an interesting Nuxt composable called useHydration(), that allows full control of the hydration cycle to set and receive data from the server.

Hope this helps!