0

I'm dynamically generating routes in Nuxt.js based on a collection in firestore. Everything generates fine but then it gives this warning.

   ╭──────────────────────────────────────────────────────────────────────────────────────╮
   │                                                                                      │
   │   ⚠ Nuxt Warning                                                                     │
   │                                                                                      │
   │   The command 'nuxt generate' finished but did not exit after 5s                     │
   │   This is most likely not caused by a bug in Nuxt.js                                 │
   │   Make sure to cleanup all timers and listeners you or your plugins/modules start.   │
   │   Nuxt.js will now force exit                                                        │
   │                                                                                      │
   │   DeprecationWarning: Starting with Nuxt version 3 this will be a fatal error        │
   │                                                                                      │
   ╰──────────────────────────────────────────────────────────────────────────────────────╯

Following advice from this post I've added the following snippet:

export default {
  hooks: {
    generate: {
      done(builder) {
        firebase.firestore.terminate()
      }
    }
  },
}

But then that yields a Fatal error:

 FATAL  The client has already been terminated.                                                                                       23:39:58  

  at new FirestoreError (node_modules\@firebase\firestore\dist\index.node.cjs.js:1201:28)
  at FirestoreClient.verifyNotTerminated (node_modules\@firebase\firestore\dist\index.node.cjs.js:17311:19)
  at FirestoreClient.listen (node_modules\@firebase\firestore\dist\index.node.cjs.js:17371:14)
  at CollectionReference.Query$1.onSnapshotInternal (node_modules\@firebase\firestore\dist\index.node.cjs.js:21820:48)
  at CollectionReference.Query$1.getViaSnapshotListener (node_modules\@firebase\firestore\dist\index.node.cjs.js:21851:29)
  at node_modules\@firebase\firestore\dist\index.node.cjs.js:21846:23
  at new Promise (<anonymous>)
  at CollectionReference.Query$1.get (node_modules\@firebase\firestore\dist\index.node.cjs.js:21836:16)
  at routes (nuxt.config.js:185:79)
  at promisifyRoute (node_modules\@nuxtjs\sitemap\lib\cache.js:59:17)
  at AsyncCache.load [as _load] (node_modules\@nuxtjs\sitemap\lib\cache.js:18:28)
  at AsyncCache.get (node_modules\async-cache\ac.js:63:8)
  at internal/util.js:297:30
  at new Promise (<anonymous>)
  at AsyncCache.get (internal/util.js:296:12)
  at generateSitemap (node_modules\@nuxtjs\sitemap\lib\generator.js:54:37)


   ╭────────────────────────────────────────────────────────────────────────────────────────╮
   │                                                                                        │
   │   ✖ Nuxt Fatal Error                                                                   │
   │                                                                                        │
   │   FirebaseError: [code=failed-precondition]: The client has already been terminated.   │
   │                                                                                        │
   ╰────────────────────────────────────────────────────────────────────────────────────────╯

This is my routes method taken in part from this question:

generate: {
    async routes() {
      const collection = await db.collection('restaurants').get();
      return collection .docs.map(x => `/restaurant/${x.title}`);
    }
},

This warning only came up when I added the generate object so I know the problem lies in that area. Any suggestions?

EDIT: Firebase init code in nuxt.config.js

import firebase from 'firebase/app'
import 'firebase/firestore'

const config = {
  apiKey: '',
  authDomain: '',
  databaseURL: '',
  projectId: '',
  storageBucket: '',
  messagingSenderId: '',
  appId: '',
  measurementId: ''
};
firebase.initializeApp(config);
const fireDb = firebase.firestore();

