nuxt-modules / prismic

Easily connect your Nuxt.js application to your content hosted on Prismic
https://prismic.nuxtjs.org
MIT License
245 stars 48 forks source link

Endpoint override using environment variable in Nuxt 3 #183

Closed michael-bouvy closed 1 year ago

michael-bouvy commented 1 year ago

Is your feature request related to a problem? Please describe.

When using different Prismic repositories (ie. staging, prod), it would be helpful to be able to override configuration values at runtime using environment variables, i.e. using Nuxt's native mechanism: NUXT_PUBLIC_*, especially the endpoint property (holding the repository name).

Describe the solution you'd like

From what I understand of the code below, runtimeConfig is not used to get the configuration values:

https://github.com/nuxt-modules/prismic/blob/cd6e497a13e5a4f8313d26f9d7cfcbabcc340cb8/src/runtime/plugin.ts#L20-L23

Merging the module configuration with the public runtimeConfig and using useRuntimeConfig should allow overriding this module's configuration. Also, config key shall be "environment variable compliant", for instance prismic instead of @nuxtjs/prismic. Overriding Prismic's endpoint would then be easily doable defining NUXT_PRISMIC_PUBLIC_ENDPOINT.

Thanks!

lihbr commented 1 year ago

Hey @michael-bouvy, thanks for contributing!

That would be awesome indeed! I'm working on supporting Nuxt 3 with Prismic those days on Slice Machine's side. If you'd like to submit a PR for the above I'd be happy to review it promptly, otherwise I'll try to find some time to implement that ☺️

michael-bouvy commented 1 year ago

@lihbr thanks for your quick reply! Glad to hear you're working on the Slice Machine, we'll definitely also be using it.

Sure for the PR, do you think changing the runtimeConfig key to prismic is acceptable?

lihbr commented 1 year ago

You're welcome ☺️

Sure for the PR, do you think changing the runtimeConfig key to prismic is acceptable?

Yes, defo, the @nuxtjs/prismic key has been pretty much internal/we're (still, I know) in RC, so even if it's a small breaking change, that's fine to me~

michael-bouvy commented 1 year ago

@lihbr PR is ready πŸ™‚

Tested locally (ssr: true) in both dev and production build mode, works as expected.

As mentioned in the PR, only issue left is the preview JS injection which still uses the unresolved repository name.

lihbr commented 1 year ago

πŸŽ‰ Released as part of RC.3: https://github.com/nuxt-modules/prismic/blob/v3/CHANGELOG.md#300-rc3-2023-03-28

hpsworlwide commented 1 year ago

Hi, Great for slicemachine with Vue 3 🀩 πŸ”₯πŸ”₯πŸ”₯

I tested RC.3 with Nuxt 3.3.2, but it caused an error : [nuxt] [request error] [unhandled] [500] useRuntimeConfig is not defined. at \node_modules.pnpm\@nuxtjs+prismic@3.0.0-rc.3_vue@3.2.47\node_modules\@nuxtjs\prismic\dist\runtime\plugin.mjs:18:35

No problem with RC.2

lihbr commented 1 year ago

Hey @hpsworlwide, my bad on this one. Just released rc.4 which should fix that!

Thank you so much for the heads up πŸ™