firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Can't add performance monitoring to SSR web app

Open patrickgordon opened this issue 7 years ago • 9 comments

[REQUIRED] Describe your environment

  • Operating System version: Ubuntu 18.04
  • Browser version: Chrome 70.x.x
  • Firebase SDK version: ^6.1.1
  • Firebase Product: performance

[REQUIRED] Describe the problem

When using SSR, specifically, Next.js, and importing performance from the library, the server explodes due to trying to access window.

Steps to reproduce:

SSR rendered app with Next.js

Add the below code to the usual imports and setup for firebase.

import "firebase/performance";

Results in:

[ error ] ReferenceError: window is not defined
    at Object.<anonymous> (/home/patrick/sites/interior-mood-boards/node_modules/@firebase/performance/dist/index.cjs.js:1196:1)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/patrick/sites/interior-mood-boards/node_modules/firebase/performance/dist/index.cjs.js:3:1)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)

patrickgordon avatar Jul 10 '19 08:07 patrickgordon

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Jul 10 '19 08:07 google-oss-bot

@patrickgordon Same issue in my VueJs module here https://github.com/lupas/nuxt-fire/issues/31#issuecomment-510875394, couldn't figure out how to make it work yet.

I do a conditional import only on client-side (which might be the solution for you too), but for some reason firebase/performance does not get imported or added to the Firebase object.

Would be interested if there is a solution to this.

lupas avatar Jul 12 '19 12:07 lupas

@lupas I also tried that but had no success.

patrickgordon avatar Jul 14 '19 06:07 patrickgordon

Have you tried dynamic imports? Maybe something like this would work: dynamic(() => import('firebase/performance'), { ssr: false });

jpbow avatar Jul 15 '19 09:07 jpbow

Any update on this? I have a React App with SSR, downloaded 6.3.1 and I also got the window is not defined. What is the workaround? Because its not like I can never have a service worker if i want to do Firebase Performance right?

ralcar avatar Jul 23 '19 09:07 ralcar

Use Dynamic Imports import() to import the Performance Monitoring library in the ComponentDidMount() life cycle of pages/index.js so that it will only import when it's rendering on the client side.

Remember to check for !firebase.apps.length before you initialize Firebase or you may run into an error of trying to initialize multiple Firebase instances.

I'm currently working on a Next.js project and this works for me. Hope this helps.

ylihao avatar Jul 24 '19 03:07 ylihao

Issue seems to be fixed with Firebase v.6.3.2+. I at least got it to work now in my VueJs/Nuxt module. <:o)

lupas avatar Jul 27 '19 06:07 lupas

Yes, the issue should be fixed in the last release. Thanks for reporting the issue.

alikn avatar Jul 30 '19 21:07 alikn

Nice, thanks for the quick turn around.

patrickgordon avatar Jul 30 '19 21:07 patrickgordon