Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased changes

* [#1977](https://github.com/mozilla/glean.js/pull/1977): Add try-catch around window.sessionStorage access to prevent uncaught error upon `import`.

[Full changelog](https://github.com/mozilla/glean.js/compare/v5.0.3...main)

# v5.0.3 (2024-08-02)
Expand Down
17 changes: 13 additions & 4 deletions glean/src/core/glean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,19 @@ declare global {
}

// Only set `Glean` values whenever running inside of a browser.
if (
typeof window !== "undefined" &&
typeof window.sessionStorage !== "undefined"
) {
// When cookies/localStorage/sessionStorage are disabled (at least in Firefox
// and Chrome), an access attempt on the window.sessionStorage property will
// throw an error ("The operation is insecure." in Firefox).
// So try-catch is needed here.
let hasStorage = false;
try {
hasStorage = typeof window !== "undefined" &&
typeof window.sessionStorage !== "undefined";
} catch (e) {
console.error("No session storage available", e);
}

if (hasStorage) {
window.Glean = {
setLogPings: (flag: boolean) => {
setDebugOptionInSessionStorage(DebugOption.LogPings, flag);
Expand Down