Replies: 4 comments 7 replies
-
Hi, @koengsen. I'm not familiar with module federation but I'm certain that if you're making an HTTP request in your container ( Could you try generating two workers—one for your remote and one for your container?
You'd have to follow with the same Module federation support is not present at the moment, but we'd love to use this opportunity to learn more about what it'd mean for the library. For instance, I'm curious how exactly the remote is exposed to the container? Is it some sort of proxy? |
Beta Was this translation helpful? Give feedback.
-
There's is also a discussion here, referencing for context & also potential for folks to add on the conversation |
Beta Was this translation helpful? Give feedback.
-
We got this working with MSW v1 (I am bit late as it seems like v2 will be released next monday 😅). It's not that complicated, but you need to be set up properly to make it work. The idea is that your remote modules should be provided a mecanism to register their MSW request handlers with the container application. Once all the remote modules are done registering their request handlers, then the container application can start MSW with the provided request handlers. To do this, we managed to get all our remote modules to expose a {
plugins: [
new ModuleFederationPlugin({
name: "remote1",
filename: "remoteEntry.js",
exposes: {
"./register": "./src/register.js"
}
})
]
} With that register function, the remodule modules can register their request handlers with the container application. Let's say that you have a import { rest } from "msw";
export const requestHandlers = [
rest.get("/api/character/1", async (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json([{
"id": 1,
"name": "Rick Sanchez",
"status": "Alive"
}]);
)
})
]; In your register function you can do: export async function register(requestHandlerRegistry) {
if (process.env.USE_MSW) {
const requestHandlers = (await import("./mocks/handlers.js")).requestHandlers;
requestHandlerRegistry.add(requestHandlers);
}
} When the container application is bootstrapped, do the following:
if (process.env.USE_MSW) {
import("./mocks/browsers.js").then(({ startMsw }) => {
startMsw(requestHandlerRegistry.handlers);
});
} Sorry there are a few missing steps, but such a setup involves many parts. Our custom application shell code and documentation is open source if you are looking for additional information or inspiration: https://gsoft-inc.github.io/wl-squide. This section is showing how to do it with our custom application shell: https://gsoft-inc.github.io/wl-squide/getting-started/learn-the-api/#mock-service-worker I hope that will still work with MSW v2 🤞🏻 |
Beta Was this translation helpful? Give feedback.
-
Is there a technical constraint inherent to service workers that would preclude handlers from being registered after msw was started? The correct approach IMHO, would be for each lazy-loaded micro-frontend to register its handlers when it gets bootstrapped. Requiring that developers eagerly load and bootstrap handlers for ALL micro-frontends would add a tremendous amount of needless complexity and undermines the motivation for using microservices in the first place. |
Beta Was this translation helpful? Give feedback.
-
I have a small setup with webpack module federation, one container app and one remote. The remote is exposing itself completely and the container is consuming it to show it after clicking a navigation link like
/users
.The container is reachable at
localhost:8080
, the remote atlocalhost:3001
Now, the remote is using mockup service worker, which works fine when visiting the app directly. When called through the container, an error comes up:
Uncaught (in promise) Error: [MSW] Failed to register a Service Worker for scope ('http://localhost:8080/') with script ('http://localhost:8080/mockServiceWorker.js'): Service Worker script does not exist at the given path.
I also tried setting the scope with
which resolves in an error
Failed to register a ServiceWorker: The origin of the provided scope ('http://localhost:3001') does not match the current origin ('http://localhost:8080')
.Does anyone have experiences with this? Using service workers in remotes with module federation? How to call them in the container?
Beta Was this translation helpful? Give feedback.
All reactions