Skip to content

Commit

Permalink
ref(replay): More graceful sessionStorage check (#8394)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mydea authored Jun 23, 2023
1 parent fc7ef13 commit 00641e2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
5 changes: 2 additions & 3 deletions packages/replay/src/session/clearSession.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions packages/replay/src/session/fetchSession.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/replay/src/session/saveSession.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/replay/src/util/hasSessionStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { WINDOW } from '../constants';

/** If sessionStorage is available. */
export function hasSessionStorage(): boolean {
return 'sessionStorage' in WINDOW && !!WINDOW.sessionStorage;
}

0 comments on commit 00641e2

Please sign in to comment.