Skip to content

Commit

Permalink
fix: 🐛 Guard the use of window object during Gatsby build
Browse files Browse the repository at this point in the history
Used advice in this issue: gatsbyjs/gatsby#309
  • Loading branch information
jonpepler committed Mar 21, 2020
1 parent 6a02930 commit 0289601
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -5,6 +5,7 @@
"version": "0.2.0",
"author": "Jon Pepler",
"dependencies": {
"@loadable/component": "^5.12.0",
"gatsby": "^2.19.45",
"gatsby-image": "^2.2.44",
"gatsby-plugin-manifest": "^2.2.48",
Expand All @@ -13,6 +14,7 @@
"gatsby-plugin-sharp": "^2.4.13",
"gatsby-source-filesystem": "^2.1.56",
"gatsby-transformer-sharp": "^2.3.19",
"localstorage-memory": "^1.0.3",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-color": "^2.18.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/asteroids.js
@@ -1,5 +1,5 @@
import React from 'react'
import Sketch from 'react-p5'
import {loadableSketch as Sketch} from './loadable-react-p5'

import { get } from '../services/storage'

Expand Down
2 changes: 2 additions & 0 deletions src/components/loadable-react-p5.js
@@ -0,0 +1,2 @@
import Loadable from "@loadable/component"
export const loadableSketch = Loadable(() => import('react-p5'))
11 changes: 9 additions & 2 deletions src/services/storage.js
@@ -1,4 +1,11 @@
import localStorageMemory from 'localstorage-memory'
import { getDefault } from './defaults'

export const get = key => localStorage.getItem(key) || getDefault(key) || ''
export const set = (key, value) => localStorage.setItem(key, value)
// Used to handle lack of window object in Gatsby build
const windowGlobal = typeof window !== 'undefined' && window
const storage = windowGlobal ?
windowGlobal.localStorage :
localStorageMemory
;
export const get = key => storage.getItem(key) || getDefault(key) || ''
export const set = (key, value) => storage.setItem(key, value)

0 comments on commit 0289601

Please sign in to comment.