algolia icon indicating copy to clipboard operation
algolia copied to clipboard

Configuring via .env variables on Nuxt 3

Open Lexpeartha opened this issue 1 year ago • 9 comments

Version

@nuxtjs/algolia: 1.1.1 nuxt: 3.0.0-rc.6

Steps to reproduce

  • Add Algolia to Nuxt 3 project
  • Configure it via env variables

What is happening?

algolia: {
    applicationId: process.env.ALGOLIA_APP_ID ?? 'applicationId',
    apiKey: process.env.ALGOLIA_API_KEY ?? 'apiKey',
  },

When starting a project error is thrown Cannot destructure property 'applicationId' of '__vite_ssr_import_0__.useRuntimeConfig(...).algolia' as it is undefined.

Not sure if this has something to do with #81 or #63 but considering the example in the playground, this should work

Lexpeartha avatar Aug 06 '22 11:08 Lexpeartha

Hey @Lexpeartha

Could you try to rebuild your project or and remove node modules?

I am using env variables in module repo, demo projects, tutorials, and many more so this issue seems wuite strange

Baroshem avatar Aug 06 '22 15:08 Baroshem

I was running nuxt 3 edge version, but down-graded it to rc 6 and issue was still there I did delete both node_modules and .nuxt, but it didn't do anything

Lexpeartha avatar Aug 06 '22 19:08 Lexpeartha

@Lexpeartha

Could you please create a reproduction repository in Stackblitz? I tried it once again and was not able to reproduce it.

Baroshem avatar Aug 07 '22 14:08 Baroshem

Well... I can't reproduce it now since it started working for my project as well 🤷🏽‍♂️ Idrk what that was, but it seems to work alright now, thanks

Lexpeartha avatar Aug 07 '22 16:08 Lexpeartha

I see.

Thank you for testing it out. I hope the module will work for you kow without any issues.

Happy coding! 😃

Baroshem avatar Aug 08 '22 05:08 Baroshem

I am seeing this issue on 1.1.1. When I roll back to 1.1.0, it works fine.

Running Nuxt 3.0.0-rc.8

fmarcheski avatar Aug 16 '22 16:08 fmarcheski

@Baroshem Not sure what might be causing the problem to appear again, but it would be great if you could update @nuxt/kit dependency since nuxt 3 in newer releases migrated to vite 3, so that might have some impact?

Lexpeartha avatar Aug 16 '22 19:08 Lexpeartha

@Lexpeartha @fmarcheski

Yes, the update to the newest version of nuxt and nuxt/ki is a planned feature. I want to release it next week preferably with #83 as a 1.2.0. I just need to test some things out to not cause breaking changes for your projects :)

Baroshem avatar Aug 18 '22 09:08 Baroshem

@Baroshem I can confirm this bug.. the solution is to move the algolia config to runtimeConfig.public. If you don't have any public runtimeConfig it works fine as described in the docs.

Works:

export default defineNuxtConfig({
  modules: ['@nuxtjs/algolia'],
  runtimeConfig: {
    FOO: process.env.FOO,
    public: {
      algolia: {
        apiKey: "12345678",
        applicationId: "123",
        instantSearch: true
      },
    }
  },
  algolia: {
     apiKey: "12345678",
     applicationId: "123",
     instantSearch: true
  },
})

Fails:

export default defineNuxtConfig({
  modules: ['@nuxtjs/algolia'],
  runtimeConfig: {
    FOO: process.env.FOO,
    public: {
      anyKey: '123'
    }
  },
  algolia: {
     apiKey: "12345678",
     applicationId: "123",
     instantSearch: true
  },
})

The Bug seems to be that if you define a public runtime there is no merge from the Algolia config and the general public runtime config. Not sure if this is a Nuxt Bug or from this Module. But i guess it's on Nuxt side as a downgrade to a earlier release fixes the issue.

podlebar avatar Aug 19 '22 08:08 podlebar

Closing an issue since everything seems to work fine in newer nuxt release candidates 👍🏽 If anybody encounters the issue again, I'll reopen it

Lexpeartha avatar Sep 30 '22 14:09 Lexpeartha

I still have the error on RC 11 after having destroyed node_modules and .nuxt... [nuxt] [request error] [unhandled] [500] Cannot destructure property 'applicationId' of 'vite_ssr_import_0.useRuntimeConfig(...).algolia' as it is undefined.

but it worked if I duplicate algolia config object in runtimeConfig.public

cfab avatar Oct 01 '22 08:10 cfab

@cfab

I would say then that this issue may be caused by the Nuxt itself rather than the module but I will investigate on my side. Have you tried modifying the module code in node_modules to see if it makes a difference?

Baroshem avatar Oct 01 '22 11:10 Baroshem

Well, I wouldn't dare... ;-) Not sure what to modify anyway ! But thanks.

cfab avatar Oct 03 '22 03:10 cfab

Folks,

The issue is caused when you have both algolia object and runtimeConfig.public both registered in nuxt.config.ts file.

I will release a fix today to fix this issue.

Baroshem avatar Oct 03 '22 15:10 Baroshem

Should be working now with 1.1.4 version

Baroshem avatar Oct 03 '22 16:10 Baroshem

Guys, I am closing the issue as it was tested already in a few Nuxf 3 apps with public runtime config. If this error occurs again, please reopen.

Baroshem avatar Oct 04 '22 09:10 Baroshem