Permalink
Browse files

Ensure saving a non-value to storage actually removes it.

localStorage coerces everything to a String before saving it, so setting undefined results in getting "undefined". This change causes falsey values to be removed rather than being string coerced. It also introduces a fix to dirty storage from this issue.

Fixes #234
  • Loading branch information...
1 parent fcc098a commit 1899ba4937b14423bf67a54f421cf26a51d42a79 @leebyron leebyron committed Jan 10, 2017
Showing with 17 additions and 3 deletions.
  1. +17 −3 src/components/GraphiQL.js
@@ -132,6 +132,8 @@ export class GraphiQL extends React.Component {
// Utility for keeping CodeMirror correctly sized.
this.codeMirrorSizer = new CodeMirrorSizer();
+
+ global.g = this;
}
componentWillReceiveProps(nextProps) {
@@ -185,7 +187,7 @@ export class GraphiQL extends React.Component {
// that when the component is remounted, it will use the last used values.
componentWillUnmount() {
this._storageSet('query', this.state.query);
- this._storageSet('variables', this.state.variables || '');
+ this._storageSet('variables', this.state.variables);
this._storageSet('operationName', this.state.operationName);
this._storageSet('editorFlex', this.state.editorFlex);
this._storageSet('variableEditorHeight', this.state.variableEditorHeight);
@@ -418,12 +420,24 @@ export class GraphiQL extends React.Component {
}
_storageGet(name) {
- return this._storage && this._storage.getItem('graphiql:' + name);
+ if (this._storage) {
+ const value = this._storage.getItem('graphiql:' + name);
+ // Clean up any inadvertently saved null/undefined values.
+ if (value === 'null' || value === 'undefined') {
+ this._storage.removeItem('graphiql:' + name);
+ } else {
+ return value;
+ }
+ }
}
_storageSet(name, value) {
if (this._storage) {
- this._storage.setItem('graphiql:' + name, value);
+ if (value) {
+ this._storage.setItem('graphiql:' + name, value);
+ } else {
+ this._storage.removeItem('graphiql:' + name);
+ }
}
}

0 comments on commit 1899ba4

Please sign in to comment.