diff --git a/src/lib/main/snippet.ts b/src/lib/main/snippet.ts index eaec17d4..9251f028 100644 --- a/src/lib/main/snippet.ts +++ b/src/lib/main/snippet.ts @@ -108,7 +108,7 @@ export function snippet( // remove any previously patched functions if (top == win) { (config!.forward || []).map(function (forwardProps) { - const { property } = resolvePartytownForwardProperty(forwardProps); + const [property] = resolvePartytownForwardProperty(forwardProps); delete win[property.split('.')[0] as any]; }); } @@ -141,7 +141,7 @@ export function snippet( // this is the top window // patch the functions that'll be forwarded to the worker (config.forward || []).map(function (forwardProps) { - const { property, preserveBehavior } = resolvePartytownForwardProperty(forwardProps); + const [property, { preserveBehavior }] = resolvePartytownForwardProperty(forwardProps); mainForwardFn = win; property.split('.').map(function (_, i, forwardPropsArr) { mainForwardFn = mainForwardFn[forwardPropsArr[i]] = diff --git a/src/lib/sandbox/main-forward-trigger.ts b/src/lib/sandbox/main-forward-trigger.ts index a098a880..2aaef22b 100644 --- a/src/lib/sandbox/main-forward-trigger.ts +++ b/src/lib/sandbox/main-forward-trigger.ts @@ -26,7 +26,7 @@ export const mainForwardTrigger = (worker: PartytownWebWorker, $winId$: WinId, w win._ptf = undefined; forwards.map((forwardProps) => { - const { property, preserveBehavior } = resolvePartytownForwardProperty(forwardProps); + const [property, { preserveBehavior }] = resolvePartytownForwardProperty(forwardProps); mainForwardFn = win; property.split('.').map((_, i, arr) => { mainForwardFn = mainForwardFn[arr[i]] = diff --git a/src/lib/types.ts b/src/lib/types.ts index e212c334..9613764c 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -522,7 +522,11 @@ export interface PartytownConfig { nonce?: string; } -export type PartytownForwardSettingsProperty = { property: string; preserveBehavior: boolean }; +export type PartytownForwardPropertySettings = { + preserveBehavior?: boolean; +}; + +export type PartytownForwardPropertyWithSettings = [string, PartytownForwardPropertySettings?]; /** * A forward property to patch on `window`. The forward config property is an string, @@ -532,7 +536,7 @@ export type PartytownForwardSettingsProperty = { property: string; preserveBehav * * @public */ -export type PartytownForwardProperty = string | PartytownForwardSettingsProperty; +export type PartytownForwardProperty = string | PartytownForwardPropertyWithSettings; /** * @public diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 5cdde5d3..2ed2d59a 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -2,7 +2,8 @@ import type { ApplyPath, MainWindow, PartytownForwardProperty, - PartytownForwardSettingsProperty, + PartytownForwardPropertySettings, + PartytownForwardPropertyWithSettings, RandomId, StringIndexable, } from './types'; @@ -145,16 +146,19 @@ export const isValidUrl = (url: any): boolean => { } }; +const defaultPartytownForwardPropertySettings: Required = { + preserveBehavior: false, +}; + export const resolvePartytownForwardProperty = ( - property: PartytownForwardProperty -): PartytownForwardSettingsProperty => { - if (typeof property === 'string') { - return { - property, - preserveBehavior: false, - }; + propertyOrPropertyWithSettings: PartytownForwardProperty +): Required => { + if (typeof propertyOrPropertyWithSettings === 'string') { + return [propertyOrPropertyWithSettings, defaultPartytownForwardPropertySettings]; } - return property; + const [property, settings = defaultPartytownForwardPropertySettings] = + propertyOrPropertyWithSettings; + return [property, { ...defaultPartytownForwardPropertySettings, ...settings }]; }; type GetOriginalBehaviorReturn = { diff --git a/tests/integrations/event-forwarding/index.html b/tests/integrations/event-forwarding/index.html index 41d6b048..2b7780ce 100644 --- a/tests/integrations/event-forwarding/index.html +++ b/tests/integrations/event-forwarding/index.html @@ -12,7 +12,7 @@ 'superDuperFunction', 'superArray.push', 'KiwiSizing', - { property: 'superPreservedArray.unshift', preserveBehavior: true }, + ['superPreservedArray.unshift', { preserveBehavior: true }], ], logCalls: true, logGetters: true, diff --git a/tests/integrations/gtm/preserve-behavior.html b/tests/integrations/gtm/preserve-behavior.html index 0ea58eca..1f1ab1fc 100644 --- a/tests/integrations/gtm/preserve-behavior.html +++ b/tests/integrations/gtm/preserve-behavior.html @@ -21,7 +21,7 @@ return url; }, - forward: [{ property: 'dataLayer.push', preserveBehavior: true }], + forward: [['dataLayer.push', { preserveBehavior: true }]], logCalls: true, logGetters: true, logSetters: true,