Skip to content
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

Fix external rewrite with body #49487

Merged
merged 1 commit into from May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/next/src/server/next-server.ts
Expand Up @@ -860,7 +860,12 @@ export default class NextNodeServer extends BaseServer {
}
})
})
proxy.web(req.originalRequest, res.originalResponse)
proxy.web(req.originalRequest, res.originalResponse, {
buffer: getRequestMeta(
req,
'__NEXT_CLONABLE_BODY'
)?.cloneBodyStream(),
})
}
})

Expand Down
6 changes: 6 additions & 0 deletions test/e2e/middleware-rewrites/app/middleware.js
Expand Up @@ -19,6 +19,12 @@ export async function middleware(request) {
})
}

if (url.pathname.includes('/middleware-external-rewrite-body')) {
return NextResponse.rewrite(
'https://next-data-api-endpoint.vercel.app/api/echo-body'
)
}

if (url.pathname.includes('/rewrite-to-static')) {
request.nextUrl.pathname = '/static-ssg/post-1'
return NextResponse.rewrite(request.nextUrl)
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/middleware-rewrites/app/next.config.js
Expand Up @@ -24,6 +24,11 @@ module.exports = {
source: '/config-rewrite-to-dynamic-static/:rewriteSlug',
destination: '/ssg',
},
{
source: '/external-rewrite-body',
destination:
'https://next-data-api-endpoint.vercel.app/api/echo-body',
},
],
fallback: [],
}
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/middleware-rewrites/test/index.test.ts
Expand Up @@ -23,6 +23,28 @@ describe('Middleware Rewrite', () => {
})

function tests() {
it('should handle next.config.js rewrite with body correctly', async () => {
const body = JSON.stringify({ hello: 'world' })
const res = await next.fetch('/external-rewrite-body', {
redirect: 'manual',
method: 'POST',
body,
})
expect(res.status).toBe(200)
expect(await res.text()).toEqual(body)
})

it('should handle middleware rewrite with body correctly', async () => {
const body = JSON.stringify({ hello: 'world' })
const res = await next.fetch('/middleware-external-rewrite-body', {
redirect: 'manual',
method: 'POST',
body,
})
expect(res.status).toBe(200)
expect(await res.text()).toEqual(body)
})

it('should handle static dynamic rewrite from middleware correctly', async () => {
const browser = await webdriver(next.url, '/rewrite-to-static')

Expand Down