nuxt-modules / color-mode

Dark and Light mode with auto detection made easy with Nuxt 🌗
https://color-mode.nuxtjs.org
MIT License
1.12k stars 112 forks source link

SSR - Hydration text mismatch #209

Open imcm7 opened 1 year ago

imcm7 commented 1 year ago

On server it not read color because color is stored in local storage and not cookie

RamaHerbin commented 1 year ago

I've got the same issue here Screenshot 2023-10-16 at 14 53 42

Kolobok12309 commented 1 year ago

You can try use ClientOnly for components that use theme variable like this

<div class="color-theme-switcher">
  <ClientOnly>
    <SfIcon
      :icon="iconName"
      class="color-theme-switcher__icon"
    />

    <span class="color-theme-switcher__text">
      Theme
    </span>

    <SfToggleSwitch
      v-model="isDark"
      class="color-theme-switcher__toggler"
    />

    <template #placeholder>
      <div class="color-theme-switcher__placeholder" />
    </template>
  </ClientOnly>
</div>
TsotMe commented 4 months ago

@Kolobok12309 Then on page refresh that content inside ClientOnly will disappear until client render

You can try use ClientOnly for components that use theme variable like this

<div class="color-theme-switcher">
  <ClientOnly>
    <SfIcon
      :icon="iconName"
      class="color-theme-switcher__icon"
    />

    <span class="color-theme-switcher__text">
      Theme
    </span>

    <SfToggleSwitch
      v-model="isDark"
      class="color-theme-switcher__toggler"
    />

    <template #placeholder>
      <div class="color-theme-switcher__placeholder" />
    </template>
  </ClientOnly>
</div>
Kolobok12309 commented 4 months ago

@TsotMe Most powerful way is to use cookie and http header for ssr, and if both of them missing, use ClientOnly. Fulfillment without ClientOnly you can try use css( but I don't like css for this cases), or you get flashes between render and mount

sharifzadesina commented 2 months ago

It seems cookie storage is merged but this issue is not fixed. The interesting thing is, when I use <img :src="/img/logo-${colorMode.value}.svg">, src attribute doesn't update after mount. Because colorMode.value only changes to real value after mount but the attribute doesn't get updated. I get a lot of mismatch errors.