Skip to content

Commit 203225d

Browse files
Fix ResizeObserver initialization if document.body does not exist yet (close #1311)
PR #1322
1 parent bea8358 commit 203225d

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-tracker-core",
5+
"comment": "Fix ResizeObserver initialization if document.body does not exist yet (#1311)",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker-core"
10+
}

libraries/browser-tracker-core/src/helpers/browser_props.ts

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ function initializeResizeObserver() {
2222
if (resizeObserverInitialized) {
2323
return;
2424
}
25+
if(!document || !document.body || !document.documentElement) {
26+
return;
27+
}
2528
resizeObserverInitialized = true;
2629

2730
const resizeObserver = new ResizeObserver((entries) => {

libraries/browser-tracker-core/test/browser_props.test.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { floorDimensionFields } from '../src/helpers/browser_props';
1+
import { floorDimensionFields, getBrowserProperties } from '../src/helpers/browser_props';
22

33
describe('Browser props', () => {
44
it('floorDimensionFields correctly floors dimension type values', () => {
@@ -10,4 +10,18 @@ describe('Browser props', () => {
1010
const testFractionalDimensions = '100.2x100.1';
1111
expect(floorDimensionFields(testFractionalDimensions)).toEqual('100x100');
1212
});
13+
14+
describe('#getBrowserProperties', () => {
15+
describe('with undefined document', () => {
16+
beforeAll(() => {
17+
// @ts-expect-error
18+
document = undefined;
19+
});
20+
21+
it('does not invoke the resize observer if the document is null', () => {
22+
const browserProperties = getBrowserProperties();
23+
expect(browserProperties).not.toEqual(null);
24+
});
25+
});
26+
});
1327
});

0 commit comments

Comments
 (0)