Skip to content

Commit ad7cb3b

Browse files
SethFalcoijjk
andauthored
fix: don't override user-agent in fetch if specified (#35547)
* fix: don't override user-agent in fetch if specified * Add test for fetch user-agent in middleware Co-authored-by: JJ Kasper <jj@jjsweb.site>
1 parent abfbf41 commit ad7cb3b

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

packages/next/server/web/sandbox/context.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ async function createModuleContext(options: {
154154
const prevs = init.headers.get(`x-middleware-subrequest`)?.split(':') || []
155155
const value = prevs.concat(options.module).join(':')
156156
init.headers.set('x-middleware-subrequest', value)
157-
init.headers.set(`user-agent`, `Next.js Middleware`)
157+
158+
if (!init.headers.has('user-agent')) {
159+
init.headers.set(`user-agent`, `Next.js Middleware`)
160+
}
158161

159162
if (typeof input === 'object' && 'url' in input) {
160163
return fetch(input.url, {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function handler(req, res) {
2+
res.json({ url: req.url, headers: req.headers })
3+
}

test/integration/middleware/core/pages/interface/_middleware.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,52 @@ export async function middleware(request) {
3131
}
3232
}
3333

34+
if (url.pathname.includes('/fetchUserAgentDefault')) {
35+
try {
36+
const apiRoute = new URL(url)
37+
apiRoute.pathname = '/api/headers'
38+
const res = await fetch(apiRoute)
39+
return new Response(await res.text(), {
40+
status: 200,
41+
headers: {
42+
'content-type': 'application/json',
43+
},
44+
})
45+
} catch (err) {
46+
return new Response(JSON.stringify({ error: err.message }), {
47+
status: 500,
48+
headers: {
49+
'content-type': 'application/json',
50+
},
51+
})
52+
}
53+
}
54+
55+
if (url.pathname.includes('/fetchUserAgentCustom')) {
56+
try {
57+
const apiRoute = new URL(url)
58+
apiRoute.pathname = '/api/headers'
59+
const res = await fetch(apiRoute, {
60+
headers: {
61+
'user-agent': 'custom-agent',
62+
},
63+
})
64+
return new Response(await res.text(), {
65+
status: 200,
66+
headers: {
67+
'content-type': 'application/json',
68+
},
69+
})
70+
} catch (err) {
71+
return new Response(JSON.stringify({ error: err.message }), {
72+
status: 500,
73+
headers: {
74+
'content-type': 'application/json',
75+
},
76+
})
77+
}
78+
}
79+
3480
if (url.pathname.endsWith('/webcrypto')) {
3581
const response = {}
3682
try {

test/integration/middleware/core/test/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ describe('Middleware base tests', () => {
139139
})
140140

141141
function urlTests(_log, locale = '') {
142+
it('should set fetch user agent correctly', async () => {
143+
const res = await fetchViaHTTP(
144+
context.appPort,
145+
`${locale}/interface/fetchUserAgentDefault`
146+
)
147+
expect((await res.json()).headers['user-agent']).toBe('Next.js Middleware')
148+
149+
const res2 = await fetchViaHTTP(
150+
context.appPort,
151+
`${locale}/interface/fetchUserAgentCustom`
152+
)
153+
expect((await res2.json()).headers['user-agent']).toBe('custom-agent')
154+
})
155+
142156
it('rewrites by default to a target location', async () => {
143157
const res = await fetchViaHTTP(context.appPort, `${locale}/urls`)
144158
const html = await res.text()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy