Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(code-block): fix local-storage breakage (#735)
* fix(code-block): check before accessing local storage * chore(code-block): add changeset
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hashicorp/react-code-block': patch | ||
--- | ||
|
||
Fixes an issue where code-block would throw an error and potentially break the consuming app if local storage is disabled. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Returns `true` if window.localStorage can be used, | ||
* or `false` otherwise. | ||
* | ||
* Based on: | ||
* https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js | ||
*/ | ||
function isLocalStorageAvailable() { | ||
const test = 'test' | ||
try { | ||
window.localStorage.setItem(test, test) | ||
window.localStorage.getItem(test) | ||
window.localStorage.removeItem(test) | ||
return true | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
|
||
/** | ||
* Gets an item from local storage, if available. | ||
* Safely returns `null` if local storage is unavailable. | ||
*/ | ||
function getItem(key: string) { | ||
if (!isLocalStorageAvailable()) return null | ||
return window.localStorage.getItem(key) | ||
} | ||
|
||
/** | ||
* Sets an item in local storage. | ||
* Safely returns `null` if local storage is unavailable. | ||
*/ | ||
function setItem(key: string, value: string) { | ||
if (!isLocalStorageAvailable()) return null | ||
return window.localStorage.setItem(key, value) | ||
} | ||
|
||
const safeLocalStorage = { getItem, setItem } | ||
|
||
export default safeLocalStorage |
0aa2389
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
react-components – ./
react-components-hashicorp.vercel.app
react-components.vercel.app
react-components-git-main-hashicorp.vercel.app