Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuxt - navigator is not defined #195

Open
admiral20 opened this issue May 13, 2024 · 5 comments
Open

Nuxt - navigator is not defined #195

admiral20 opened this issue May 13, 2024 · 5 comments
Assignees
Labels
advice Information how to use/implement the component documentation Improvements or additions to documentation

Comments

@admiral20
Copy link

admiral20 commented May 13, 2024

in nuxt3, i not use vue3 defineAsyncComponent API , the deep-chat error info: navigator is not defined;

after i use defineAsyncComponent API, the dom is not render;

image

@OvidijusParsiunas
Copy link
Owner

Hi @admiral20.

I have experimented with Deep Chat in Nuxt quite a while back, but haven't had a chance to add it to the framework examples as I would inherently need to also update the example servers directory.

The trick is to render the component on the client side and not in the server itself. The reason for this is because Deep Chat uses a variety of global variables/APIs that are not available during server render time such as window and document, hence it needs to wait for its code to be interpreted in the browser.

In Nuxt, the way you can do this is by checking the process.client flag on the mounted lifecycle hook. Once this flag has passed, you can dynamically import Deep Chat and render it conditionally by using a data state variable such as isLoaded. Here is example code below:

<template>
  <deep-chat
    v-if="isLoaded"
    :demo="true"
    :textInput="{ placeholder: { text: 'Welcome to the demo!' } }"
    :initialMessages="initialMessages"
  />
</template>

<script lang="ts">
export default {
  async mounted() {
    if (process.client) {
      // dynamically import the component and conditionally render it via isLoaded when it has finished importing
      await import('deep-chat');
      this.isLoaded = true;
    }
  },
  data() {
    return {
      initialMessages: [
        { role: 'user', text: 'Hey, how are you today?' },
        { role: 'ai', text: 'I am doing very well!' },
      ],
      isLoaded: false,
    };
  },
};
</script>

You can also find a live example for this here, however the link may change in the future when an official example will be added to the repo/documentation.

Let me know if this helps. Thanks!

@OvidijusParsiunas OvidijusParsiunas self-assigned this May 15, 2024
@OvidijusParsiunas OvidijusParsiunas added documentation Improvements or additions to documentation advice Information how to use/implement the component labels May 15, 2024
@OvidijusParsiunas OvidijusParsiunas changed the title nuxt3 error Nuxt - navigator is not defined May 15, 2024
@OvidijusParsiunas
Copy link
Owner

Quick update, I have been quite busy with my work obligations and and have not had much time to create a full code sample for Nuxt, so instead I will simply add the live code example to our documentation and close this issue as soon as possible. Stay tuned for further updates!

@IllamosSalaman
Copy link

Hi @OvidijusParsiunas, would it be possible to create a nitro (the default server layer for Nuxt) server template? I would love to use deepchat in my current Nuxt application, but am puzzled and confused in how I can make it work properly.
Here a link to the Nitro website: https://nitro.unjs.io/

@OvidijusParsiunas
Copy link
Owner

@IllamosSalaman I'll try to get some free time to experiment with it and see if I can get it to work.

@nimakarimi97
Copy link

Hello y'all, I'm have used this library for a Next project and now I wanna use it in Nuxt but I am having some difficulties. If I import it normally it says window is not defined.
However, I got these errors while I used your sample code for Nuxt
image

I'd appreciate any help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
advice Information how to use/implement the component documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

4 participants