Skip to content

Commit f09b0cb

Browse files
zgreenskipjack
authored andcommitted
fix(notification-bar): test for localStorage before using it (#1249)
1 parent 8f93c76 commit f09b0cb

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

components/notification-bar/notification-bar.jsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React from 'react';
22
import Container from '../container/container';
3+
import testLocalStorage from '../../utilities/test-local-storage';
34

45
const version = '1';
6+
const localStorageIsEnabled = testLocalStorage() !== false;
57

68
export default class NotificationBar extends React.Component {
79
render() {
@@ -15,10 +17,12 @@ export default class NotificationBar extends React.Component {
1517
</p>
1618
<p>
1719
Buy the brand-new webpack stickers at <a href="http://www.unixstickers.com/tag/webpack">Unixstickers!</a>
18-
19-
<i
20-
className="notification-bar__close icon-cross"
21-
onClick={ this._close.bind(this) } />
20+
{localStorageIsEnabled ?
21+
<i
22+
className="notification-bar__close icon-cross"
23+
onClick={ this._close.bind(this) } /> :
24+
null
25+
}
2226
</p>
2327
</Container>
2428
</div>
@@ -31,7 +35,9 @@ export default class NotificationBar extends React.Component {
3135
* @param {object} e - Click event
3236
*/
3337
_close(e) {
34-
localStorage.setItem('notification-dismissed', version);
38+
if (localStorageIsEnabled) {
39+
localStorage.setItem('notification-dismissed', version);
40+
}
3541
this.forceUpdate();
3642
}
3743

@@ -41,7 +47,7 @@ export default class NotificationBar extends React.Component {
4147
* @return {boolean} - Whether or not the current message was dismissed
4248
*/
4349
get _dismissed() {
44-
if (typeof window !== 'undefined') {
50+
if (localStorageIsEnabled) {
4551
return localStorage.getItem('notification-dismissed') === version;
4652

4753
} else return false;

utilities/test-local-storage.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Test if localStorage is enabled.
3+
*
4+
* {@link https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js}
5+
* @return {undefined|bool} Returns false on error.
6+
*/
7+
module.exports = function() {
8+
const test = "localStorageTest";
9+
try {
10+
localStorage.setItem(test, test);
11+
localStorage.removeItem(test);
12+
} catch (e) {
13+
return false;
14+
}
15+
};

0 commit comments

Comments
 (0)