@lark-apaas/fullstack-rspack-preset 1.0.25 → 1.0.26-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/preset.js +29 -0
- package/package.json +1 -1
package/lib/preset.js
CHANGED
|
@@ -23,6 +23,26 @@ function sendBackendUnavailable502(_err, _req, res) {
|
|
|
23
23
|
});
|
|
24
24
|
res.end(JSON.stringify({ error: 'Bad Gateway', message: 'Backend service is not running' }));
|
|
25
25
|
}
|
|
26
|
+
function forwardXForwardedHost(proxyReq, req) {
|
|
27
|
+
const xForwardedHostRaw = req.headers['x-forwarded-host'];
|
|
28
|
+
const hostRaw = req.headers.host;
|
|
29
|
+
const normalizeHeader = (raw) => {
|
|
30
|
+
if (!raw)
|
|
31
|
+
return undefined;
|
|
32
|
+
const combined = Array.isArray(raw) ? raw.join(',') : raw;
|
|
33
|
+
const first = combined.split(',')[0]?.trim();
|
|
34
|
+
return first || undefined;
|
|
35
|
+
};
|
|
36
|
+
const xForwardedHost = normalizeHeader(xForwardedHostRaw);
|
|
37
|
+
const host = normalizeHeader(hostRaw);
|
|
38
|
+
const forwardedHost = xForwardedHost || host;
|
|
39
|
+
if (!forwardedHost)
|
|
40
|
+
return;
|
|
41
|
+
proxyReq.setHeader('X-Forwarded-Host', forwardedHost);
|
|
42
|
+
if (xForwardedHost) {
|
|
43
|
+
proxyReq.setHeader('Host', xForwardedHost);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
26
46
|
function createRecommendRspackConfig(options) {
|
|
27
47
|
const { isDev = true, enableReactRefresh = isDev, needRoutes = true, clientBasePath = '', publicPath = '', // 静态资源路径
|
|
28
48
|
} = options;
|
|
@@ -293,12 +313,18 @@ function createRecommendRspackConfig(options) {
|
|
|
293
313
|
target: `http://localhost:${serverPort}`,
|
|
294
314
|
changeOrigin: true,
|
|
295
315
|
onError: sendBackendUnavailable502,
|
|
316
|
+
onProxyReq: (proxyReq, req) => {
|
|
317
|
+
forwardXForwardedHost(proxyReq, req);
|
|
318
|
+
},
|
|
296
319
|
},
|
|
297
320
|
{
|
|
298
321
|
context: [`${clientBasePath}/__innerapi__`],
|
|
299
322
|
target: `http://localhost:${serverPort}`,
|
|
300
323
|
changeOrigin: true,
|
|
301
324
|
onError: sendBackendUnavailable502,
|
|
325
|
+
onProxyReq: (proxyReq, req) => {
|
|
326
|
+
forwardXForwardedHost(proxyReq, req);
|
|
327
|
+
},
|
|
302
328
|
},
|
|
303
329
|
{
|
|
304
330
|
context: (pathname, req) => {
|
|
@@ -311,6 +337,9 @@ function createRecommendRspackConfig(options) {
|
|
|
311
337
|
target: `http://localhost:${serverPort}`,
|
|
312
338
|
changeOrigin: true,
|
|
313
339
|
logLevel: 'debug',
|
|
340
|
+
onProxyReq: (proxyReq, req) => {
|
|
341
|
+
forwardXForwardedHost(proxyReq, req);
|
|
342
|
+
},
|
|
314
343
|
onError: (err, req, res) => (0, devtool_kits_1.handleDevProxyError)(err, req, res, {
|
|
315
344
|
logDir: process.env.LOG_DIR || './logs',
|
|
316
345
|
logFileName: 'server.std.log',
|