Can't add performance monitoring to SSR web app
[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)
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
@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 I also tried that but had no success.
Have you tried dynamic imports?
Maybe something like this would work: dynamic(() => import('firebase/performance'), {
ssr: false
});
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?
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.
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)
Yes, the issue should be fixed in the last release. Thanks for reporting the issue.
Nice, thanks for the quick turn around.