3

I'm using React, Webpack and adopting the Micro-frontend model. I have two simple apps as follows:

  1. App A (Shell app)
  2. App B (Remote app).

App B uses a simple Web Worker as follows:
new Worker(new URL("./myworker.ts", import.meta.url))

App B runs normally, but when running on App A, it crashes:
Uncaught SecurityError: Failed to construct 'Worker': Script at 'appB.com/worker.js' cannot be accessed from origin 'appA.com'

I know there is a method that is to use Blob, but in this case, I don't know how to do that because Worker is on App B itself.
One more thing, I want App B to be able to run both standalone and to be able to run well on shell app (App A).
Is there any solution? Thank

mikenlanggio
  • 1,122
  • 1
  • 7
  • 27

2 Answers2

0

Solution 1: Use Blob Which creates a file-like object in memory. Blob object can create a Web Worker.

Blob object is not subject to the same security restrictions as a regular file aka you can create a Web Worker from a Blob even if the two origins are different.

const workerBlob = new Blob([workerSource],               {type: "application/javascript"});
  const worker = new Worker(workerBlob);

Solution 2: Use Service Workers as ways to create background workers that can run even when the user is not actively using the application. Use Service Workers to create a Web Worker that is accessible from both App A and App B.

const sw = new ServiceWorker();
  sw.register("./serviceworker.js");

  const worker = new Worker("/serviceworker.js/worker.js");

same advantage above as Blob

Solution 3: Use Cross-Origin Resource Sharing (CORS) to allow the worker script to be loaded from a different origin. This can be done by adding the headers to the worker script:

Access-Control-Allow-Origin: *
  Access-Control-Allow-Headers: Content-Type

references :

user1874594
  • 2,277
  • 1
  • 25
  • 49
-1

to web Worker to function properly within your micro-frontend architecture, you can use the technique of creating a Blob URL. In App B, instead of directly passing the script URL to the Worker constructor, you can create a Blob URL for the worker script. This allows the worker to run in the context of App A while maintaining its independence.

const workerScript = `
  // Contents of myworker.ts
  self.onmessage = function (e) {
    // Worker logic
    self.postMessage("Message received by the worker");
  };
`;

const blob = new Blob([workerScript], { type: "application/javascript" });
const workerUrl = URL.createObjectURL(blob);

new Worker(workerUrl);

the worker script from myworker.ts is embedded within the workerScript string. This string is then converted into a Blob object and assigned a URL using URL.createObjectURL(). Finally, the Worker constructor is provided with this created URL.

  • 2
    Welcome to Stack Overflow! Most or all of your (currently) 19 answers (and most all of your comment replies as well) appear likely to have been entirely or partially written by AI (e.g., ChatGPT), and **far** too many of the ones I've checked so far appear to have errors that have been pointed out in the comments. Please be aware that [posting of AI-generated content is banned here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. Thanks! – NotTheDr01ds Jun 26 '23 at 21:38
  • 2
    **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jun 26 '23 at 21:38