Skip to content

Commit

Permalink
DEV-613 Fix localStorage issue
Browse files Browse the repository at this point in the history
Fixed issue where feature detection for localStorage was incorrectly applied. Due to the different browser configurations and security settings that can involve localStorage, feature detection needs to be placed within a try/catch block.
  • Loading branch information
originalwebgurl committed Nov 10, 2021
1 parent 0be86b2 commit 5783879
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "battlesnake-board",
"version": "1.1.2",
"version": "1.1.3",
"private": true,
"dependencies": {
"@emotion/react": "11.4.1",
Expand Down
26 changes: 26 additions & 0 deletions src/app/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function storageAvailable(type) {
let storage;
try {
storage = window[type];
const x = "__storage_test__";
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch (e) {
return (
((e instanceof DOMException &&
// everything except Firefox
e.code === 22) ||
// Firefox
e.code === 1014 ||
// test name field too, because code might not be present
// everything except Firefox
e.name === "QuotaExceededError" ||
// Firefox
e.name === "NS_ERROR_DOM_QUOTA_REACHED") &&
// acknowledge QuotaExceededError only if there's something already stored
storage &&
storage.length !== 0
);
}
}
11 changes: 10 additions & 1 deletion src/components/game/GamePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ import {
stepBackwardFrame,
highlightSnake
} from "../../actions";
import { storageAvailable } from "../../app/storage";

const options = parseQueryString(window.location.search);
let storedSettings = {};
if (storageAvailable("localStorage")) {
storedSettings = window.localStorage;
} else {
console.info(
"Please enable localStorage for an improved experience that allows you to persist board settings."
);
}

const mapStateToProps = state => {
const gameState = state.game;
return {
options: { ...window.localStorage, ...options },
options: { ...storedSettings, ...options },
ruleset: gameState.ruleset,
grid: gameState.grid,
gameNotFound: gameState.gameNotFound,
Expand Down
5 changes: 3 additions & 2 deletions src/components/settings/settings-slice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createSlice } from "@reduxjs/toolkit";
import { storageAvailable } from "../../app/storage";

const initialState = {
frameRate: Number(getLocalSetting("frameRate")) || 10,
Expand Down Expand Up @@ -29,13 +30,13 @@ export const currentFrameRate = state => state.settings.frameRate;
export const currentTheme = state => state.settings.theme;

function getLocalSetting(key) {
if (window.localStorage) {
if (storageAvailable("localStorage")) {
return window.localStorage.getItem(key);
}
}

function setLocalSetting(key, value) {
if (window.localStorage) {
if (storageAvailable("localStorage")) {
window.localStorage.setItem(key, value);
}
}
Expand Down

0 comments on commit 5783879

Please sign in to comment.