Skip to content

[Performance] do not access window before initialization #2003

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

Merged
merged 2 commits into from
Jul 18, 2019
Merged

Conversation

alikn
Copy link
Contributor

@alikn alikn commented Jul 17, 2019

No description provided.

@alikn alikn requested a review from zijianjoy as a code owner July 17, 2019 21:05
@alikn alikn requested a review from Feiyang1 July 17, 2019 21:06
@@ -36,6 +36,10 @@ export function registerPerformance(instance: FirebaseNamespace): void {
if (app.name !== DEFAULT_ENTRY_NAME) {
throw ERROR_FACTORY.create(ErrorCode.FB_NOT_DEFAULT);
}
if (!window) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will still throw exception in Nodejs. typeof window !== undefined should work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! typeof returns a string. Updated the condition.

@alikn alikn merged commit 663f3e9 into master Jul 18, 2019
@imjared
Copy link

imjared commented Aug 5, 2019

this seems to still noisily throw errors in a window-less environment. for example, we're using in a next.js app and it just throws errors and prevents the app from initializing. is there another way to handle initialization that wouldn't throw but just silently fail if in a server environemnt?

{
  ReferenceError: window is not defined
    at Object.<anonymous> (../index.ts:51:0)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> ([...]/node_modules/firebase/performance/dist/index.cjs.js:3:1)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18) sourceMapsApplied: true }

@alikn
Copy link
Contributor Author

alikn commented Aug 5, 2019

Performance sdk can be imported but should not be initialized in window-less environments. I suggest to make the initialization of Performance (firebase.performance()) conditional.

@firebase firebase locked and limited conversation to collaborators Oct 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants