nuxt-modules / algolia

🔎 Algolia module for Nuxt
https://algolia.nuxtjs.org/
MIT License
191 stars 35 forks source link

Configuring via .env variables on Nuxt 3 #84

Closed Lexpeartha closed 2 years ago

Lexpeartha commented 2 years ago

Version

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

Steps to reproduce

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

Baroshem commented 2 years ago

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

Lexpeartha commented 2 years ago

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

Baroshem commented 2 years ago

@Lexpeartha

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

Lexpeartha commented 2 years ago

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

Baroshem commented 2 years ago

I see.

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

Happy coding! 😃

fmarcheski commented 2 years ago

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

Lexpeartha commented 2 years ago

@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?

Baroshem commented 2 years ago

@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 :)

podlebar commented 2 years ago

@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.

Lexpeartha commented 2 years ago

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

cfab commented 2 years ago

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

Baroshem commented 2 years ago

@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?

cfab commented 2 years ago

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

Baroshem commented 2 years ago

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 commented 2 years ago

Should be working now with 1.1.4 version

Baroshem commented 2 years ago

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.