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
1 change: 1 addition & 0 deletions .dictionary
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ webpack
async
queueing
LocalStorage
ssr
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

* [#1733](https://github.com/mozilla/glean.js/pull/1733): Add SSR support for Glean.js
* [#1728](https://github.com/mozilla/glean.js/pull/1728): Migrate client_id and first_run_date.
* [#1695](https://github.com/mozilla/glean.js/pull/1695): Update Glean.js web to use LocalStorage.

Expand Down
9 changes: 8 additions & 1 deletion glean/src/core/internal_metrics/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { InternalDatetimeMetricType as DatetimeMetricType } from "../metrics/typ
import { InternalStringMetricType as StringMetricType } from "../metrics/types/string.js";
import { createMetric } from "../metrics/utils.js";
import TimeUnit from "../metrics/time_unit.js";
import { generateUUIDv4 } from "../utils.js";
import { generateUUIDv4, isWindowObjectUnavailable } from "../utils.js";
import { Lifetime } from "../metrics/lifetime.js";
import log, { LoggingLevel } from "../log.js";
import { Context } from "../context.js";
Expand Down Expand Up @@ -126,6 +126,13 @@ export class CoreMetricsSync {
}

initialize(): void {
// The "sync" version of Glean.js is only meant to be used in the browser.
// If we cannot access the window object, then we are unable to store
// any of the metric data in `localStorage`.
if (isWindowObjectUnavailable()) {
return;
}

// If the client had used previous versions of Glean.js before we moved
// to LocalStorage as the data store, then we need to move important
// user data from IndexedDB to LocalStorage.
Expand Down
12 changes: 12 additions & 0 deletions glean/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,15 @@ export function getCurrentTimeInNanoSeconds(): number {
}
return now;
}

/**
* Checks if the current environment has access to the `window` object. This
* check is used to conditional-ize browser code for SSR projects. If the
* platform does not have access to the `window` APIs, then we are unable to
* store data in the browser.
*
* @returns Whether or not the current platform has access to the `window` object.
*/
export function isWindowObjectUnavailable(): boolean {
return typeof window === "undefined";
}
7 changes: 6 additions & 1 deletion glean/src/platform/browser/web/platform_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { KnownOperatingSystems } from "../../../core/platform_info/shared.js";

const BrowserPlatformInfo: PlatformInfoSync = {
os(): KnownOperatingSystems {
const ua = navigator.userAgent;
let ua;
if (!!navigator && !!navigator.userAgent) {
ua = navigator.userAgent;
} else {
ua = KnownOperatingSystems.Unknown;
}

if (ua.includes("Windows")) {
return KnownOperatingSystems.Windows;
Expand Down
14 changes: 14 additions & 0 deletions glean/src/platform/browser/web/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import {
getValueFromNestedObject,
updateNestedObject
} from "../../../core/storage/utils.js";
import { isWindowObjectUnavailable } from "../../../core/utils.js";

const LOG_TAG = "platform.web.Storage";

// If `window.localStorage` is unavailable, we return undefined for all.
class WebStore implements SynchronousStore {
private logTag: string;

Expand All @@ -23,6 +25,10 @@ class WebStore implements SynchronousStore {
}

get(index: StorageIndex = []): JSONValue | undefined {
if (isWindowObjectUnavailable()) {
return;
}

let result;

try {
Expand All @@ -42,6 +48,10 @@ class WebStore implements SynchronousStore {
}

update(index: StorageIndex, transformFn: (v?: JSONValue) => JSONValue): void {
if (isWindowObjectUnavailable()) {
return;
}

try {
const json = localStorage.getItem(this.rootKey) || "{}";
const obj = JSON.parse(json) as JSONObject;
Expand All @@ -54,6 +64,10 @@ class WebStore implements SynchronousStore {
}

delete(index: StorageIndex): void {
if (isWindowObjectUnavailable()) {
return;
}

try {
const json = localStorage.getItem(this.rootKey) || "{}";
const obj = JSON.parse(json) as JSONObject;
Expand Down