Skip to content

Commit 1899ba4

Browse files
committed
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
1 parent fcc098a commit 1899ba4

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/components/GraphiQL.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export class GraphiQL extends React.Component {
132132

133133
// Utility for keeping CodeMirror correctly sized.
134134
this.codeMirrorSizer = new CodeMirrorSizer();
135+
136+
global.g = this;
135137
}
136138

137139
componentWillReceiveProps(nextProps) {
@@ -185,7 +187,7 @@ export class GraphiQL extends React.Component {
185187
// that when the component is remounted, it will use the last used values.
186188
componentWillUnmount() {
187189
this._storageSet('query', this.state.query);
188-
this._storageSet('variables', this.state.variables || '');
190+
this._storageSet('variables', this.state.variables);
189191
this._storageSet('operationName', this.state.operationName);
190192
this._storageSet('editorFlex', this.state.editorFlex);
191193
this._storageSet('variableEditorHeight', this.state.variableEditorHeight);
@@ -418,12 +420,24 @@ export class GraphiQL extends React.Component {
418420
}
419421

420422
_storageGet(name) {
421-
return this._storage && this._storage.getItem('graphiql:' + name);
423+
if (this._storage) {
424+
const value = this._storage.getItem('graphiql:' + name);
425+
// Clean up any inadvertently saved null/undefined values.
426+
if (value === 'null' || value === 'undefined') {
427+
this._storage.removeItem('graphiql:' + name);
428+
} else {
429+
return value;
430+
}
431+
}
422432
}
423433

424434
_storageSet(name, value) {
425435
if (this._storage) {
426-
this._storage.setItem('graphiql:' + name, value);
436+
if (value) {
437+
this._storage.setItem('graphiql:' + name, value);
438+
} else {
439+
this._storage.removeItem('graphiql:' + name);
440+
}
427441
}
428442
}
429443

0 commit comments

Comments
 (0)