From ff7d8ef156d0a59dba26d9e637c6b15fb3079479 Mon Sep 17 00:00:00 2001 From: Sean Parmelee Date: Tue, 2 Nov 2021 03:13:28 -0500 Subject: [PATCH] fix: allow amplitude to be imported/required during SSR (#436) --- src/index.js | 2 +- src/language.js | 3 ++- src/localstorage.js | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 2cfc8c22..239d0b2a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ // Entry point import Amplitude from './amplitude'; -const old = window.amplitude || {}; +const old = (typeof window !== 'undefined' && window.amplitude) || {}; const newInstance = new Amplitude(); newInstance._q = old._q || []; diff --git a/src/language.js b/src/language.js index e8b7409e..3780b8fd 100644 --- a/src/language.js +++ b/src/language.js @@ -1,6 +1,7 @@ var getLanguage = function () { return ( - (navigator && ((navigator.languages && navigator.languages[0]) || navigator.language || navigator.userLanguage)) || + (typeof navigator !== 'undefined' && + ((navigator.languages && navigator.languages[0]) || navigator.language || navigator.userLanguage)) || '' ); }; diff --git a/src/localstorage.js b/src/localstorage.js index 6d5b4f37..9cf2aed4 100644 --- a/src/localstorage.js +++ b/src/localstorage.js @@ -26,7 +26,7 @@ if (BUILD_COMPAT_LOCAL_STORAGE) { if (windowLocalStorageAvailable()) { localStorage = window.localStorage; - } else if (window.globalStorage) { + } else if (typeof window !== 'undefined' && window.globalStorage) { // Firefox 2-3 use globalStorage // See https://developer.mozilla.org/en/dom/storage#globalStorage try {