Skip to content

Commit 0c5a427

Browse files
committed
Isomorphic-render: Add guards around global objects
1 parent 82d848f commit 0c5a427

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

src/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* global __VERSION__ */
21
var React = require('react')
32
var Link = require('react-router/lib/Link')
43

@@ -19,10 +18,12 @@ var App = React.createClass({
1918
SettingsStore.load()
2019
StoryStore.loadSession()
2120
UpdatesStore.loadSession()
21+
if (typeof window === 'undefined') return
2222
window.addEventListener('beforeunload', this.handleBeforeUnload)
2323
},
2424

2525
componentWillUnmount() {
26+
if (typeof window === 'undefined') return
2627
window.removeEventListener('beforeunload', this.handleBeforeUnload)
2728
},
2829

@@ -60,7 +61,6 @@ var App = React.createClass({
6061
{this.props.children}
6162
</div>
6263
<div className="App__footer">
63-
{`react-hn v${__VERSION__} | `}
6464
<a href="https://github.com/insin/react-hn">insin/react-hn</a>
6565
</div>
6666
</div>

src/stores/StoryStore.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class StoryStore extends EventEmitter {
9999
start() {
100100
firebaseRef = HNService.storiesRef(this.type)
101101
firebaseRef.on('value', this.onStoriesUpdated)
102+
if (typeof window === 'undefined') return
102103
window.addEventListener('storage', this.onStorage)
103104
}
104105

@@ -107,6 +108,7 @@ class StoryStore extends EventEmitter {
107108
firebaseRef.off()
108109
firebaseRef = null
109110
}
111+
if (typeof window === 'undefined') return
110112
window.removeEventListener('storage', this.onStorage)
111113
}
112114
}
@@ -124,6 +126,7 @@ extend(StoryStore, {
124126
* Deserialise caches from sessionStorage.
125127
*/
126128
loadSession() {
129+
if (typeof window === 'undefined') return
127130
idCache = parseJSON(window.sessionStorage.idCache, {})
128131
itemCache = parseJSON(window.sessionStorage.itemCache, {})
129132
},
@@ -132,6 +135,7 @@ extend(StoryStore, {
132135
* Serialise caches to sessionStorage as JSON.
133136
*/
134137
saveSession() {
138+
if (typeof window === 'undefined') return
135139
window.sessionStorage.idCache = JSON.stringify(idCache)
136140
window.sessionStorage.itemCache = JSON.stringify(itemCache)
137141
}

src/stores/UpdatesStore.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ function handleUpdateItems(items) {
100100

101101
var UpdatesStore = extend(new EventEmitter(), {
102102
loadSession() {
103+
if (typeof window === 'undefined') return
103104
var json = window.sessionStorage.updates
104105
updatesCache = (json ? JSON.parse(json) : {comments: {}, stories: {}})
105106
populateUpdates()
106107
},
107108

108109
saveSession() {
110+
if (typeof window === 'undefined') return
109111
window.sessionStorage.updates = JSON.stringify(updatesCache)
110112
},
111113

src/utils/setTitle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var {SITE_TITLE} = require('./constants')
22

33
function setTitle(title) {
4+
if (typeof document === 'undefined') return
45
document.title = (title ? title + ' | ' + SITE_TITLE : SITE_TITLE)
56
}
67

src/utils/storage.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
module.exports = {
22
get(key, defaultValue) {
3-
var value = window.localStorage[key]
4-
return (typeof value != 'undefined' ? value : defaultValue)
3+
if (typeof window !== 'undefined') {
4+
var value = window.localStorage[key]
5+
return (typeof value != 'undefined' ? value : defaultValue)
6+
}
57
},
68
set(key, value) {
7-
window.localStorage[key] = value
9+
if (typeof window !== 'undefined') {
10+
window.localStorage[key] = value
11+
}
812
}
913
}
14+

0 commit comments

Comments
 (0)