Skip to content

Commit

Permalink
Update CrossOriginWorker to work with type=module (#1219)
Browse files Browse the repository at this point in the history
  • Loading branch information
whitphx authored Jan 9, 2025
1 parent 7ea8350 commit c3f27d9
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/kernel/src/cross-origin-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

const workerBlobUrlCache = new Map<string, string>();

function getWorkerBlobUrl(url: URL): string {
function getWorkerBlobUrl(url: URL, isModule = false): string {
const urlStr = url.toString();

const cached = workerBlobUrlCache.get(urlStr);
Expand All @@ -19,8 +19,12 @@ function getWorkerBlobUrl(url: URL): string {
return cached;
}

const workerBlob = new Blob([`importScripts("${urlStr}");`], {
type: "text/javascript",
const workerCode = isModule
? `import "${urlStr}";`
: `importScripts("${urlStr}");`;

const workerBlob = new Blob([workerCode], {
type: isModule ? "text/javascript;type=module" : "text/javascript",
});
const workerBlobUrl = URL.createObjectURL(workerBlob);
workerBlobUrlCache.set(urlStr, workerBlobUrl);
Expand Down Expand Up @@ -52,7 +56,10 @@ export class CrossOriginWorkerMaker {
// so we can't catch the error synchronously.
} else {
console.debug(`Loading a worker script from a different origin: ${url}`);
const workerBlobUrl = getWorkerBlobUrl(url);
const workerBlobUrl = getWorkerBlobUrl(
url,
workerOptions.type === "module",
);
this.worker = shared
? new SharedWorker(workerBlobUrl, workerOptions)
: new Worker(workerBlobUrl, workerOptions);
Expand Down

0 comments on commit c3f27d9

Please sign in to comment.