nuxt-modules / color-mode

Dark and Light mode with auto detection made easy with Nuxt 🌗
https://color-mode.nuxtjs.org
MIT License
1.08k stars 99 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 11 months ago

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

Kolobok12309 commented 10 months 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 1 month 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 1 month 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