Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase - Could not reach Cloud Firestore backend #14608

Closed
cglatzel opened this issue Aug 16, 2022 · 15 comments
Closed

Firebase - Could not reach Cloud Firestore backend #14608

cglatzel opened this issue Aug 16, 2022 · 15 comments

Comments

@cglatzel
Copy link

cglatzel commented Aug 16, 2022

Environment

WSL 2 - Nuxt Version 3.0.0-rc.8 will result in the same issue
module content will have no effect (positive or negative)


  • Operating System: Linux
  • Node Version: v16.16.0
  • Nuxt Version: 3.0.0-rc.6
  • Package Manager: npm@8.15.1
  • Builder: vite
  • User Config: css, modules, content
  • Runtime Modules: @nuxt/content@2.0.1
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-kzwv1p?file=plugins/firebase.ts

Just a very basic firebase firestore setup. You have to provide your own firebase config. In case you don't have any, create a new firebase project and use it and create a collection with the name layers in order for the code to work directly out of the box.

Describe the bug

Retrieving and accessing the data from firestore outputs the following into the terminal:

[nitro] [dev] [unhandledRejection] TypeError: a.body.getReader is not a function
    at k.Va (file:///home/projects/github-kzwv1p/.nuxt/dist/server/server.mjs:26618:368)
[2022-08-16T19:32:24.127Z]  @firebase/firestore: Firestore (9.9.2): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

After the first request, data is retrieved, BUT everything stops working, no data can be retrieved again and the application loads a very long time. HMR stops working. Vite building doesn't work anymore and the app crashed after some reload attempts.

Additional context

Since it runs into the same issue on Stackblitz, I don't think it has to something with the environment it is executed from.

Logs

WARN  Sourcemap for "/home/projects/github-kzwv1p/node_modules/@firebase/util/dist/node-esm/index.node.esm.js" points to missing source files

✔ Vite server built in 3356ms                                                                    21:37:43
✔ Nitro built in 796 ms                                                                    nitro 21:37:44
[nitro] [dev] [unhandledRejection] TypeError: a.body.getReader is not a function
    at k.Va (file:///home/projects/github-kzwv1p/.nuxt/dist/server/server.mjs:26618:368)
ℹ Vite client warmed up in 9040ms                                                                21:37:46
[2022-08-16T19:37:55.851Z]  @firebase/firestore: Firestore (9.9.2): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
[2022-08-16T19:38:30.861Z]  @firebase/firestore: Firestore (9.9.2): Connection WebChannel transport errored: Vd {
  type: 'c',
  target: <ref *1> Y {
    s: false,
    o: undefined,
    i: $a { src: [Circular *1], g: [Object], h: 4 },
    P: [Circular *1],
    I: null,
    g: Id {
      za: 0,
      l: [Array],
      h: [Mb],
      s: null,
      P: [Object],
      o: '$httpHeaders',
      aa: [Object],
      D: 'gsessionid',
      sa: null,
      g: null,
      W: 'https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel',
      F: [U],
      oa: null,
      la: null,
      V: 31449,
      Za: 4,
      Xa: false,
      j: [Z],
      m: null,
      u: null,
      v: null,
      N: true,
      X: true,
      U: -1,
      ta: -1,
      I: -1,
      C: 0,
      A: 0,
      Y: 0,
      Pa: 5000,
      '$a': 10000,
      Ya: 2,
      ra: 600000,
      qa: [pd],
      Ba: false,
      K: undefined,
      H: true,
      J: '',
      i: [gd],
      Ca: [ld],
      ja: false,
      Ra: false,
      '$': false,
      ka: undefined,
      O: 0,
      L: false,
      B: null,
      Wa: true,
      G: 0
    },
    l: 'https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel',
    h: { database: 'projects//databases/(default)' },
    A: true,
    v: true,
    j: Z { g: [Circular *1] }
  },
  g: <ref *1> Y {
    s: false,
    o: undefined,
    i: $a { src: [Circular *1], g: [Object], h: 4 },
    P: [Circular *1],
    I: null,
    g: Id {
      za: 0,
      l: [Array],
      h: [Mb],
      s: null,
      P: [Object],
      o: '$httpHeaders',
      aa: [Object],
      D: 'gsessionid',
      sa: null,
      g: null,
      W: 'https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel',
      F: [U],
      oa: null,
      la: null,
      V: 31449,
      Za: 4,
      Xa: false,
      j: [Z],
      m: null,
      u: null,
      v: null,
      N: true,
      X: true,
      U: -1,
      ta: -1,
      I: -1,
      C: 0,
      A: 0,
      Y: 0,
      Pa: 5000,
      '$a': 10000,
      Ya: 2,
      ra: 600000,
      qa: [pd],
      Ba: false,
      K: undefined,
      H: true,
      J: '',
      i: [gd],
      Ca: [ld],
      ja: false,
      Ra: false,
      '$': false,
      ka: undefined,
      O: 0,
      L: false,
      B: null,
      Wa: true,
      G: 0
    },
    l: 'https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel',
    h: { database: 'projects//databases/(default)' },
    A: true,
    v: true,
    j: Z { g: [Circular *1] }
  },
  defaultPrevented: false,
  status: 1
}
@cglatzel
Copy link
Author

*Ubuntu 22.04.1 LTS

@vincekruger
Copy link

any update on this?

@Minimata
Copy link

I have the same issue on Windows and haven't found a solution yet.

@edwardcahyadi
Copy link

Still an issue in 3.0.0-rc.9. You can still use Firestore if you initialize the plugin client-side (firebase.client.ts). But you lose the SSR benefits.

Hope this gets fixed before the stable release. Else it's pretty much unusable for Firestore users.

@hecateball
Copy link
Contributor

as far as I know this is the same issue as: #13393

@cglatzel
Copy link
Author

Is it really? I mean from the bug description here, the log looks quite different from what I provided above - https://github.com/niklv/nuxt3_firebase9_ssr_bug#bug-description.

@hecateball
Copy link
Contributor

I meant that both of this issue and #13393 are caused by unintened module resolution.
(see: #13393)

Similaly, some other firebase modules does not work on SSR dev mode.
I'm not sure if all of them have the same root cause, but anyway we should resolve the module resolution issue first.

@vincekruger
Copy link

Still an issue in 3.0.0-rc.11 :(

@cglatzel
Copy link
Author

just a quick note that it is still an issue with rc.12 and firebase 9.12.1

@SamMakesCode
Copy link

SamMakesCode commented Oct 31, 2022

Still an issue with 9.12.1 on MacOS 13 Ventura

Edit: I commented here thinking I was in the firebase repo, but rather thank delete my comment, I thought I'd share my fix even though my issue was nothing to do with Nuxt specifically.

I was trying to use a firebase app against the emulator suite using the connectAuthEmulator() and connectFirestoreEmulator() functions. The mistake I made was assuming the parameters for both of these functions were the same (because they look like they ought to be). connectAuthEmulator() requests a url and connectFirestoreEmulator() requests a host. In other words, make sure your connectFirestoreEmulator() doesn't have a "http://" on the front of it.

@ddddeano
Copy link

still an issue, any ideas?

@vincekruger
Copy link

So I've managed to connected to firestore with functions and functions-admin using composables and plugins and api endpoints.

@edwardcahyadi
Copy link

So I've managed to connected to firestore with functions and functions-admin using composables and plugins and api endpoints.

@vincekruger If possible, could you share the minimal working code? I think that may help many people here.

@vincekruger
Copy link

vincekruger commented Nov 23, 2022

Here is something basic for the front end.

It's better to use asyndData https://nuxt.com/docs/getting-started/data-fetching.

Server middleware is even better, because if you host on Firebase Hosting and use a function to serve you can use firebase-admin and lock down your database - https://nuxt.com/docs/guide/directory-structure/server

plugins/firebase.client.ts

import { initializeApp } from 'firebase/app'
import { initializeAppCheck, ReCaptchaV3Provider } from 'firebase/app-check'
import { getAnalytics } from 'firebase/analytics'
import { getPerformance } from 'firebase/performance'

export default defineNuxtPlugin((nuxtApp) => {
    // Get Config
    const config = useRuntimeConfig()

    // Initialize Firebase
    const app = initializeApp(config.public.firebase_config)

    // App Check
    initializeAppCheck(app, {
        provider: new ReCaptchaV3Provider(config.public.recaptcha_site_key),
        isTokenAutoRefreshEnabled: true,
    })

    // Initialize Performance Monitoring
    getPerformance(app)

    // Initialize Analytics
    getAnalytics(app)

    // Provide Firebase
    nuxtApp.provide('firebase', {
        app,
    })
})

composables/firestore.ts

import _ from 'lodash'
import { getFirestore, connectFirestoreEmulator, collection, query, where, getDocs } from 'firebase/firestore'

export const useFirestore = async () => {
    if (process.server) return

    const { $firebase } = useNuxtApp()
    const db = getFirestore($firebase.app)
    connectFirestoreEmulator(db, 'localhost', 8080)

    const collectionRef = collection(db, 'user')
    const q = query(collectionRef, where('active', '==', true))
    const querySnapshot = await getDocs(q)

    return _.map(querySnapshot.docs, (doc) => ({ id: doc.id, active: doc.get('active') }))
}

Usage in a Vue page/component.

<script setup lang="ts">
const data = await useFirestore()
</script>
<template>
    {{ data }}
</template>

@posva
Copy link
Collaborator

posva commented Nov 29, 2022

Adding this to the nuxt.config.ts seems to serve as a workaround:

import { fileURLToPath } from 'node:url'
import { resolve } from 'node:path'

// we need the root node modules where packages are hoisted
const nodeModules = fileURLToPath(
  new URL('./node_modules', import.meta.url)
)

export default defineNuxtConfig({
  hooks: {
    // cannot be added in nuxt's resolve.alias
    'vite:extendConfig': (config, { isServer }) => {
      if (isServer) {
        config.resolve ??= {}
        config.resolve.alias ??= {}
        // @ts-ignore
        config.resolve.alias['firebase/firestore'] = resolve(
          nodeModules,
          'firebase/firestore/dist/index.mjs'
        )
        // @ts-ignore
        config.resolve.alias['@firebase/firestore'] = resolve(
          nodeModules,
          '@firebase/firestore/dist/index.node.mjs'
        )

        // add any other firebase alias you need
      }
    },
  },
})

I opened

But I think this error comes either from vite or from the firebase packages' package.json exports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants