Skip to content

Commit 63aeddb

Browse files
authored
URI-encode url parameter in image optimizer (#27671)
fixes #27210 maybe related: #19668 currently, the image optimizer returns 400 when an image url contains non-ascii characters. this pr uri-encodes the `url` query parameter to fix it. also see #27210 (comment) ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes
1 parent 74503f1 commit 63aeddb

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

packages/next/server/image-optimizer.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,22 @@ export async function imageOptimizer(
6969
}
7070

7171
const { headers } = req
72-
const { url, w, q } = parsedUrl.query
72+
const { url: decodedUrl, w, q } = parsedUrl.query
7373
const mimeType = getSupportedMimeType(MODERN_TYPES, headers.accept)
7474
let href: string
7575

76-
if (!url) {
76+
if (!decodedUrl) {
7777
res.statusCode = 400
7878
res.end('"url" parameter is required')
7979
return { finished: true }
80-
} else if (Array.isArray(url)) {
80+
} else if (Array.isArray(decodedUrl)) {
8181
res.statusCode = 400
8282
res.end('"url" parameter cannot be an array')
8383
return { finished: true }
8484
}
8585

86+
const url = encodeURI(decodedUrl)
87+
8688
let isAbsolute: boolean
8789

8890
if (url.startsWith('/')) {
1.51 KB
Loading

test/integration/image-optimizer/test/index.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ function runTests({ w, isDev, domains = [], ttl, isSharp }) {
5555
expect(await res.text()).toMatch(/Image Optimizer Home/m)
5656
})
5757

58+
it('should handle non-ascii characters in image url', async () => {
59+
const query = { w, q: 90, url: '/äöü.png' }
60+
const res = await fetchViaHTTP(appPort, '/_next/image', query, {})
61+
expect(res.status).toBe(200)
62+
})
63+
5864
it('should maintain animated gif', async () => {
5965
const query = { w, q: 90, url: '/animated.gif' }
6066
const res = await fetchViaHTTP(appPort, '/_next/image', query, {})

0 commit comments

Comments
 (0)