-
Notifications
You must be signed in to change notification settings - Fork 107
fix: dev server 504 with chunk loding #1611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,10 +82,10 @@ exports.dev = async function (opts) { | |
| assert(opts, 'opts should be supplied'); | ||
| checkConfig(opts); | ||
| const express = require('express'); | ||
| const proxy = require('express-http-proxy'); | ||
| const app = express(); | ||
| const port = opts.port || 8000; | ||
| const hmrPort = opts.port + 1; | ||
| const makoConfig = await getMakoConfig(opts); | ||
|
|
||
| // cors | ||
| app.use( | ||
|
|
@@ -118,16 +118,36 @@ exports.dev = async function (opts) { | |
| createProxy(opts.config.proxy, app); | ||
| } | ||
|
|
||
| app.use( | ||
| proxy(`http://127.0.0.1:${hmrPort}`, { | ||
| filter: function (req, res) { | ||
| return req.method == 'GET' || req.method == 'HEAD'; | ||
| }, | ||
| skipToNextHandlerFilter: function (proxyRes) { | ||
| return proxyRes.statusCode !== 200; | ||
| }, | ||
| }), | ||
| ); | ||
| // serve dist files | ||
| app.use(express.static(makoConfig.output.path)); | ||
| if (process.env.SSU === 'true') { | ||
| // for ssu cache chunks | ||
|
|
||
| app.use(function (req, res, next) { | ||
| if (req.method !== 'GET' && req.method !== 'HEAD') { | ||
| return next(); | ||
| } | ||
|
|
||
| let proxy = createProxyMiddleware({ | ||
| target: `http://127.0.0.1:${hmrPort}`, | ||
| selfHandleResponse: true, | ||
| onProxyRes: (proxyRes, req, res) => { | ||
| if (proxyRes.statusCode !== 200) { | ||
| next(); | ||
| } else { | ||
| proxyRes.pipe(res); | ||
| } | ||
| }, | ||
| onError: (err, req, res) => { | ||
| next(); | ||
| }, | ||
|
Comment on lines
+141
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 错误处理时应传递错误对象给 在 |
||
| }); | ||
|
Comment on lines
+131
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 建议将 当前在每次请求时都会重新创建 |
||
|
|
||
| proxy(req, res, () => { | ||
| next(); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| // after middlewares | ||
| (opts.afterMiddlewares || []).forEach((m) => { | ||
|
|
@@ -169,7 +189,6 @@ exports.dev = async function (opts) { | |
|
|
||
| // mako dev | ||
| const { build } = require('@umijs/mako'); | ||
| const makoConfig = await getMakoConfig(opts); | ||
| if (process.env.HMR === 'none') { | ||
| makoConfig.hmr = false; | ||
| } else { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要在
onProxyRes中设置响应的状态码和头信息当使用
selfHandleResponse: true时,需要手动将proxyRes的状态码和头信息复制到res。当前只使用了proxyRes.pipe(res);,但未设置状态码和头信息,可能导致客户端无法正确解析响应。建议在管道之前,添加以下代码:+ res.writeHead(proxyRes.statusCode, proxyRes.headers); proxyRes.pipe(res);Committable suggestion