An example of using dynamic remote federated modules in web workers, inspired
by this example.
More specifically, the dynamic module loading utilities in
packages/main/src/utils/
are adapted from these utilities
packages/main
is a web app with a web workerpackages/remote
a utility that posts a messagepackages/shared
a utility with a counter. The counter is used in both the main and remote modules
There are two separate re-implementations of these utilities from the reference example, because these lines provide an api that cannot be exactly recapitulated in a web worker. Specifically:
-
The code in
packages/remote/src/utils/getOrLoadComponent.js
is essentially just a transliteration or the original code from the reference implementation. Specifically, it has exactly the same API, but suffers from the fact thatimportScripts
is synchronous which means the worker is not free to load another component in paralell. -
The code in
packages/main/src/utils/getOrLoadComponent.js
allows for (down)loading multiple packages in parallel, taking advantage of the fact that additional arguments toimportscripts()
are downloaded in parallel, much like the multiple<script>
tags that would be created with multiple asynchronous calls to the original method. This requires a bit more work to keep track of the already-in-progress downloads (using a dictionary of promises instead of a document full of<script>
tags). This is exposed in theloadComponents()
function, illustrated inpackages/main/src/worker.js
.
- Run
yarn
to install dependencies. - Run
yarn build
to build the packages. - Run
yarn serve
to serve prebuilt dist (oryarn start
to serve from source). - Visit the app at
localhost:3001