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

NuxtTurnstile component not loading Turnstile script as expected #250

Open
artifex-pro opened this issue Jun 19, 2023 · 12 comments · Fixed by #271
Open

NuxtTurnstile component not loading Turnstile script as expected #250

artifex-pro opened this issue Jun 19, 2023 · 12 comments · Fixed by #271
Labels
bug Something isn't working needs reproduction

Comments

@artifex-pro
Copy link

artifex-pro commented Jun 19, 2023

🐛 The bug

While using the NuxtTurnstile component from the nuxt turnstile package, it seems that the Turnstile script isn't loading as expected, throwing an Uncaught TypeError: Cannot read properties of undefined (reading 'render') error.

The problem is here according to the console.

script.mjs?v=be5efb17:33 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'render')
at Object.render (script.mjs?v=be5efb17:33:31)

line:

return window.turnstile.render(element, {

When loading in the cdn script through useHead and using onMounted to wait for when the script is available makes it work. But then the script is loaded 2 times. Resulting in a lot of other undesired behaviour.

useHead({
  script: [
    {
      async: true,
      src: 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit',
    },
  ],
});

onMounted(() => {
  // wait until cloudflare turnstile  is available
  const interval = setInterval(() => {
    if (window.turnstile) {
      clearInterval(interval);
      state.turnstile_loaded = true;
    }
  }, 100);
});

<NuxtTurnstile
  v-if="state.turnstile_loaded"
  v-model="turnstile"
  data-theme="dark"
  class="turnstile mt-2 mb-2"
/>

Maybe you can add in your code to wait for window.turnstile?

🛠️ To reproduce

.

🌈 Expected behaviour

That window.turnstile is available before use.

ℹ️ Additional context

No response

@artifex-pro artifex-pro added the bug Something isn't working label Jun 19, 2023
@dargmuesli
Copy link
Collaborator

Would you mind adding a reproduction using stackblitz? 🙏

@huang-julien
Copy link
Contributor

quite strange but i've been able to reproduce it on the playground sometimes. I have a website deployed with nuxt turnstile but no issues for now.
image

@huang-julien
Copy link
Contributor

useHead() doesn't seems to run in the plugin because it couldn't retrieve the head object using injectHead()

@dargmuesli
Copy link
Collaborator

Reopening, see #271 (comment)

@ajaythakkar
Copy link

I am also facing same issue, it is giving error on manual refresh but with router changes it is working fine

@ajaythakkar
Copy link

ajaythakkar commented Oct 4, 2023

I have tried this and it works until this issue not getting resolved

<template v-if="isLoaded"> <NuxtTurnstile v-model="token" /> </template>

mounted() { setTimeout(() => { this.isLoaded=true; }, 500); },

@bayramorhan
Copy link

bayramorhan commented Nov 22, 2023

Using it on my nuxt 3 project. Works fine on SSR but if I navigate to the page that contains turnstile from another page via SPA then it fails. my solution is using <a href instead of <nuxt-link only for the pages that containing turnstile. that way you will never experience this issue on such cases.

@chris-si
Copy link

I'm facing the same issue, that the component is not loading when I completely reload the page (no SSR). But the solution from @ajaythakkar works so far as a workaround.
But I use it just on the Turnstile component which seems to be sufficient to get it working.

<NuxtTurnstile v-if="loadTurnstile" v-model="token" />
...
const loadTurnstile = ref(false);
onMounted(() => {
  setTimeout(() => {
    loadTurnstile.value = true;
  }, 500);
});

@vfrz
Copy link

vfrz commented Dec 25, 2023

I've just faced the same issue and resolved it this way (less random than waiting 500ms):

const pageReady: Ref<boolean> = ref(false);

onNuxtReady(() => {
  pageReady.value = true;
});

@dargmuesli
Copy link
Collaborator

@harlan-zw this sounds like a use case for @nuxt/scripts, no?

const head = useHead({ script: script() })

cc @danielroe

@harlan-zw
Copy link

@harlan-zw this sounds like a use case for @nuxt/scripts, no?

const head = useHead({ script: script() })

cc @danielroe

See #306

@dargmuesli
Copy link
Collaborator

Ahh, that's where I saw that! 🙈 confused it with another repo 😄 Great work 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs reproduction
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants