Skip to content

Commit a892d74

Browse files
authored
feat: support env vars to add cors when use serve command (#4005)
1 parent fa2c939 commit a892d74

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

.changeset/poor-penguins-build.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+
feat: support env vars to add cors when use serve command

packages/modernjs/src/server/index.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { ServerPlugin } from '@modern-js/server-runtime';
2-
import { createStaticMiddleware } from './staticMiddleware';
2+
import {
3+
createCorsMiddleware,
4+
createStaticMiddleware,
5+
} from './staticMiddleware';
36

47
const staticServePlugin = (): ServerPlugin => ({
58
name: '@modern-js/module-federation/server',
69
setup: (api) => {
710
api.onPrepare(() => {
811
// In development, we don't need to serve the manifest file, bundler dev server will handle it
9-
console.log(process.env.NODE_ENV);
1012
if (process.env.NODE_ENV === 'development') {
1113
return;
1214
}
@@ -15,21 +17,30 @@ const staticServePlugin = (): ServerPlugin => ({
1517
const config = api.getServerConfig();
1618

1719
const assetPrefix = config.output?.assetPrefix || '';
18-
if (!config.server?.ssr) {
19-
return;
20+
// When SSR is enabled, we need to serve the files in `bundle/` directory externally
21+
// Modern.js will only serve the files in `static/` directory
22+
if (config.server?.ssr) {
23+
const context = api.getServerContext();
24+
const pwd = context.distDirectory!;
25+
const serverStaticMiddleware = createStaticMiddleware({
26+
assetPrefix,
27+
pwd,
28+
});
29+
middlewares.push({
30+
name: 'module-federation-serve-manifest',
31+
handler: serverStaticMiddleware,
32+
});
2033
}
2134

22-
const context = api.getServerContext();
23-
const pwd = context.distDirectory!;
24-
25-
const serverStaticMiddleware = createStaticMiddleware({
26-
assetPrefix,
27-
pwd,
28-
});
29-
middlewares.push({
30-
name: 'module-federation-serve-manifest',
31-
handler: serverStaticMiddleware,
32-
});
35+
// When the MODERN_MF_AUTO_CORS environment variable is set, the server will add CORS headers to the response
36+
// This environment variable should only be set when running `serve` command in local test.
37+
if (process.env.MODERN_MF_AUTO_CORS) {
38+
const corsMiddleware = createCorsMiddleware();
39+
middlewares.push({
40+
name: 'module-federation-cors',
41+
handler: corsMiddleware,
42+
});
43+
}
3344
});
3445
},
3546
});

packages/modernjs/src/server/staticMiddleware.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,20 @@ const createStaticMiddleware = (options: {
5959
};
6060
};
6161

62-
export { createStaticMiddleware };
62+
const createCorsMiddleware = (): MiddlewareHandler => {
63+
return async (c, next) => {
64+
const pathname = c.req.path;
65+
// If the request is only for a static file
66+
if (path.extname(pathname)) {
67+
c.header('Access-Control-Allow-Origin', '*');
68+
c.header(
69+
'Access-Control-Allow-Methods',
70+
'GET, POST, PUT, DELETE, PATCH, OPTIONS',
71+
);
72+
c.header('Access-Control-Allow-Headers', '*');
73+
}
74+
return next();
75+
};
76+
};
77+
78+
export { createStaticMiddleware, createCorsMiddleware };

0 commit comments

Comments
 (0)