Module Federation: injecting scripts for remotes with HtmlWebpackPlugin
See original GitHub issueBug report
What is the current behavior?
During runtime when request to remote application is pending, injected script is blocking the page.
If the current behavior is a bug, please provide the steps to reproduce.
In the scenario we have host and remote applications:
- host application
shell
(localhost:3000
) - remote
app
(localhost:3002
) which is not responding, we simulaterequest pending
by setting the timeout
shell
webpack configuration:
new ModuleFederationPlugin({
name: "shell",
filename: "remoteEntry.js",
remotes: {
app: 'app@http://localhost:3002/remoteEntry.js',
}
})
new HtmlWebpackPlugin({
template: "./public/index.html",
inject: "body",
})
Source code of app
:
const express = require("express");
const app = express();
const port = 3002;
app.use((req, res, next) => setTimeout(next, 30000));
app.get("*", (req, res) => { res.send("Hello World!"); });
app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); });
As you can see script connecting for the remote entry is located in head
tag.
We can use promise based loaders for remotes, pass a promise to this remote and inject into body
tag:
market: `promise new Promise(resolve => {
const remoteUrl = 'http://localhost:3002/remoteEntry.js'
const script = document.createElement('script')
script.async = true;
script.defer = true;
script.src = remoteUrl
document.body.appendChild(script);
})
`
and at runtime script is located at the end body but the page is still blocked by loading remote entry.
Alternatively we can dynamically load remotes during runtime using this
What is the expected behavior?
- When
inject: body
is set withHtmlWebpackPlugin
put loading script at the end of the body tag. - Do not block page/application when remote entry is not responding.
Other relevant information: webpack version: 5.38.1 Node.js version: 14.17.1 Operating System: macOS/Ubuntu Additional tools: html-webpack-plugin: 5.3.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
@DevHunvee3 The issue still occurs for us and we are using workaround for non blocking render:
and using
dynamicImport
like:where
remoteService
is remote path from module federation configPlease open an issue in
HtmlWebpackPlugin
, it should be solved there