32
loading...
This website collects cookies to deliver better user experience
CloudFront
, Lambda@Edge
and S3
.microfrontends
.publicPath
, and now when we load the remote application in our local host application all the assets are relative to the wrong domain (the local one, that belongs to the host) causing all the assets to be fetched from the wrong place.app1
and it's deployed to QA environment.publicPath
that is set on the remote is causing trouble when it's being loaded on a local host - being relative to the wrong domain and failing to fetch the correct files.publicPath
of a remote during runtime so that it includes the domain, and the subpath that the application is being served on.publicPath
works just fine in all the other scenarios. (this is our insurance that we don't modify the publicPath
when we don't need to)publicPath
with the full domain and subpath, sounds better.// DynamicPublicPathForRemoteInLocalHost.js
module.exports = class DynamicPublicPathForRemoteInLocalHost {
constructor(props) {
if (props.pathPrefix) {
this.pathPrefix = props.pathPrefix;
}
}
apply(compiler) {
compiler.hooks.make.tap('MutateRuntime', (compilation) => {
compilation.hooks.runtimeModule.tap('MutateRuntime', (module) => {
const isPublicPathRuntimeModule =
module.constructor.name === 'PublicPathRuntimeModule';
if (!isPublicPathRuntimeModule) {
return;
}
const [key] = module.getGeneratedCode().split('=');
// This function mutates publicPath in runtime when remote is used in local env.
// Without this plugin, when running host in local and importing remotes from QA, remote files are mistakenly being fetched from local instead of QA.
module._cachedGeneratedCode = `${key}=(() => {
if (window.location.host.startsWith('local') && !document.currentScript.src.includes('local')) {
return new URL(document.currentScript.src).origin + '${this.pathPrefix}';
} else {
return '${module.publicPath}';
}
})();`;
return module;
});
});
}
};
module.exports = {
...
output: {
publicPath:
process.env.NODE_ENV === 'local'
? 'https://localhost:3001/'
: '/app1/',
},
plugins: [
...
new DynamicPublicPathForRemoteInLocalHost({
pathPrefix: '/app1/',
}),
],
};
publicPath
.publicPath
you set in your webpack config if it doesn't detect the local scenario, which is a local host loading a non-local remote.