From ddbf17aefb8169a225cefd691d519f218bcf217f Mon Sep 17 00:00:00 2001 From: EricHeitmuller-Gusto <153567695+EricHeitmuller-Gusto@users.noreply.github.com> Date: Thu, 27 Jun 2024 23:10:39 -0700 Subject: [PATCH] Fix ResizeObserver initialization if document.body does not exist yet (close #1311) PR #1322 --- ...ore-document-body-ready_2024-06-27-05-58.json | 10 ++++++++++ .../src/helpers/browser_props.ts | 3 +++ .../test/browser_props.test.ts | 16 +++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 common/changes/@snowplow/browser-tracker-core/issue-1311-reszieobserver-invoked-before-document-body-ready_2024-06-27-05-58.json diff --git a/common/changes/@snowplow/browser-tracker-core/issue-1311-reszieobserver-invoked-before-document-body-ready_2024-06-27-05-58.json b/common/changes/@snowplow/browser-tracker-core/issue-1311-reszieobserver-invoked-before-document-body-ready_2024-06-27-05-58.json new file mode 100644 index 000000000..af201dff9 --- /dev/null +++ b/common/changes/@snowplow/browser-tracker-core/issue-1311-reszieobserver-invoked-before-document-body-ready_2024-06-27-05-58.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@snowplow/browser-tracker-core", + "comment": "Fix ResizeObserver initialization if document.body does not exist yet (#1311)", + "type": "none" + } + ], + "packageName": "@snowplow/browser-tracker-core" +} \ No newline at end of file diff --git a/libraries/browser-tracker-core/src/helpers/browser_props.ts b/libraries/browser-tracker-core/src/helpers/browser_props.ts index 41250364a..4a247550d 100644 --- a/libraries/browser-tracker-core/src/helpers/browser_props.ts +++ b/libraries/browser-tracker-core/src/helpers/browser_props.ts @@ -22,6 +22,9 @@ function initializeResizeObserver() { if (resizeObserverInitialized) { return; } + if(!document || !document.body || !document.documentElement) { + return; + } resizeObserverInitialized = true; const resizeObserver = new ResizeObserver((entries) => { diff --git a/libraries/browser-tracker-core/test/browser_props.test.ts b/libraries/browser-tracker-core/test/browser_props.test.ts index 73a456e51..238c99ecb 100644 --- a/libraries/browser-tracker-core/test/browser_props.test.ts +++ b/libraries/browser-tracker-core/test/browser_props.test.ts @@ -1,4 +1,4 @@ -import { floorDimensionFields } from '../src/helpers/browser_props'; +import { floorDimensionFields, getBrowserProperties } from '../src/helpers/browser_props'; describe('Browser props', () => { it('floorDimensionFields correctly floors dimension type values', () => { @@ -10,4 +10,18 @@ describe('Browser props', () => { const testFractionalDimensions = '100.2x100.1'; expect(floorDimensionFields(testFractionalDimensions)).toEqual('100x100'); }); + + describe('#getBrowserProperties', () => { + describe('with undefined document', () => { + beforeAll(() => { + // @ts-expect-error + document = undefined; + }); + + it('does not invoke the resize observer if the document is null', () => { + const browserProperties = getBrowserProperties(); + expect(browserProperties).not.toEqual(null); + }); + }); + }); });