From 00641e2a5be791f1ee4e935e30015a6bf00959e7 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 23 Jun 2023 13:09:05 +0200 Subject: [PATCH] ref(replay): More graceful `sessionStorage` check (#8394) It seems in some environments `sessionStorage` my be unset to `null` or similar. To be extra careful, we can guard for existence as well there. Fixes https://github.com/getsentry/sentry-javascript/issues/8392 --- packages/replay/src/session/clearSession.ts | 5 ++--- packages/replay/src/session/fetchSession.ts | 5 ++--- packages/replay/src/session/saveSession.ts | 4 ++-- packages/replay/src/util/hasSessionStorage.ts | 6 ++++++ 4 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 packages/replay/src/util/hasSessionStorage.ts diff --git a/packages/replay/src/session/clearSession.ts b/packages/replay/src/session/clearSession.ts index d084764c2fb9..78f50255e363 100644 --- a/packages/replay/src/session/clearSession.ts +++ b/packages/replay/src/session/clearSession.ts @@ -1,5 +1,6 @@ import { REPLAY_SESSION_KEY, WINDOW } from '../../src/constants'; import type { ReplayContainer } from '../../src/types'; +import { hasSessionStorage } from '../util/hasSessionStorage'; /** * Removes the session from Session Storage and unsets session in replay instance @@ -13,9 +14,7 @@ export function clearSession(replay: ReplayContainer): void { * Deletes a session from storage */ function deleteSession(): void { - const hasSessionStorage = 'sessionStorage' in WINDOW; - - if (!hasSessionStorage) { + if (!hasSessionStorage()) { return; } diff --git a/packages/replay/src/session/fetchSession.ts b/packages/replay/src/session/fetchSession.ts index 4b4b1eccf530..3e89a9cbd049 100644 --- a/packages/replay/src/session/fetchSession.ts +++ b/packages/replay/src/session/fetchSession.ts @@ -1,14 +1,13 @@ import { REPLAY_SESSION_KEY, WINDOW } from '../constants'; import type { Session } from '../types'; +import { hasSessionStorage } from '../util/hasSessionStorage'; import { makeSession } from './Session'; /** * Fetches a session from storage */ export function fetchSession(): Session | null { - const hasSessionStorage = 'sessionStorage' in WINDOW; - - if (!hasSessionStorage) { + if (!hasSessionStorage()) { return null; } diff --git a/packages/replay/src/session/saveSession.ts b/packages/replay/src/session/saveSession.ts index 8f75d0ab50ed..d868fd6ea8a1 100644 --- a/packages/replay/src/session/saveSession.ts +++ b/packages/replay/src/session/saveSession.ts @@ -1,12 +1,12 @@ import { REPLAY_SESSION_KEY, WINDOW } from '../constants'; import type { Session } from '../types'; +import { hasSessionStorage } from '../util/hasSessionStorage'; /** * Save a session to session storage. */ export function saveSession(session: Session): void { - const hasSessionStorage = 'sessionStorage' in WINDOW; - if (!hasSessionStorage) { + if (!hasSessionStorage()) { return; } diff --git a/packages/replay/src/util/hasSessionStorage.ts b/packages/replay/src/util/hasSessionStorage.ts new file mode 100644 index 000000000000..f242df101c25 --- /dev/null +++ b/packages/replay/src/util/hasSessionStorage.ts @@ -0,0 +1,6 @@ +import { WINDOW } from '../constants'; + +/** If sessionStorage is available. */ +export function hasSessionStorage(): boolean { + return 'sessionStorage' in WINDOW && !!WINDOW.sessionStorage; +}