-
Notifications
You must be signed in to change notification settings - Fork 221
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM👏👏
@@ -3,6 +3,7 @@ export interface Extension { | |||
name: string; | |||
region: ExtensionRegion; | |||
url: string; | |||
type?: 'iframe' | 'worker'; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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}")`, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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);
});
There was a problem hiding this comment.
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
What?
Create new method to register extension web worker
Why?
Support building checkout extension using web worker.
Testing / Proof
@bigcommerce/team-checkout