I'm trying to figure out how to implement a MicroFrontend Host application that would be able to bootstrap one or more React applications built with webpack. For that purpose I need to force the application to load its resources from some different URL - and not just using relative paths.
Webpack has a nice feature for setting the public path: https://webpack.js.org/guides/public-path/ However there are some limitations:
Is there any way to make such a public url dynamic for an application built with webpack? I can live with the fact that I will have to manually update all urls in index.html, but then all the other resources (images, etc) I'd like to be able to get from some other URL. This is in general similar to hosting all the resources generated by webpack on a CDN and having index.html still served from a web server with a different address.
I think this feature should be configured on your frontend server (nginx/apache/node), not webpack.
It looks like you just need a proxy configuration for it.
Example for webpack-dev-server
, you can try something like this on localhost
:
devServer: {
proxy: [{
{
context: '/my/backend',
target: `${PROTOCOL}://${HOST}:${PORT}`,
pathRewrite: {'^/app1/images': '/shared/images'}
},
{
context: '/my/backend',
target: `${PROTOCOL}://${HOST}:${PORT}`,
pathRewrite: {'^/app2/images': '/shared/images'}
},
{
context: '/my/backend',
target: `${PROTOCOL}://${HOST}:${PORT}`,
pathRewrite: {'^/app1/sounds': '/shared/sounds'}
},
{
context: '/my/backend',
target: `${PROTOCOL}://${HOST}:${PORT}`,
pathRewrite: {'^/app2/sounds': '/path/to/cdn'}
}]
}
It is pseudocode, do not try this at home, but you got the idea.
Code and rules depends on server you choose of course.
PS. But it is very interesting question, I will try to find some solution with webpack
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With