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

Rewrite hasLocalStorage check to not produce localStorage-entries #612

Merged
merged 10 commits into from
Mar 26, 2024

Conversation

Andr3wid
Copy link
Contributor

@Andr3wid Andr3wid commented Mar 25, 2024

Description

Existence of the localStorage API was probed so far by creating (and immediately deleting) a localStorage-entry. This is undesirable behaviour in case a page wishes to not have the localStorage-API utilized in any way.

This PR introduces the following changes:

  • Determine existence of the localStorage-API by checking if getItem and setItem functions are available instead of setting a storage item
  • Handle exceptions directly in the getItem/setItem wrapper functions (e.g. if access is denied or quota got exceeded)

Anyways, a localStorage item would still be set if certain feature of the player-UI are used. PR is a followup to introduce disabling of localStorage.

Checklist (for PR submitter and reviewers)

  • CHANGELOG entry

@Andr3wid Andr3wid self-assigned this Mar 25, 2024
@Andr3wid Andr3wid marked this pull request as ready for review March 25, 2024 15:31
Copy link
Contributor

@felix-hoc felix-hoc left a comment

Choose a reason for hiding this comment

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

nice, just some suggestions :)

Comment on lines 21 to 23
if (StorageUtils.isLocalStorageAvailable()) {
window.localStorage.setItem(key, data);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

the isLocalStorageAvailable() check does not require try-catch, that's already done internally. So we could pull it out of the try block and do an early return, or wrap the whole try-catch block with it. (same for getItem())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, you are right. I thought that, when browser prohibits access, exceptions are triggered when actually trying to call getItem / setItem, but it already happens when trying to access window.localStorage. Hence, it's already covered in isLocalStorageAvailable.

I removed the try-catches in this commit: Removed superfluous try-catch

Let's have a call if you meant it in a different way :)

return window.localStorage.getItem(key);
}
} catch (e) {
console.warn(`Failed to retrieve storage entry for ${key}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO warn is too high of a log level, as we will always check for certain localStorage keys. I'd suggest debug or removing the log completely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the log entirely as well in Removed superfluous try-catch. Just not a fan of empty catch blocks :)

src/ts/storageutils.ts Outdated Show resolved Hide resolved
@Andr3wid Andr3wid requested a review from felix-hoc March 26, 2024 08:22
@Andr3wid Andr3wid merged commit 3f93565 into develop Mar 26, 2024
2 checks passed
@Andr3wid Andr3wid deleted the fix/PW-10974/localstorage-check branch March 26, 2024 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants