Skip to content

[馃悶] Fallback deletes dataLayer even when dataLayer.push is forwarded with preserveBehavior: true聽#789

Closed

Description

@ArmenMesropyanOptimax

Describe the bug
When Partytown falls back to the main thread (the service worker is unavailable or fallbackTimeout is reached), fallback() deletes the top-level global of every forward entry:

delete win[property.split('.')[0]];

With forward: [['dataLayer.push', { preserveBehavior: true }]], pushes made before the fallback go into the real window.dataLayer, which is the point of preserveBehavior. The fallback then deletes window.dataLayer. GTM runs again on the main thread starting from an empty array, so everything pushed before it is lost (consent defaults, page data, early events).

Follow-up to #596, as requested in #596 (comment). I'd like to submit a PR for this.

Reproduction
https://stackblitz.com/edit/gdc63npv-vugcjapu?file=index.html

Steps to reproduce

  1. Open the reproduction. swPath points to a missing file, so the service worker never registers and the fallback runs after fallbackTimeout: 500.
  2. Two entries are pushed into dataLayer before the fallback.
  3. A text/partytown script, standing in for the GTM snippet, records what's in window.dataLayer when it runs on the main thread.

Expected: entriesSeenByGtm contains the 2 entries pushed before the fallback.
Actual: entriesSeenByGtm is []. ga, forwarded without preserveBehavior, is deleted as expected.

Browser Info
Chrome 155, macOS. Partytown 1.0.0, service worker mode. The fallback code doesn't depend on the browser.

Additional Information
Proposed fix: in fallback(), don't delete a top-level object when a nested property under it is forwarded with preserveBehavior (keep dataLayer for dataLayer.push). Top-level stubs like fbq should still be deleted so their snippet can set them up again. We currently ship this as a pnpm patch.

Activity

  1. ArmenMesropyanOptimax commented on Oct 14, 2026

    @ArmenMesropyanOptimax
    ContributorAuthor

    @gioboa Hi! Can you please take a look? 馃檹

    Also I have raised a PR #790

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions