You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I host a multi-threaded package on platforms like jsDelivr with proper cross-origin isolation settings, I encounter an error such as:
Uncaught SecurityError: Failed to construct 'Worker': Script at 'https://cdn.jsdelivr.net/npm/xxx' cannot be accessed from origin 'http://127.0.0.1:8080'.
This issue arises because workers cannot be created across different origins. According to the discussion in this Webpack issue and the solution proposed in this comment, we can use fetch to bypass this limitation.
A potential solution is modify the allocateUnusedWorker function from
allocateUnusedWorker(){varworkerOptions={type: "module",name: "em-pthread"};try{// Use XMLHttpRequest for a synchronous requestvarxhr=newXMLHttpRequest();xhr.open("GET",newURL('your js file',import.meta.url),false);// false means synchronous requestxhr.send(null);// Check if the request was successfulif(xhr.status===200){constscriptText=xhr.responseText;// Get the response text// Create a Blob URLconstworkerUrl=URL.createObjectURL(newBlob([scriptText],{type: 'application/javascript'}));// Create a Worker and add it to the unused Workers listPThread.unusedWorkers.push(newWorker(workerUrl,workerOptions));}else{console.error('Failed to fetch the script:',xhr.status);}}catch(error){console.error("Error occurred while allocating worker:",error);}}
The text was updated successfully, but these errors were encountered:
DylanShang
changed the title
Support for CND resource of pthread worker
Support for CDN resource of pthread worker
Sep 29, 2024
Hi, developers
When I host a multi-threaded package on platforms like jsDelivr with proper cross-origin isolation settings, I encounter an error such as:
Uncaught SecurityError: Failed to construct 'Worker': Script at 'https://cdn.jsdelivr.net/npm/xxx' cannot be accessed from origin 'http://127.0.0.1:8080'.
This issue arises because workers cannot be created across different origins. According to the discussion in this Webpack issue and the solution proposed in this comment, we can use fetch to bypass this limitation.
A potential solution is modify the
allocateUnusedWorker
function fromto
The text was updated successfully, but these errors were encountered: