Skip to content

Commit

Permalink
Ensure saving a non-value to storage actually removes it.
Browse files Browse the repository at this point in the history
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
leebyron committed Jan 10, 2017
1 parent fcc098a commit 1899ba4
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/components/GraphiQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 1899ba4

Please sign in to comment.