GaborTorma / nuxt-dayjs-i18n

3 stars 1 forks source link

how to fix hydration error? #2

Closed reslear closed 8 months ago

reslear commented 8 months ago
Screenshot 2023-10-18 at 12 51 40

used syntax:

{{ $df(item.updatedAt, 'LL') }}
// or
{{ $dayjs(item.updatedAt).format('LL') }}
reslear commented 8 months ago

oh fixed simple add v-if:

<div v-if="item.updatedAt">
  {{ $df(item.updatedAt, 'LL') }}
</div>
GaborTorma commented 8 months ago

I think item.updatedAt is undefined on server side

reslear commented 8 months ago

Uh, that didn't help, so it turns out

it does hydration on the server with the selected language - as if it caches only the first one, when you refresh the page with another language - there is already a hydration problem.

also same is add:

definePageMeta({
  key: (route) => route.fullPath,
})
GaborTorma commented 8 months ago

Can you provide a minimal repo?

reslear commented 8 months ago

https://stackblitz.com/edit/nuxt-dayjs-i18n-xtqkqq

  1. Change locale to hu
  2. after prefix_except_default update url and reload page
  3. re-inspect frame

https://github.com/GaborTorma/nuxt-dayjs-i18n/assets/12596485/1cc4dcbb-93c0-443c-a540-8ed3a5ef46cb

reslear commented 8 months ago

temporally worked solution

<time :datetime="item.updatedAt">
  <ClientOnly :fallback="item.updatedAt">{{ $df(item.updatedAt, 'LL') }}</ClientOnly>
</time>
GaborTorma commented 8 months ago

Unfortunately the Day.js is not SSR ready. Server-side generated with the default locale.

GaborTorma commented 8 months ago

I found a solution. I will release a new version in few days.

GaborTorma commented 8 months ago

Fixed in v.2.0.0 Make sure don't define defaultLocale in dayjs config. I comes from i18n.locale.

GaborTorma commented 8 months ago

Use dayjs-nuxt@2.1.9

reslear commented 8 months ago

Updated, works like a charm :) you are awesome !