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(middleware): fetch resource may be a URL instance (or any stringifiable value) #31260

Merged
merged 3 commits into from
Nov 12, 2021
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
2 changes: 1 addition & 1 deletion packages/next/server/web/sandbox/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function getFetchHeaders(middleware: string, init: RequestInit) {
}

function getFetchURL(input: RequestInfo, headers: NodeHeaders = {}): string {
const initurl = isRequestLike(input) ? input.url : input
const initurl = isRequestLike(input) ? input.url : String(input)
if (initurl.startsWith('/')) {
const host = headers.host?.toString()
const localhost =
Expand Down
18 changes: 18 additions & 0 deletions test/integration/middleware/core/pages/interface/_middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ export async function middleware(request) {
})
}

if (url.pathname.endsWith('/fetchURL')) {
const response = {}
try {
await fetch(new URL('http://localhost'))
} catch (err) {
response.error = {
name: err.name,
message: err.message,
}
} finally {
return new NextResponse(JSON.stringify(response), {
headers: {
'content-type': 'application/json; charset=utf-8',
},
})
}
}

if (url.pathname.endsWith('/webcrypto')) {
const response = {}
try {
Expand Down
7 changes: 7 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ function interfaceTests(locale = '') {
expect('error' in response).toBe(false)
})

it(`${locale} fetch accepts a URL instance`, async () => {
const res = await fetchViaHTTP(context.appPort, '/interface/fetchURL')
const response = await res.json()
expect('error' in response).toBe(true)
expect(response.error.name).not.toBe('TypeError')
})

it(`${locale} should validate request url parameters from a static route`, async () => {
const res = await fetchViaHTTP(
context.appPort,
Expand Down