Skip to content

Commit 645cfbe

Browse files
author
Dimitri Kopriwa
committed
fix(polyfill): using try catch to determine if browser or node environment
1 parent d4eee4c commit 645cfbe

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,32 @@ const defaultOptions = {
3131
*/
3232
export function polyfill(options) {
3333
const opts = { ...defaultOptions, ...options };
34+
let root = null;
35+
try {
36+
root = window || global;
37+
} catch (e) {
38+
root = global;
39+
}
3440
if (opts.isomorphicFetch) {
3541
const Tough = require('tough-cookie');
3642
const Store = new Tough.MemoryCookieStore();
3743
const cookieJar = new Tough.CookieJar(Store);
3844
const fetch = require('fetch-cookie')(require('isomorphic-fetch'), cookieJar);
39-
global.fetch = fetch;
40-
global.cookieJar = cookieJar;
45+
root.fetch = fetch;
46+
root.cookieJar = cookieJar;
4147
require('whatwg-fetch');
4248
}
4349

4450
if (opts.fetch && !opts.isomorphicFetch) {
4551
require('whatwg-fetch');
46-
global.fetchMock = require('fetch-mock');
52+
root.fetchMock = require('fetch-mock');
4753
}
4854

49-
if (opts.localStorage && !global.localStorage) {
50-
global.localStorage = require('localStorage');
55+
if (root.localStorage && !root.localStorage) {
56+
root.localStorage = require('localStorage');
5157
}
5258

5359
if (opts.media) {
54-
const root = window || global;
5560
root.matchMedia = root.matchMedia
5661
|| function () {
5762
return {

0 commit comments

Comments
 (0)