fix(middleware): avoid manual NextRequest with body for GET/HEAD to resolve failed to pipe response error (160k occurrences)
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 10s
Build & Deploy / 🧪 QA (push) Successful in 1m52s
Build & Deploy / 🏗️ Build (push) Failing after 3m37s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Post-Deploy Verification (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 2m43s
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 10s
Build & Deploy / 🧪 QA (push) Successful in 1m52s
Build & Deploy / 🏗️ Build (push) Failing after 3m37s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Post-Deploy Verification (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 2m43s
This commit is contained in:
@@ -52,16 +52,20 @@ export default async function middleware(request: NextRequest) {
|
||||
|
||||
urlObj.protocol = proto;
|
||||
|
||||
effectiveRequest = new NextRequest(urlObj, {
|
||||
headers: request.headers,
|
||||
method: request.method,
|
||||
body: request.body,
|
||||
});
|
||||
// Only create a new request for GET/HEAD to avoid consuming the body stream on POST/PUT
|
||||
// This resolves the "failed to pipe response" error (160k occurrences in GlitchTip)
|
||||
if (['GET', 'HEAD'].includes(request.method)) {
|
||||
effectiveRequest = new NextRequest(urlObj, {
|
||||
headers: request.headers,
|
||||
method: request.method,
|
||||
// No body for GET/HEAD
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== 'production' || !process.env.CI) {
|
||||
console.log(
|
||||
`🛡️ Proxy: Fixed internal URL leak: ${url} -> ${urlObj.toString()} | Proto: ${proto} | Host: ${hostHeader}`,
|
||||
);
|
||||
if (process.env.NODE_ENV !== 'production' || !process.env.CI) {
|
||||
console.log(
|
||||
`🛡️ Proxy: Fixed internal URL leak: ${url} -> ${urlObj.toString()} | Proto: ${proto} | Host: ${hostHeader}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user