Skip to content

Account for a navigator that has a key of serviceWorker but an undefined value #6723

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

Merged
merged 1 commit into from
Oct 25, 2022
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: 5 additions & 0 deletions .changeset/dirty-eggs-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/performance': patch
---

Expand check in `getServiceWorkerStatus` to account for a `navigator` that has a key of `serviceWorker` with a falsy value
14 changes: 11 additions & 3 deletions packages/performance/src/utils/attribute_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ import '../../test/setup';

describe('Firebase Performance > attribute_utils', () => {
describe('#getServiceWorkerStatus', () => {
it('returns unsupported when service workers is in navigator but has a falsy value', () => {
stub(Api, 'getInstance').returns({
navigator: { serviceWorker: undefined }
} as unknown as Api);

expect(getServiceWorkerStatus()).to.be.eql(1 /** UNSUPPORTED */);
});

it('returns unsupported when service workers unsupported', () => {
stub(Api, 'getInstance').returns({
navigator: {}
} as unknown as Api);

expect(getServiceWorkerStatus()).to.be.eql(1);
expect(getServiceWorkerStatus()).to.be.eql(1 /** UNSUPPORTED */);
});

it('returns controlled when service workers controlled', () => {
Expand All @@ -49,7 +57,7 @@ describe('Firebase Performance > attribute_utils', () => {
}
} as unknown as Api);

expect(getServiceWorkerStatus()).to.be.eql(2);
expect(getServiceWorkerStatus()).to.be.eql(2 /** CONTROLLED */);
});

it('returns uncontrolled when service workers uncontrolled', () => {
Expand All @@ -59,7 +67,7 @@ describe('Firebase Performance > attribute_utils', () => {
}
} as unknown as Api);

expect(getServiceWorkerStatus()).to.be.eql(3);
expect(getServiceWorkerStatus()).to.be.eql(3 /** UNCONTROLLED */);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/performance/src/utils/attributes_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const MAX_ATTRIBUTE_VALUE_LENGTH = 100;

export function getServiceWorkerStatus(): ServiceWorkerStatus {
const navigator = Api.getInstance().navigator;
if ('serviceWorker' in navigator) {
if (navigator?.serviceWorker) {
if (navigator.serviceWorker.controller) {
return ServiceWorkerStatus.CONTROLLED;
} else {
Expand Down