nuxt-community / firebase-module

🔥 Easily integrate Firebase into your Nuxt project. 🔥
https://firebase.nuxtjs.org
MIT License
642 stars 97 forks source link

[Feature Request] Inject Firebase Config dynamically during run time instead of build time #443

Open ericgrainbridge opened 3 years ago

ericgrainbridge commented 3 years ago

Version

@nuxtjs/firebase: 7.3.3 firebase: 8.0.1 nuxt: 2.14.12

Steps to reproduce

Custom environment does not work as expected. I'm unable to build nuxt once and deploy to multiple environments. Follow the steps in the documentation for the customEnv option to reproduce. https://firebase.nuxtjs.org/guide/options#customenv

Looking into the implementation for the customEnv option, the firebase appConfig gets injected only at build time and not at run time. This is a show stopper for larger projects like the organization I work for.

https://github.com/nuxt-community/firebase-module/blob/dev/lib/plugins/main.js

What is Expected?

Firebase module should behave like @nuxtjs/axios where module configurations get added via nuxt publicRuntimeConfig or privateRuntimeConfig settings. Use the nuxt axios project as a good example to solve build once and deploy to many environment issues.

https://axios.nuxtjs.org/options https://github.com/nuxt-community/axios-module/blob/a1a189486d63356433c939529d6e631f3fc9f923/lib/plugin.js#L194

What is actually happening?

You have to build for each deployment environment. If you build once and deploy to many environments only one environment can be deployed.

lupas commented 3 years ago

Hey @ericgrainbridge Thank you for the issue.

This is in fact working as expected - the way this is currently implemented is not expected to be injected during run time, as you correctly mentioned.

I do see however the value of this being injected during run time. So labelling this as a feature request.

Feel free to create a PR for this issue if you find the time.

br, Pascal

ericgrainbridge commented 3 years ago

@lupas Thanks, much appreciated!

krisanalfa commented 3 years ago

I bumped into this problem. Quick workaround is to use patch-package.

  1. First, you need to use the exact version of @nuxtjs/firebase like mine:

    {
    "dependencies": {
    "@nuxtjs/firebase": "=7.6.1"
    }
    }
  2. Create a new file in patches/@nuxtjs+firebase+7.6.1.patch with the contents below:

    
    diff --git a/node_modules/@nuxtjs/firebase/lib/module.js b/node_modules/@nuxtjs/firebase/lib/module.js
    index 573a616..3c1ab8f 100644
    --- a/node_modules/@nuxtjs/firebase/lib/module.js
    +++ b/node_modules/@nuxtjs/firebase/lib/module.js
    @@ -6,15 +6,19 @@ const templateUtils = require('./utils/template-utils')
    const r = (...path) => resolve(__dirname, ...path)
    
    module.exports = function (moduleOptions) {
    +  const { nuxt } = this
    const defaultOptions = {
     injectModule: true,
    }

3. Add this line to your `package.json` scripts
```json
"postinstall": "patch-package"
  1. Run npm install or yarn install to initiate patching process

  2. Last but not least put your Firebase config in publicRuntimeConfig

module.exports = {
  publicRuntimeConfig: (env) => ({
    firebase: {
      apiKey: env.FIREBASE_API_KEY || 'xxx',
      authDomain: env.FIREBASE_AUTH_DOMAIN || 'xxx.firebaseapp.com',
      databaseURL:
        env.FIREBASE_DATABASE_URL || 'https://xxx.firebaseio.com',
      projectId: env.FIREBASE_PROJECT_ID || 'xxx',
      storageBucket: env.FIREBASE_STORAGE_BUCKET || 'xxx.appspot.com',
      messagingSenderId: env.FIREBASE_MESSAGING_SENDER_ID || 'xxx',
      appId: env.FIREBASE_APP_ID || 'xxx',
      measurementId: env.FIREBASE_MEASUREMENT_ID || 'xxx',
    },
  }),
}

P.S: Not sure this is the best approach, but at least it works on my development machine. Don't use this on production as I'm not responsible for any buggy experiences on your application.

fabioscmelo commented 3 years ago

Any updates? It would be great to be able to change the config at runtime

tabuz commented 3 years ago

I encountered exactly same problem. I reckon this will be a little bit more complex than just replacing config at runtime, due to the fact that config is copied to auto generated service-worker.js.

BobbieGoede commented 2 years ago

Running into the need for this feature now too as we want to split our dev, staging and production firebase configs.

The changes suggested by @krisanalfa do work, but yes unfortunately the generated workers won't be updated as the options are baked/compiled at build time.

alamilladev commented 2 years ago

You should merge @BobbieGoede pull request. It would be great to be able to change the config at runtime.

jeepkd commented 1 year ago

Would love to know more about concerns preventing the PR from being merged?