export default {
    generate :{...}
}
SRR
  • 1,608
  • 1
  • 21
  • 51
  • Can you please share your firebase initialization code? – Hardik Shah May 01 '20 at 06:25
  • @HardikShah done – SRR May 01 '20 at 06:30
  • firebase initialize code is in `nuxt.config.js` or as `plugin`!!! please try as a plugin. Any initialization with firestore in that code like for connection to the specific store?? – Hardik Shah May 01 '20 at 06:44
  • @HardikShah I do have it in a plugin. But if I do ```import {fireDb} from '@/plugins/firebase'``` in ```nuxt.config.js``` I get ```Cannot find module '@/plugins/firebase'``` so I've resorted to the other method for now – SRR May 01 '20 at 08:28
  • Are you exporting `fireDb` from `plugins/firebase.js`? – Hardik Shah May 02 '20 at 09:09

4 Answers4

1

If you still need help, you can reach out to Firebase directly as they provides free support here: https://firebase.google.com/support/troubleshooter/contact

Rajeevan
  • 150
  • 6
0

From plugins/firebase.js you should export fireDb and you can import in any component and use that variable as below

In plugins/firebase.js

import firebase from 'firebase/app';
import 'firebase/database';

if (!firebase.apps.length) {
  const config = {
    apiKey: '',
    authDomain: '',
    databaseURL: '',
    projectId: '',
    storageBucket: '',
    messagingSenderId: ''
  };
  firebase.initializeApp(config);
}

const fireDb = firebase.firestore();

export { fireDb };
Hardik Shah
  • 1,366
  • 2
  • 15
  • 35
  • firebase.database() or firebase.firestore() ? – SRR May 02 '20 at 09:49
  • my bad! in my case it was database in yours it's firestore – Hardik Shah May 02 '20 at 09:56
  • This is what I have already. I use ```fireDb``` in the ```.vue``` files in my project so it gets exported correctly. I just can't access it in ```nuxt.config.js``` I don't know why – SRR May 02 '20 at 19:19
  • Why you require in nuxt.config.js can you explain!?? – Hardik Shah May 03 '20 at 04:45
  • So that I can generate dynamic routes. So there's a collection in my database called restaurants and I want each restaurant to have its own route (page). But I don't want to create them manually so I'm using nuxt to generate routes based on that collection. – SRR May 03 '20 at 21:40
  • In my opinion if you try to access firedb in `nuxt.config.js` than it will give you a warning as you got `The command 'nuxt generate' finished but did not exit after 5s ` coz at build time only you are trying to access the firestore not at runtime – Hardik Shah May 04 '20 at 07:41
0

NEW:

This is an issue with node and the realtimedatabase.

The following works without having to kill the process: this.$fire.database.goOffline()


OLD WITH SIDEEFFECTS (DONT USE):

See the following stackoverflow for solutions. I can confirtm that process.exit() worked for my usecase.

node process doesn't exit after firebase once

Timar Ivo Batis
  • 1,861
  • 17
  • 21
  • Sorry the update caused the database not be injected. As soon as i fixed that, and the database was working the timeout began again. As soon as a request is being made to the database, the timeout starts again... Funnily, it says V.6 but it made me do the migration as required by V.7 ... so despite the package json, i think im on `nuxtjs/firebase^7` – Timar Ivo Batis Nov 06 '20 at 16:30
  • Ah, in this case I'm not using `@nuxtjs/firebase` apologies for not specifying that in the question – SRR Nov 06 '20 at 17:22
  • well it doesn't matter because in the end `nuxtjs/firebase` is just a premade plugin. theres no difference. – Timar Ivo Batis Nov 06 '20 at 17:24
  • Perhaps I misunderstood. `@nuxtjs/firebase` is a plugin that controls `firebase`. Because I didn't use the plugin in the project, I didn't see how an answer that quoted an upgrade of the plugin to be the solution for my use case. – SRR Nov 06 '20 at 17:48
  • Nevermind. I updated my answer with a working solution! – Timar Ivo Batis Nov 06 '20 at 19:33
0

For those who still did not manage to fix this, nuxt firebase module has a config flag to terminate thing on generate:

terminateDatabasesAfterGenerate: true

View doc here: https://firebase.nuxtjs.org/guide/options/

Daniel Vilela
  • 587
  • 1
  • 4
  • 19