Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(use-storage): using with SSR #2053

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/use-storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@
typeof window.sessionStorage !== 'undefined'
)

const createMemoryStorage = (): Storage => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing unit test on this part

const storage: { [key: string]: string | null } = {}

Check warning on line 28 in packages/use-storage/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/use-storage/src/index.ts#L28

Added line #L28 was not covered by tests

return {

Check warning on line 30 in packages/use-storage/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/use-storage/src/index.ts#L30

Added line #L30 was not covered by tests
length: Object.entries(storage).length,
clear() {
Object.keys(storage).forEach(key => {
delete storage[key]

Check warning on line 34 in packages/use-storage/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/use-storage/src/index.ts#L32-L34

Added lines #L32 - L34 were not covered by tests
})
},
key(index) {

Check warning on line 37 in packages/use-storage/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/use-storage/src/index.ts#L37

Added line #L37 was not covered by tests
return Object.values(storage)[index] ?? null
},
getItem(key) {

Check warning on line 40 in packages/use-storage/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/use-storage/src/index.ts#L40

Added line #L40 was not covered by tests
return storage[key] ?? null
},
setItem(key, value) {
storage[key] = value

Check warning on line 44 in packages/use-storage/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/use-storage/src/index.ts#L43-L44

Added lines #L43 - L44 were not covered by tests
},
removeItem(key) {
delete storage[key]

Check warning on line 47 in packages/use-storage/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/use-storage/src/index.ts#L46-L47

Added lines #L46 - L47 were not covered by tests
},
}
}

const getStorage = (name: 'localStorage' | 'sessionStorage') =>
canUseDOM ? window[name] : createMemoryStorage()

const subscribeStorage = (callback: () => void) => {
if (canUseDOM) {
window.addEventListener('storage', callback)
Expand All @@ -47,7 +75,9 @@
): [T | null, (value: T | undefined) => void] => {
const storage = useMemo(
() =>
options?.kind === 'session' ? window.sessionStorage : window.localStorage,
options?.kind === 'session'
? getStorage('sessionStorage')
: getStorage('localStorage'),
[options?.kind],
)

Expand Down
Loading