Open JamesMHenderson opened 1 week ago
try adding hostname as well in base url
vite.config set server.origin
@JamesMHenderson have you tested these solutions?
@gioboa I've tried both suggestions and I get the same issue
In addition to having hostname in base config in remote, are you using type:module
in host config for this remote?
for e.g.
remotes: {
current:{
name: 'current',
entry: 'http://localhost:5174/remoteEntry.js',
type:"module"
}
}
I had this problem earlier in my config, i don't know if the solution is good but is working. I just add the complete path that i want in the base like "http://localhost:3001/conductor/remotes/current/". This create some duplication when i run preview in localhost but it's working normal when i get modules in the host app.
In addition to having hostname in base config in remote, are you using
type:module
in host config for this remote? for e.g.remotes: { current:{ name: 'current', entry: 'http://localhost:5174/remoteEntry.js', type:"module" } }
Yes I am
I had this problem earlier in my config, i don't know if the solution is good but is working. I just add the complete path that i want in the base like "http://localhost:3001/conductor/remotes/current/". This create some duplication when i run preview in localhost but it's working normal when i get modules in the host app.
This was the suggestion from @rubiks-cube which I tried and didn't resolve the problem
I also encountered the same problem. I tried adding hostname as well in base url and set server.origin, but it didn't seem to work. Here is my viteconfig file, oh btw it's a .mts
file
It's a similar issue for me, I resolved it with the following patch but I don't know if it covers all edge cases.
This patch assumes that base
is an absolute prefix, eg /_layout
.
diff --git a/node_modules/@module-federation/vite/lib/index.cjs b/node_modules/@module-federation/vite/lib/index.cjs
index 3f608c4..72ca20a 100644
--- a/node_modules/@module-federation/vite/lib/index.cjs
+++ b/node_modules/@module-federation/vite/lib/index.cjs
@@ -37,6 +37,7 @@ var addEntry = function addEntry(_ref) {
var devEntryPath = entryPath.startsWith('virtual:mf') ? '/@id/' + entryPath : entryPath;
var entryFiles = [];
var htmlFilePath;
+ var base;
var _command;
return [{
name: 'add-entry',
@@ -44,6 +45,8 @@ var addEntry = function addEntry(_ref) {
config: function config(_config, _ref2) {
var command = _ref2.command;
_command = command;
+ base = _config.base.endsWith('/') ? _config.base.slice(0, -1) : _config.base;
+ devEntryPath = base + devEntryPath;
},
configureServer: function configureServer(server) {
var _server$httpServer;
@@ -56,7 +59,8 @@ var addEntry = function addEntry(_ref) {
next();
return;
}
- if (req.url && req.url.startsWith(fileName.replace(/^\/?/, '/'))) {
+ if (req.url && req.url.startsWith((base + '/' + fileName).replace(/^\/?/, '/'))) {
+
req.url = devEntryPath;
}
next();
@@ -671,7 +675,8 @@ var Manifest = function Manifest() {
next();
return;
}
- if (req.url === mfManifestName.replace(/^\/?/, '/')) {
+
+ if (req.url === `${server.config.base}${mfManifestName}`.replace(/^\/?/, '/')) {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.end(JSON.stringify(_extends({}, generateMFManifest({}), {
@mzglinski Can you drop a PR with this fix pls?
@mzglinski Please provide a reproduction demo first. If server.origin
doesn’t meet the requirements, feel free to submit a PR.
The base
for a remote cannot be set to an absolute path.
@gioboa I'm not confident that this is a proper fix and won't break other use cases. As I mentioned, it makes assumptions regarding base
configuration, eg you cannot use a domain in it.
@zhangHongEn is it a specification requirement? The local setup that I'm using utilizes docker and proxy allowing the services to serve under the same domain. For example
https://project.localhost/_layout <- this is a remote
https://project.localhost <- first host
https://project.localhost/service-b <- second host
Without the patch, it works with production builds if I set
getPublicPath: `function() {return window.location.origin + '/_layout/'}`
in the configuration of the remote.
The dev mode was not working, with the patch it does.
I've just rechecked different options passed to server.origin
and couldn't get it to work.
I will prepare a reproduction repo later.
@mzglinski Understood. If your host and remote are on the same domain during the dev mode, it can indeed support setting the base to an absolute path.
@mzglinski Can you drop a PR with this fix pls?
@mzglinski I previously thought this was an unnecessary feature, but as @gioboa mentioned, you are welcome to fix this issue.
We will test the PR before release the fix, don't worry about that @mzglinski
If your vite config contains a url in the base config the application fails to load, as modules are attempted to be loaded from a path not including the base url
Example config
when you start the dev server up using the command
vite --host
the app fails to load as it fails to load virtual modulesIf you browse to one of the files it fails to load you get the error
This module is exposed from http://localhost:3001/conductor/remotes/current/@id/virtual:mf-exposes as expected