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 SSR issue with vue SDK #1008

Open
scottcarlton opened this issue Feb 5, 2025 · 1 comment
Open

Nuxt SSR issue with vue SDK #1008

scottcarlton opened this issue Feb 5, 2025 · 1 comment

Comments

@scottcarlton
Copy link

scottcarlton commented Feb 5, 2025

SSR causes document is not defined within a nuxt application using the @descope/vue-sdk

Reproduce

  1. Create a basic nuxt application using pnpm dlx nuxi@latest init <project-name>

  2. Then setup your nuxt.config.ts file

export default defineNuxtConfig({
  future: {
    compatibilityVersion: 4
  }
})
  1. Create your plugin in the app/plugins directory as descope.client.js
import descope, { getSdk } from "@descope/vue-sdk";
import { defineNuxtPlugin, useRuntimeConfig } from '#imports'

export default defineNuxtPlugin({
  name: 'descope',
  async setup ({ vueApp }) {
    vueApp.use(descope, {
      projectId: <ProjectId>,
      baseUrl: <BaseURL>
    });

    const sdk = getSdk();
    sdk?.onSessionTokenChange((newSession) => {
      // here you can implement custom logic when the session is changing
    });
  }
})
  1. Then simply try to use one of the composables or the Descope component in a page
# app/pages/index.vue
<script setup>
  import { Descope, useDescope } from "@descope/vue-sdk"; // Causes document not defined from within the BaseDescopeWc.ts 
</script>

<template>
  <div>
     <Descope flowId="sign-in" />
  </div>
</template>

Note: This works but has issues too, though this is not a viable because if you try to use a composable anywhere outside of the client rendered component you still get the document issue.

# app/components/DescopeClient.vue
<script setup>
  import { Descope, useDescope } from "@descope/vue-sdk";
</script>

<template>
  <div>
     <Descope flowId="sign-in" />
  </div>
</template>

# app/components/DescopeClient.vue
<script setup>
  import { Descope, useDescope } from "@descope/vue-sdk";
</script>

<template>
  <div>
     <ClientOnly fallbackTag="span">
       <DescopeClient  />
       <template #fallback>Loading Component<template>
    </ClientOnly>
  </div>
</template>

Issue: The issue here is vue warns component and can't resolve because vue thinks descope-wc is a vue component.

*Why this is an issue: Nuxt and even Vue have SSR capabilities. Not being able to use this feature and descope sdk is a major barrier to using descope as an auth solution. Especially the composables within an SSR application.

@scottcarlton scottcarlton changed the title Nuxt SSR causes with vue SDK Nuxt SSR issue with vue SDK Feb 5, 2025
@nirgur
Copy link
Collaborator

nirgur commented Feb 10, 2025

Descope is a client-side component
It renders a custom element that uses browser APIs and cannot be server-side rendered
Therefore, it must be rendered on the client
As you mentioned, wrapping it in a <ClientOnly> tag should achieve this

Regarding the Failed to resolve component: descope-wc warning,
you can resolve it by adding the following configuration to your nuxt.config.ts:

  vue: {
    compilerOptions: {
      isCustomElement: tag => tag === 'descope-wc'
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants