Skip to content

feat(checkout): create new method to register extension web worker #2869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2025

Conversation

richapatel1510
Copy link
Contributor

@richapatel1510 richapatel1510 commented May 19, 2025

What?

Create new method to register extension web worker

Why?

Support building checkout extension using web worker.

Testing / Proof

  • CI checks

@bigcommerce/team-checkout

@richapatel1510 richapatel1510 marked this pull request as ready for review May 19, 2025 06:15
@richapatel1510 richapatel1510 requested a review from a team as a code owner May 19, 2025 06:15
Copy link
Contributor

@bc-peng bc-peng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM👏👏

@richapatel1510 richapatel1510 merged commit 0f70f55 into master May 27, 2025
8 checks passed
@richapatel1510 richapatel1510 deleted the extension-webworker-method branch May 27, 2025 00:45
@@ -3,6 +3,7 @@ export interface Extension {
name: string;
region: ExtensionRegion;
url: string;
type?: 'iframe' | 'worker';
Copy link
Contributor

@animesh1987 animesh1987 May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we type this as an enum, and then we can use the value here https://github.com/bigcommerce/checkout-sdk-js/pull/2869/files extension-action-creator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do that in my next open PR #2876

const blob = URL.createObjectURL(
new Blob(
[
`importScripts=((i)=>(...a)=>i(...a.map((u)=>''+new URL(u,"${url}"))))(importScripts);importScripts("${url}")`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I ask what this does?

Copy link
Contributor

@animesh1987 animesh1987 May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidchin can you please take a look at this as well.

On a quick search on internet I can see this as a thing as well

fetch('https://test.com/script.js')
  .then((response) => response.text())
  .then((code) => {
    const blob = new Blob([code], { type: 'application/javascript' });
    const url = URL.createObjectURL(blob);
    const worker = new Worker(url);

    worker.onmessage = (e) => {
      console.log('Worker says:', e.data);
    };

    worker.postMessage('Hello from main thread');

    // Optional: revoke the URL when done
    // URL.revokeObjectURL(url);
  });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mainly because we are creating a web worker from a script url that is not on our origin, due to which we get a CORS error when we try to create worker directly by doing new Worker(new URL(url)).

Ref: https://github.com/jantimon/remote-web-worker/blob/main/src/index.ts

Related threads: https://stackoverflow.com/questions/21913673/execute-web-worker-from-different-origin/62914052#62914052
https://stackoverflow.com/questions/58098143/why-are-cross-origin-workers-blocked-and-why-is-the-workaround-ok/60015898#60015898

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants