jehna / ga-lite

Small, cacheable and open version of Google Analytics JS client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SSR issue introduced

mcrawshaw opened this issue · comments

ReferenceError: window is not defined

Was introduced with verison 2.0.4.

Thank you for reporting! I'll check this asap. @mcrawshaw Do you have a more detailed stack trace to help debuggning?

Sorry, here's the full stack trace:

  ReferenceError: window is not defined
      at Object.<anonymous> (/Users/markcrawshaw/Code/metamap/node_modules/ga-lite/dist/ga-lite.min.js:1:194)
      at Module._compile (internal/modules/cjs/loader.js:778:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
      at Module.load (internal/modules/cjs/loader.js:653:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
      at Function.Module._load (internal/modules/cjs/loader.js:585:3)
      at Module.require (internal/modules/cjs/loader.js:692:17)
      at require (internal/modules/cjs/helpers.js:25:18)
      at eval (webpack-internal:///ga-lite:1:18)
      at Object.ga-lite (/Users/markcrawshaw/Code/metamap/dist/src/app.js:18371:1)

Just to note this is happening on module load. It's fine to have uncondition use of window once the module is loaded, but nasty if it happens while the module is loading. This prefix works typeof window !== "undefined && window.[do stuff]".

Thank you, the stack trace helped a lot!

I think I managed to find the root cause: I just updated webpack to latest version. The last working version (2.0.1) did use this as the global object:

https://github.com/jehna/ga-lite/blob/2.0.1/dist/ga-lite.js#L10

...while the latest version uses window:

https://github.com/jehna/ga-lite/blob/2.0.4/dist/ga-lite.js#L10

I think webpack had changed the default configuration to window at some point, as their documentation now states:

When targeting a library, especially the libraryTarget is'umd', this option indicates what global object will be used to mount the library. To make UMD build available on both browsers and Node.js, set output.globalObject option to 'this'.

Source: https://webpack.js.org/configuration/output/#outputglobalobject

I'll push a new version soon, and I'm hoping changing the global object to this will fix this issue. I'd appreciate if you could verify the new version with your setup too once I push it live :)

Ok, version 2.0.5 is now available from NPM. @mcrawshaw could you verify that this version fixes your issue?

That did it, testing and working. Thanks for the super-fast response!