Skip to content

Commit 044e471

Browse files
committed
Fix image optimization encoding url
1 parent 27ccd3c commit 044e471

File tree

7 files changed

+115
-11
lines changed

7 files changed

+115
-11
lines changed

packages/next/server/image-optimizer.ts

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

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

76-
if (!decodedUrl) {
76+
if (!url) {
7777
res.statusCode = 400
7878
res.end('"url" parameter is required')
7979
return { finished: true }
80-
} else if (Array.isArray(decodedUrl)) {
80+
} else if (Array.isArray(url)) {
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-
8886
let isAbsolute: boolean
8987

9088
if (url.startsWith('/')) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
images: {
3+
domains: ['image-optimization-test.vercel.app'],
4+
},
5+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react'
2+
import Image from 'next/image'
3+
import img from '../public/äöü.png'
4+
5+
const Page = () => {
6+
return (
7+
<div>
8+
<h1>Unicode Image URL</h1>
9+
<Image id="static" src={img} />
10+
<Image id="internal" src="/äöü.png" width={400} height={400} />
11+
<Image
12+
id="external"
13+
src="https://image-optimization-test.vercel.app/äöü.png"
14+
width={400}
15+
height={400}
16+
/>
17+
</div>
18+
)
19+
}
20+
21+
export default Page
1.51 KB
Loading
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* eslint-env jest */
2+
3+
import {
4+
findPort,
5+
killApp,
6+
launchApp,
7+
nextBuild,
8+
nextStart,
9+
} from 'next-test-utils'
10+
import webdriver from 'next-webdriver'
11+
//import fetch from 'node-fetch'
12+
import { join } from 'path'
13+
14+
jest.setTimeout(1000 * 60)
15+
16+
const appDir = join(__dirname, '../')
17+
18+
let appPort
19+
let app
20+
let browser
21+
22+
function runTests() {
23+
it('should load static unicode image', async () => {
24+
const src = await browser.elementById('static').getAttribute('src')
25+
expect(src).toMatch(
26+
/_next%2Fstatic%2Fimage%2Fpublic%2F%C3%A4%C3%B6%C3%BC(.+)png/
27+
)
28+
const res = await fetch(src)
29+
expect(res.status).toBe(200)
30+
})
31+
32+
it('should load internal unicode image', async () => {
33+
const src = await browser.elementById('internal').getAttribute('src')
34+
expect(src).toMatch('/_next/image?url=%2F%C3%A4%C3%B6%C3%BC.png')
35+
const res = await fetch(src)
36+
expect(res.status).toBe(200)
37+
})
38+
39+
it('should load external unicode image', async () => {
40+
const src = await browser.elementById('external').getAttribute('src')
41+
expect(src).toMatch(
42+
'/_next/image?url=https%3A%2F%2Fimage-optimization-test.vercel.app%2F%C3%A4%C3%B6%C3%BC.png'
43+
)
44+
const res = await fetch(src)
45+
expect(res.status).toBe(200)
46+
})
47+
}
48+
49+
describe('Image Component Unicode Image URL', () => {
50+
describe('dev mode', () => {
51+
beforeAll(async () => {
52+
appPort = await findPort()
53+
app = await launchApp(appDir, appPort)
54+
browser = await webdriver(appPort, '/')
55+
})
56+
afterAll(() => {
57+
killApp(app)
58+
if (browser) {
59+
browser.close()
60+
}
61+
})
62+
runTests()
63+
})
64+
65+
describe('server mode', () => {
66+
beforeAll(async () => {
67+
await nextBuild(appDir)
68+
appPort = await findPort()
69+
app = await nextStart(appDir, appPort)
70+
browser = await webdriver(appPort, '/')
71+
})
72+
afterAll(() => {
73+
killApp(app)
74+
if (browser) {
75+
browser.close()
76+
}
77+
})
78+
runTests()
79+
})
80+
})

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ function Home() {
66
<>
77
<h1>Image Optimizer Home</h1>
88
<Image src={Logo} />
9+
<Image src="/äöü.png" width={200} height={200} />
10+
<Image
11+
src="https://image-optimization-test.vercel.app/äöü.png"
12+
width={200}
13+
height={200}
14+
/>
915
</>
1016
)
1117
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ 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-
6458
it('should maintain animated gif', async () => {
6559
const query = { w, q: 90, url: '/animated.gif' }
6660
const res = await fetchViaHTTP(appPort, '/_next/image', query, {})

0 commit comments

Comments
 (0)