ambitiondev / cookiebot

Composable to use Cookiebot functionality in your Vue or Nuxt application,
https://vue-nuxt-cookiebot.netlify.app/
MIT License
5 stars 0 forks source link

Hydration error #21

Closed Aurion72 closed 1 month ago

Aurion72 commented 1 month ago

Hello,

Im trying to use this package but i got an error :

image

I just installed the module and make a component :

<template>
  <div></div>
</template>

<script setup lang="ts">
  const { consentBanner } = useCookiebot({})
  consentBanner()
</script>

nuxt.config.ts

cookiebot: {
    autoConsentBanner: false,
    cookieBotId: process.env.COOKIEBOT_ID,
  },
bnachtweh commented 1 month ago

Hi @Aurion72, thank you for your issue. Sadly I cannot reproduce your issue. Have you tried removing the module from Nuxt config to see if the problem persists? It seems like the mismatch is more on a templating level. The server renders an HTML comment regarding teleport, while the client renders an empty div?

If the problem only persists when the Cookiebot module is activated, can you please provide me with some more info, like:

Aurion72 commented 1 month ago

Thanks for your reply.

I've finally found the cause of the problem, it's nuxtUI's component which seems to conflict with your package or CookieBot itselft.

I couldn't say why, but I've wrapped in a and that seems to work.

https://github.com/nuxt/ui/blob/dev/src/runtime/components/overlays/Notifications.vue

bnachtweh commented 1 month ago

@Aurion72 it's probably due to the fact that you're rendering a separate component with an empty div into you document which has no other effect than injecting Cookiebot. The recommended usage is to either:

// app.vue
<script setup lang="ts">
const { consentBanner } = useCookiebot({ 
  // config here, only if you're planning to override specifics
});

consentBanner();
</script>

<template>
  <div>
    <UContainer>
      <NuxtPage />
    </UContainer>

    <UNotifications />
  </div>
</template>

following either of those two solutions would solve your hydration error(s)