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.88k stars 427 forks source link

Recaptcha v3 fails with "Error: mozRequestAnimationFrame not on global?", any help? #209

Open gremo opened 2 years ago

gremo commented 2 years ago

This is my first time with partytown. I'm try to load Recaptcha v3 script into the worker thread.

This is the partytown configuration in the <head>:

<script>
  partytown = {
    resolveUrl(url, location) {
      if ([
        'www.google.com',
      ].includes(url.hostname)) {
        const proxyUrl = new URL("https://cdn.builder.io/api/v1/proxy-api");
        proxyUrl.searchParams.append("url", url);

        return proxyUrl;
      }
    },
    debug: true,
  };
</script>

And I'm loading Recaptcha v3 at the <body> bottom (site key XXX):

<script type="text/partytown" src="https://www.google.com/recaptcha/api.js?render=XXX&hl=it"></script>

The Recaptcha script seems to load fine (no CORS errors)... but as soon as it loads, I get the error:

Error: mozRequestAnimationFrame not on global?

Recatpcha error

Adding forward: ["mozRequestAnimationFrame"] doesn't help at all.

charisTheo commented 2 years ago

+1

Replicated the issue:

forward: ["mozRequestAnimationFrame"] doesn't seem to work for me either.


UPDATE:

I tweaked a local copy of Partytown to include all global functions Recaptcha required, inside of worker-window.ts:392:

win.mozRequestAnimationFrame = win.webkitRequestAnimationFrame = win.webkitAnimationFrame = win.msRequestAnimationFrame = win.requestAnimationFrame = (cb: (ts: number) => void) =>
          setTimeout(() => cb(performance.now()), 9);

win.webkitCancelRequestAnimationFrame = win.webkitCancelAnimationFrame = win.cancelAnimationFrame = (id: number) => clearTimeout(id);

But then I get this error from Recaptcha:

TypeError: Failed to execute 'postMessage' on 'MessagePort': Overload resolution failed.

This looks similar to issue #208 where spawning workers within workers is not allowed. Can someone please provide more details about why workers within workers are not allowed?

LynxTR commented 1 year ago

Is there any update or workaround about this?