AlexLavoie42 / Nuxt-Mapbox

Elegant Mapbox integration with Nuxt
81 stars 11 forks source link

Support for runtimeConfig #41

Closed aballet closed 10 months ago

aballet commented 1 year ago

Hello,

I am developing an application that will be deployed to multiple environments (development, staging, production...). I have an access token for each environment. Currently, I did not find a workaround for setting the access token at runtime, once my app is built.

I suggest using the runtimeConfig field in nuxt.config.ts:

{
    // ...
    runtimeConfig: {
        public: {
            mapbox: {
                accessToken: ""
            }
        }
    }
}

This way, we can set the access token at runtime using the environment variable NUXT_PUBLIC_MAPBOX_ACCESS_TOKEN.

In order not to break things, there can be an order of precedence: first the appConfig, then the runtimeConfig if the field is empty in the appConfig.

What do you think? I'm not an expert in module development, but I can give it a try with a PR.

AlexLavoie42 commented 1 year ago

Here is the relevant docs: https://nuxt.com/docs/guide/going-further/modules#exposing-options-to-runtime

I think we would want to use defu to set the default values in runtime config to what was set in app config. Then we need to change the access token given to mapbox to the one in the runtime config instead.

Keep in mind for this to work, we probably will need to watch the access token and reinitialize mapbox with it once it changes.

Personally, I would get the access token on build from different env files, and use a different build step for each environment. Runtime config seems like an interesting feature though, perhaps all the mapboxgl options should be provided at runtime for easier access.

AlexLavoie42 commented 10 months ago

Added in #73