BuilderIO / partytown

Relocate resource intensive third-party scripts off of the main thread and into a web worker. 🎉
https://partytown.builder.io
MIT License
12.85k stars 426 forks source link

Uncaught TypeError: Cannot convert undefined or null to object - partytown-sandbox-sw.js #198

Open jhonatantft opened 2 years ago

jhonatantft commented 2 years ago

Describe the bug I tried to integrate partytown to an ecommerce website (VTEX platform) following the HTML integration doc, and all the resources are being loaded but I'm getting this error that is preventing the code to be executed.

Uncaught TypeError: Cannot convert undefined or null to object
    at Function.getPrototypeOf (<anonymous>)
    at readOwnImplementation (partytown-sandbox-sw.js?v=0.6.1:476:38)
    at partytown-sandbox-sw.js?v=0.6.1:462:75
    at Array.map (<anonymous>)
    at readImplementations (partytown-sandbox-sw.js?v=0.6.1:462:19)
    at readMainPlatform (partytown-sandbox-sw.js?v=0.6.1:434:27)
    at partytown-sandbox-sw.js?v=0.6.1:526:56
    at worker.onmessage (partytown-sandbox-sw.js?v=0.6.1:536:69)

Partytown version "@builder.io/partytown": "^0.6.1"

Screenshots

image

Additional context

The code breaks because it tries to use the Object.getPrototypeOf with an undefined type as you can see in the screenshot below. The method readOwnImplementation from partytown-sandbox-sw.js tries to get the prototype but it does not have any validation before doing it, so it breaks. I highlighted the method in yellow and where the param is coming from.

image

jhonatantft commented 2 years ago

Similar issue but different problem: https://github.com/BuilderIO/partytown/issues/38

mhevery commented 2 years ago

Thanks, we are running a bit behind right now with a related project. Is this still an issue?

jhonatantft commented 2 years ago

Yes, it is @mhevery

adamdbradley commented 1 year ago

Are you able to debug which API is crashing? What's the code running before this error happens?

AkshayKumar007 commented 1 year ago

Hey, just wanted to confirm whether the open bug is related to the issue I'm facing while integrating Partytown in my React Application. I'm getting non stop errors getting printed in console causing my app to crash. It's mainly due to my app not being able to find the third party scripts since I changed their type to text/partytown. The errors are mostly like: Uncaught TypeError: Cannot read properties of undefined (reading 'then'), TypeError: Cannot read properties of undefined (reading 'apply'), etc.

I included the following script at top after loading partytown in public/ directory.

<script src="~partytown/partytown.js"></script>

image

joepagan commented 1 year ago

@AkshayKumar007 I just had that issue where I was forwarding both datalayer and fbq events, however, I wasn't loading the facebook tracking script, only GA. I would still says that's a bug though.

chasegiunta commented 1 year ago

I've got partytown working almost flawlessly if not for this issue. For us, it's stemming from calling gtag/window.gtag, like gtag('config', ...options...) or gtag('set', ...options...). Both gtag & window.gtag (just to be safe) are listed in the partytown forward array.

jon301 commented 1 year ago

Same issue here: image

I have used the "Copy Library Files" instructions in my NextJS application.

Then configured partytown like this, as i'm using google analytics, gtm and fb pixel in my app:

import { Partytown } from "@builder.io/partytown/react";

...

<Partytown
  forward={["dataLayer.push", "fbq", "gtag"]}
  resolveUrl={(url, location, type) => {
    // ...
  }
/>