Skip to content

Commit

Permalink
feat(terminatedatabasesaftergenerate): added 'terminateDatabasesAfter…
Browse files Browse the repository at this point in the history
…Generate' config

Added 'terminateDatabasesAfterGenerate' config to avoud nuxt generate warning

re #451
  • Loading branch information
lupas committed Feb 18, 2021
1 parent bc16b45 commit 4df37a7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ dist
yarn.lock
!docs/yarn.lock
sw.*
.husky
16 changes: 15 additions & 1 deletion docs/content/en/guide/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,18 @@ You can either enabled lazy loading for all services or none.
- Type: `Boolean` or `Object`
- Default: `true`

Whether to inject the entire [Firebase module](/guide/usage#firemodule) as `this.$fireModule` or not.
Whether to inject the entire [Firebase module](/guide/usage#firemodule) as `this.$fireModule` or not.

## terminateDatabasesAfterGenerate

- Type: `Boolean`
- Default: `false`

Terminates the Firebase RealTime Database and Firestore after `nuxt generate` has been run. This fixes the below warning by Nuxt and speeds up generate time:

> The command 'nuxt generate' finished but did not exit after 5s
> This is most likely not caused by a bug in Nuxt
> Make sure to cleanup all timers and listeners you or your plugins/modules start.
> Nuxt will now force exit
>
> DeprecationWarning: Starting with Nuxt version 3 this will be a fatal error
34 changes: 34 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ module.exports = function (moduleOptions) {
// add ssrAuth plugin last
// so res object is augmented for other plugins of this module
loadAuthPlugin()

// Terminate Database after Nuxt Generate
if (options.terminateDatabasesAfterGenerate) {
terminateDatabasesInGenerateHooks(this)
}
}

/**
Expand Down Expand Up @@ -303,6 +308,35 @@ function validateConfigKeys (options, currentEnv) {
}
}

/**
* This makes sure nuxt generate does finish without running into a timeout issue.
* Might be able to remove this at some point (e.g. with Nuxt 3)
* See https://github.com/nuxt-community/firebase-module/issues/93
*/
function terminateDatabasesInGenerateHooks (ctx) {
const firebaseDir = ctx.nuxt.options.srcDir + '/node_modules/firebase/'
ctx.nuxt.hook('generate:before', async (generator) => {
const { default: firebase } = await import(firebaseDir + 'app')

if (!firebase.apps.length) {
firebase.initializeApp(generator.options.firebase.config)
}

generator.$fire = firebase.apps[0]
})
ctx.nuxt.hook('generate:done', async ({ $fire }) => {
// Terminate Databases
try {
await $fire.database().goOffline()
console.info('RealTime Database manually terminated.')
} catch (e) {}
try {
await $fire.firestore().terminate()
console.info('Firestore manually terminated.')
} catch (e) {}
})
}

function isEmpty (val) {
return val == null || !(Object.keys(val) || val).length
}
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export interface FirebaseModuleConfiguration {
}
customEnv?: boolean
onFirebaseHosting?: boolean | object
terminateDatabasesAfterGenerate?: boolean
}

/***********************************
Expand Down

0 comments on commit 4df37a7

Please sign in to comment.