Skip to content

Commit 292f2fd

Browse files
authored
chore(modern-js-plugin): warn if header origin is not specified (#3654)
1 parent 21c2fb9 commit 292f2fd

File tree

7 files changed

+70
-10
lines changed

7 files changed

+70
-10
lines changed

.changeset/shy-ghosts-worry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/modern-js': patch
3+
---
4+
5+
chore(modern-js-plugin): warn if header origin is not specified

apps/modernjs/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"parallel": true,
6464
"commands": [
6565
{
66-
"command": "lsof -i :8080 || nx run modernjs:serve & echo 'done'",
66+
"command": "lsof -i :4001 || nx run modernjs:serve & echo 'done'",
6767
"forwardAllArgs": false
6868
},
6969
{

apps/website-new/docs/en/guide/troubleshooting/other.mdx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,14 @@ new ModuleFederationPlugin({
7979

8080
* If shared is provided from online host, use [Module Federation DevTools](../basic/chrome-devtool), and click `Enable HMR` button .
8181

82-
![](@public/guide/chrome-devtools/mf-devtool-hmr.jpg)
82+
![](@public/guide/chrome-devtools/mf-devtool-hmr.jpg)
83+
84+
## CORS warn
85+
86+
When `exposes` is set in the project, it will be regarded as a producer. To ensure that the producer resources can be loaded normally by consumers, `@module-federation/modern-js` and `@module-federation/rsbuild-plugin` will set `Access-Control-Allow-Origin` to `*` and issue a warning at the same time.
87+
88+
#### Solutions
89+
90+
* [Modern.js]: Set [devServer.headers](https://modernjs.dev/configure/app/tools/dev-server.html#headers) value to the specified domain whitelist instead of `*`
91+
92+
* [Rsbuild]: Set [server.cors.origin](https://rsbuild.dev/config/server/cors#origin) value to the specified domain whitelist instead of `*`

apps/website-new/docs/zh/guide/troubleshooting/other.mdx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,14 @@ Uncaught TypeError: Cannot read properties on null (reading `useState`)
5353

5454
* 若 shared 提供方是线上的 react ,那么需要使用 [Module Federation DevTools](../basic/chrome-devtool),并点击 `Enable HMR` 按钮
5555

56-
![](@public/guide/chrome-devtools/mf-devtool-hmr.jpg)
56+
![](@public/guide/chrome-devtools/mf-devtool-hmr.jpg)
57+
58+
## CORS 警告
59+
60+
当项目中设置了 `exposes` ,会被视为生产者,为保证生产者资源能正常被消费者加载,`@module-federation/modern-js``@module-federation/rsbuild-plugin` 会设置 `Access-Control-Allow-Origin``*` ,并同时发出警告。
61+
62+
#### 解决方案
63+
64+
* [Modern.js]: 设置 [devServer.headers](https://modernjs.dev/configure/app/tools/dev-server.html#headers) 值为指定的域名白名单而非 `*`
65+
66+
* [Rsbuild]: 设置 [server.cors.origin](https://rsbuild.dev/config/server/cors#origin) 值为指定的域名白名单而非 `*`

packages/modernjs/src/cli/configPlugin.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { moduleFederationPlugin, encodeName } from '@module-federation/sdk';
44
import { bundle } from '@modern-js/node-bundle-require';
55
import { PluginOptions } from '../types';
66
import { LOCALHOST, PLUGIN_IDENTIFIER } from '../constant';
7-
import logger from './logger';
87
import { autoDeleteSplitChunkCacheGroups } from '@module-federation/rsbuild-plugin/utils';
8+
import logger from './logger';
99

1010
import type { InternalModernPluginOptions } from '../types';
1111
import type {
@@ -420,15 +420,41 @@ export const moduleFederationConfigPlugin = (
420420
}
421421
}
422422

423+
const devServerConfig = modernjsConfig.tools?.devServer;
424+
const corsWarnMsgs = [
425+
'View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details.',
426+
];
427+
if (
428+
typeof devServerConfig !== 'object' ||
429+
!('headers' in devServerConfig)
430+
) {
431+
corsWarnMsgs.unshift(
432+
'Detect devServer.headers is empty, mf modern plugin will add default cors header: devServer.headers["Access-Control-Allow-Headers"] = "*". It is recommended to specify an allowlist of trusted origins instead.',
433+
);
434+
}
435+
436+
const exposes = userConfig.csrConfig?.exposes;
437+
const hasExposes =
438+
exposes && Array.isArray(exposes)
439+
? exposes.length
440+
: Object.keys(exposes ?? {}).length;
441+
442+
if (corsWarnMsgs.length > 1 && hasExposes) {
443+
logger.warn(corsWarnMsgs.join('\n'));
444+
}
445+
446+
const corsHeaders = hasExposes
447+
? {
448+
'Access-Control-Allow-Origin': '*',
449+
'Access-Control-Allow-Methods':
450+
'GET, POST, PUT, DELETE, PATCH, OPTIONS',
451+
'Access-Control-Allow-Headers': '*',
452+
}
453+
: undefined;
423454
return {
424455
tools: {
425456
devServer: {
426-
headers: {
427-
'Access-Control-Allow-Origin': '*',
428-
'Access-Control-Allow-Methods':
429-
'GET, POST, PUT, DELETE, PATCH, OPTIONS',
430-
'Access-Control-Allow-Headers': '*',
431-
},
457+
headers: corsHeaders,
432458
},
433459
},
434460
source: {

packages/rsbuild-plugin/src/cli/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ export const pluginModuleFederation = (
182182
// Allow remote modules to be loaded by setting CORS headers
183183
// This is required for MF to work properly across different origins
184184
config.server.headers ||= {};
185+
if (!config.server.headers['Access-Control-Allow-Origin']) {
186+
const corsWarnMsgs = [
187+
'Detect devServer.headers is empty, mf modern plugin will add default cors header: devServer.headers["Access-Control-Allow-Headers"] = "*". It is recommended to specify an allowlist of trusted origins instead.',
188+
'View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details.',
189+
];
190+
191+
logger.warn(corsWarnMsgs.join('\n'));
192+
}
185193
config.server.headers['Access-Control-Allow-Origin'] ||= '*';
186194

187195
// For remote modules, Rsbuild should send the ws request to the provider's dev server.

packages/runtime-core/src/plugins/snapshot/SnapshotHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ export class SnapshotHandler {
326326
{
327327
manifestUrl,
328328
moduleName: moduleInfo.name,
329+
hostName: this.HostInstance.options.name,
329330
},
330331
`${err}`,
331332
),

0 commit comments

Comments
 (0)