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: don't override user-agent in fetch if specified #35547

Merged
merged 5 commits into from Mar 28, 2022
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
5 changes: 4 additions & 1 deletion packages/next/server/web/sandbox/context.ts
Expand Up @@ -154,7 +154,10 @@ async function createModuleContext(options: {
const prevs = init.headers.get(`x-middleware-subrequest`)?.split(':') || []
const value = prevs.concat(options.module).join(':')
init.headers.set('x-middleware-subrequest', value)
init.headers.set(`user-agent`, `Next.js Middleware`)

if (!init.headers.has('user-agent')) {
init.headers.set(`user-agent`, `Next.js Middleware`)
}

if (typeof input === 'object' && 'url' in input) {
return fetch(input.url, {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/middleware/core/pages/api/headers.js
@@ -0,0 +1,3 @@
export default function handler(req, res) {
res.json({ url: req.url, headers: req.headers })
}
46 changes: 46 additions & 0 deletions test/integration/middleware/core/pages/interface/_middleware.js
Expand Up @@ -31,6 +31,52 @@ export async function middleware(request) {
}
}

if (url.pathname.includes('/fetchUserAgentDefault')) {
try {
const apiRoute = new URL(url)
apiRoute.pathname = '/api/headers'
const res = await fetch(apiRoute)
return new Response(await res.text(), {
status: 200,
headers: {
'content-type': 'application/json',
},
})
} catch (err) {
return new Response(JSON.stringify({ error: err.message }), {
status: 500,
headers: {
'content-type': 'application/json',
},
})
}
}

if (url.pathname.includes('/fetchUserAgentCustom')) {
try {
const apiRoute = new URL(url)
apiRoute.pathname = '/api/headers'
const res = await fetch(apiRoute, {
headers: {
'user-agent': 'custom-agent',
},
})
return new Response(await res.text(), {
status: 200,
headers: {
'content-type': 'application/json',
},
})
} catch (err) {
return new Response(JSON.stringify({ error: err.message }), {
status: 500,
headers: {
'content-type': 'application/json',
},
})
}
}

if (url.pathname.endsWith('/webcrypto')) {
const response = {}
try {
Expand Down
14 changes: 14 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Expand Up @@ -139,6 +139,20 @@ describe('Middleware base tests', () => {
})

function urlTests(_log, locale = '') {
it('should set fetch user agent correctly', async () => {
const res = await fetchViaHTTP(
context.appPort,
`${locale}/interface/fetchUserAgentDefault`
)
expect((await res.json()).headers['user-agent']).toBe('Next.js Middleware')

const res2 = await fetchViaHTTP(
context.appPort,
`${locale}/interface/fetchUserAgentCustom`
)
expect((await res2.json()).headers['user-agent']).toBe('custom-agent')
})

it('rewrites by default to a target location', async () => {
const res = await fetchViaHTTP(context.appPort, `${locale}/urls`)
const html = await res.text()
Expand Down