Skip to content

Commit 00641e2

Browse files
authored
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 #8392
1 parent fc7ef13 commit 00641e2

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

packages/replay/src/session/clearSession.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { REPLAY_SESSION_KEY, WINDOW } from '../../src/constants';
22
import type { ReplayContainer } from '../../src/types';
3+
import { hasSessionStorage } from '../util/hasSessionStorage';
34

45
/**
56
* Removes the session from Session Storage and unsets session in replay instance
@@ -13,9 +14,7 @@ export function clearSession(replay: ReplayContainer): void {
1314
* Deletes a session from storage
1415
*/
1516
function deleteSession(): void {
16-
const hasSessionStorage = 'sessionStorage' in WINDOW;
17-
18-
if (!hasSessionStorage) {
17+
if (!hasSessionStorage()) {
1918
return;
2019
}
2120

packages/replay/src/session/fetchSession.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { REPLAY_SESSION_KEY, WINDOW } from '../constants';
22
import type { Session } from '../types';
3+
import { hasSessionStorage } from '../util/hasSessionStorage';
34
import { makeSession } from './Session';
45

56
/**
67
* Fetches a session from storage
78
*/
89
export function fetchSession(): Session | null {
9-
const hasSessionStorage = 'sessionStorage' in WINDOW;
10-
11-
if (!hasSessionStorage) {
10+
if (!hasSessionStorage()) {
1211
return null;
1312
}
1413

packages/replay/src/session/saveSession.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { REPLAY_SESSION_KEY, WINDOW } from '../constants';
22
import type { Session } from '../types';
3+
import { hasSessionStorage } from '../util/hasSessionStorage';
34

45
/**
56
* Save a session to session storage.
67
*/
78
export function saveSession(session: Session): void {
8-
const hasSessionStorage = 'sessionStorage' in WINDOW;
9-
if (!hasSessionStorage) {
9+
if (!hasSessionStorage()) {
1010
return;
1111
}
1212

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { WINDOW } from '../constants';
2+
3+
/** If sessionStorage is available. */
4+
export function hasSessionStorage(): boolean {
5+
return 'sessionStorage' in WINDOW && !!WINDOW.sessionStorage;
6+
}

0 commit comments

Comments
 (0)