Skip to content

Commit

Permalink
Protectionon global document variable
Browse files Browse the repository at this point in the history
  • Loading branch information
MacFJA committed Mar 13, 2021
1 parent 67d4923 commit 4fc7a7b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.0.2]

### Fixed

- Add protection on global `document` variable

## [1.0.1]

### Added
Expand All @@ -20,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

First version

[Unreleased]: https://github.com/MacFJA/svelte-persistent-store/compare/1.0.1...HEAD
[1.0.1]: https://github.com/MacFJA/svelte-persistent-store/releases/tag/1.0.1
[Unreleased]: https://github.com/MacFJA/svelte-persistent-store/compare/1.0.2...HEAD
[1.0.2]: https://github.com/MacFJA/svelte-persistent-store/compare/1.0.1...1.0.2
[1.0.1]: https://github.com/MacFJA/svelte-persistent-store/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/MacFJA/svelte-persistent-store/releases/tag/1.0.0
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@macfja/svelte-persistent-store",
"version": "1.0.1",
"version": "1.0.2",
"description": "A Svelte store that keep its value through pages and reloads",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,33 @@ function getBrowserStorage(browserStorage: Storage): StorageInterface<any> {
* Storage implementation that use the browser local storage
*/
export function localStorage<T>(): StorageInterface<T> {
if (typeof window !== 'undefined' && window?.localStorage) {
if (typeof window !== "undefined" && window?.localStorage) {
return getBrowserStorage(window.localStorage)
}
console.warn("Unable to find the localStorage. No data will be persisted.")
return noopStorage()
}

/**
* Storage implementation that use the browser session storage
*/
export function sessionStorage<T>(): StorageInterface<T> {
if (typeof window !== 'undefined' && window?.sessionStorage) {
if (typeof window !== "undefined" && window?.sessionStorage) {
return getBrowserStorage(window?.sessionStorage)
}
console.warn("Unable to find the sessionStorage. No data will be persisted.")
return noopStorage()
}

/**
* Storage implementation that use the browser cookies
*/
export function cookieStorage(): StorageInterface<any> {
if (typeof document === "undefined" || typeof document?.cookie !== "string") {
console.warn("Unable to find the cookies. No data will be persisted.")
return noopStorage()
}

return {
getValue(key:string): any | null {
if (!Cookies.hasItem(key)) {
Expand Down

0 comments on commit 4fc7a7b

Please sign in to comment.