Skip to content

Commit 4df37a7

Browse files
committed
feat(terminatedatabasesaftergenerate): added 'terminateDatabasesAfterGenerate' config
Added 'terminateDatabasesAfterGenerate' config to avoud nuxt generate warning re #451
1 parent bc16b45 commit 4df37a7

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ dist
1111
yarn.lock
1212
!docs/yarn.lock
1313
sw.*
14+
.husky

docs/content/en/guide/options.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,18 @@ You can either enabled lazy loading for all services or none.
162162
- Type: `Boolean` or `Object`
163163
- Default: `true`
164164

165-
Whether to inject the entire [Firebase module](/guide/usage#firemodule) as `this.$fireModule` or not.
165+
Whether to inject the entire [Firebase module](/guide/usage#firemodule) as `this.$fireModule` or not.
166+
167+
## terminateDatabasesAfterGenerate
168+
169+
- Type: `Boolean`
170+
- Default: `false`
171+
172+
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:
173+
174+
> The command 'nuxt generate' finished but did not exit after 5s
175+
> This is most likely not caused by a bug in Nuxt
176+
> Make sure to cleanup all timers and listeners you or your plugins/modules start.
177+
> Nuxt will now force exit
178+
>
179+
> DeprecationWarning: Starting with Nuxt version 3 this will be a fatal error

lib/module.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ module.exports = function (moduleOptions) {
7070
// add ssrAuth plugin last
7171
// so res object is augmented for other plugins of this module
7272
loadAuthPlugin()
73+
74+
// Terminate Database after Nuxt Generate
75+
if (options.terminateDatabasesAfterGenerate) {
76+
terminateDatabasesInGenerateHooks(this)
77+
}
7378
}
7479

7580
/**
@@ -303,6 +308,35 @@ function validateConfigKeys (options, currentEnv) {
303308
}
304309
}
305310

311+
/**
312+
* This makes sure nuxt generate does finish without running into a timeout issue.
313+
* Might be able to remove this at some point (e.g. with Nuxt 3)
314+
* See https://github.com/nuxt-community/firebase-module/issues/93
315+
*/
316+
function terminateDatabasesInGenerateHooks (ctx) {
317+
const firebaseDir = ctx.nuxt.options.srcDir + '/node_modules/firebase/'
318+
ctx.nuxt.hook('generate:before', async (generator) => {
319+
const { default: firebase } = await import(firebaseDir + 'app')
320+
321+
if (!firebase.apps.length) {
322+
firebase.initializeApp(generator.options.firebase.config)
323+
}
324+
325+
generator.$fire = firebase.apps[0]
326+
})
327+
ctx.nuxt.hook('generate:done', async ({ $fire }) => {
328+
// Terminate Databases
329+
try {
330+
await $fire.database().goOffline()
331+
console.info('RealTime Database manually terminated.')
332+
} catch (e) {}
333+
try {
334+
await $fire.firestore().terminate()
335+
console.info('Firestore manually terminated.')
336+
} catch (e) {}
337+
})
338+
}
339+
306340
function isEmpty (val) {
307341
return val == null || !(Object.keys(val) || val).length
308342
}

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export interface FirebaseModuleConfiguration {
138138
}
139139
customEnv?: boolean
140140
onFirebaseHosting?: boolean | object
141+
terminateDatabasesAfterGenerate?: boolean
141142
}
142143

143144
/***********************************

0 commit comments

Comments
 (0)