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 abort condition for requests #64000

Merged
merged 1 commit into from
Apr 2, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export class NextRequestAdapter {
}

return new NextRequest(url, {
body,
method: request.method,
headers: fromNodeOutgoingHttpHeaders(request.headers),
// @ts-expect-error - see https://github.com/whatwg/fetch/pull/1457
Expand All @@ -105,6 +104,14 @@ export class NextRequestAdapter {
// geo
// ip
// nextConfig

// body can not be passed if request was aborted
// or we get a Request body was disturbed error
...(signal.aborted
? {}
: {
body,
}),
})
}

Expand All @@ -116,7 +123,6 @@ export class NextRequestAdapter {
}

return new NextRequest(request.url, {
body,
method: request.method,
headers: fromNodeOutgoingHttpHeaders(request.headers),
// @ts-expect-error - see https://github.com/whatwg/fetch/pull/1457
Expand All @@ -125,6 +131,14 @@ export class NextRequestAdapter {
// geo
// ip
// nextConfig

// body can not be passed if request was aborted
// or we get a Request body was disturbed error
...(request.request.signal.aborted
? {}
: {
body,
}),
})
}
}
37 changes: 36 additions & 1 deletion test/e2e/app-dir/app-routes/app-custom-routes.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
import { check, waitFor } from 'next-test-utils'
import { Readable } from 'stream'

import {
Expand Down Expand Up @@ -129,6 +129,41 @@ createNextDescribe(
}
)

describe.each(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'])(
'abort via a %s request',
(method) => {
it.each(['/basic/endpoint', '/basic/vercel/endpoint'])(
'aborts without error on %s',
async (path) => {
const outputIdx = next.cliOutput.length
const controller = new AbortController()

const resProm = next
.fetch(basePath + path, {
method,
signal: controller.signal,
})
.catch((err) => err)

setTimeout(() => {
controller.abort()
}, 10)

await resProm

for (let i = 0; i < 3; i++) {
await waitFor(1000)
const trimmedOutput = next.cliOutput.substring(outputIdx)
expect(trimmedOutput).not.toContain('Error')
expect(trimmedOutput).not.toContain(
'should not be disturbed or locked'
)
}
}
)
}
)

describe('route groups', () => {
it('routes to the correct handler', async () => {
const res = await next.fetch(basePath + '/basic/endpoint/nested')
Expand Down