Skip to content

Commit

Permalink
Data: Fix persistence handling of null value
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Aug 3, 2018
1 parent 2085eed commit 94a1f4c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/data/src/plugins/persistence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,19 @@ export function createPersistenceInterface( options ) {
*/
function get() {
if ( data === undefined ) {
// If unset, getItem is expected to return null. Fall back to
// empty object.
const persisted = storage.getItem( storageKey );
try {
data = JSON.parse( persisted );
} catch ( error ) {
if ( persisted === null ) {
data = {};
} else {
try {
data = JSON.parse( persisted );
} catch ( error ) {
// Similarly, should any error be thrown during parse of
// the string (malformed JSON), fall back to empty object.
data = {};
}
}
}

Expand Down

0 comments on commit 94a1f4c

Please sign in to comment.