nuxt-modules / turnstile

đŸ”Ĩ Cloudflare Turnstile integration for Nuxt
https://cloudflare.com/products/turnstile
MIT License
215 stars 17 forks source link

Nuxt Site Config not working in combination with Nuxt Turnstile #314

Closed Applelo closed 1 day ago

Applelo commented 4 days ago

🐛 The bug

Since Nuxt 3.12, Nuxt Turnstile is causing bugs across my website with two modules. The first one, nuxt-multi-cache was easy to fix, I had just to change the position of the modules inside the nuxt.config.ts file. The second one, @nuxtjs/sitemap throws an error when I load the website.

 ERROR  [nuxt] [request error] [unhandled] [500] Cannot read properties of undefined (reading 'debug')
  at Object.handler (./node_modules/.pnpm/nuxt-site-config@2.2.12_@nuxt+devtools@1.3.3_cc2uo6sas5d5jmayayiogvdkxu__@unocss+webpack@0.60_fse6xbdnt3mfht7ixfkdoaxcgy/node_modules/nuxt-site-config/dist/runtime/nitro/middleware/init.mjs:12:19)  
  at ./node_modules/.pnpm/h3@1.11.1/node_modules/h3/dist/index.mjs:1962:31  
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)  
  at async Object.callAsync (./node_modules/.pnpm/unctx@2.3.1/node_modules/unctx/dist/index.mjs:72:16)  
  at async Server.toNodeHandle (./node_modules/.pnpm/h3@1.11.1/node_modules/h3/dist/index.mjs:2249:7)

If I remove the Nuxt Turnstile module, Nuxt sitemap is working as expected.

🛠ī¸ To reproduce

https://stackblitz.com/edit/nuxt-starter-dj2rf9?file=nuxt.config.ts

🌈 Expected behaviour

To not making other modules crash :)

ℹī¸ Additional context


[EDIT] After investigation, nuxt-site-config-inject this kind of payload

{ stack:
   [ { _context: 'system', _priority: -15, name: 'julbo', env: 'development' },
     { _context: 'package.json', _priority: -10, name: 'julbo' },
     { _context: 'buildEnv', _priority: -1, url: 'http://localhost:3000' },
     { _context: '@nuxtjs/i18n', url: 'http://localhost:3000' } ],
  version: '2.2.12',
  debug: false }

And nuxt turnstile rewrite it somehow and remove all information on nuxt-site-config object.

[EDIT 2] Seems like the following lines are causing the issue by overwriting stack. https://github.com/nuxt-modules/turnstile/blob/2b425eec7c99ff4cd04021cc3dff8dbd499f44a9/src/module.ts#L67C4-L76C7

[HS EDIT] Another issue I found, we need to put on runtimeConfig the .env infos. It was not necessary before Nuxt 3.12. Related to #27117 ?

{
turnstile: {
      secretKey: '',
    },

 public: {
      turnstile: {
        siteKey: '',
      },
}
}
Applelo commented 4 days ago

I have change this

   nuxt.options.runtimeConfig = defu(nuxt.options.runtimeConfig, {
      turnstile: {
        secretKey: options.secretKey,
      },
      public: {
        turnstile: {
          siteKey,
        },
      },
    })

to this

nuxt.options.runtimeConfig.turnstile = defu(nuxt.options.runtimeConfig.turnstile, {
    secretKey: options.secretKey,
});
nuxt.options.runtimeConfig.public.turnstile = defu(nuxt.options.runtimeConfig.public.turnstile, {
   siteKey,
});

I will make a PR to fix this issue.

Here a patch file waiting for the fix to be release on the next version of nuxt.

@nuxtjs__turnstile@0.8.0.patch

danielroe commented 3 days ago

I believe this issue should be resolved in https://github.com/nuxt/nuxt/pull/27596. Would you check with the nightly release channel? 🙏

Applelo commented 3 days ago

Yeah it solves the issue. Thanks, I close the PR and I will close this issue when the next version is release with the fix 😉

danielroe commented 3 days ago

Great! It should be out this weekend 🤞

dargmuesli commented 1 day ago

Is out and I can confirm this resolves the issue. @Applelo I hope you can agree? :raised_hands:

Applelo commented 1 day ago

Yeah it is working as expected 😄 Thanks for your reactivity 🚀

danielroe commented 1 day ago

Not at all - and sorry for the pain 🙏