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

ref(replay): More graceful sessionStorage check #8394

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
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
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;
Copy link
Member

Choose a reason for hiding this comment

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

l: do we even need the in check?

Copy link
Member Author

Choose a reason for hiding this comment

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

TBH I was wondering the same thing, I vaguely remember that sometimes (??) WINDOW.xxx check may fail, but not 100% sure anymore under which circumstances that would happen 🤔 we use the in syntax for all our window checks, as far as I can tell...!

Copy link
Member

Choose a reason for hiding this comment

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

Ahh I see. Better safe than sorry, was just wondering :)

}