Skip to content

Commit 3029503

Browse files
authored
Merge pull request #195 from blargity/master
Server side rendering fixes for Gatsby.
2 parents 13f5ee8 + 9693b6c commit 3029503

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

dist/lib/GoogleApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
var googleVersion = opts.version || '3.31';
4040

4141
var script = null;
42-
var google = window.google || null;
42+
var google = typeof window !== 'undefined' && window.google || null;
4343
var loading = false;
4444
var channel = null;
4545
var language = opts.language;

dist/lib/ScriptCache.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151

5252
Cache._scriptTag = function (key, src) {
5353
if (!scriptMap.has(key)) {
54+
// Server side rendering environments don't always have access to the `document` global.
55+
// In these cases, we're not going to be able to return a script tag, so just return null.
56+
if (typeof document === 'undefined') return null;
57+
5458
var tag = document.createElement('script');
5559
var promise = new Promise(function (resolve, reject) {
5660
var resolved = false,

src/lib/GoogleApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const GoogleApi = function(opts) {
1616
const googleVersion = opts.version || '3.31';
1717

1818
let script = null;
19-
let google = window.google || null;
19+
let google = typeof window !== 'undefined' && window.google || null;
2020
let loading = false;
2121
let channel = null;
2222
let language = opts.language;

src/lib/ScriptCache.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export const ScriptCache = (function(global) {
3535

3636
Cache._scriptTag = (key, src) => {
3737
if (!scriptMap.has(key)) {
38+
// Server side rendering environments don't always have access to the `document` global.
39+
// In these cases, we're not going to be able to return a script tag, so just return null.
40+
if (typeof document === 'undefined') return null;
41+
3842
let tag = document.createElement('script');
3943
let promise = new Promise((resolve, reject) => {
4044
let resolved = false,

0 commit comments

Comments
 (0)