Unverified Commit d670198e authored by Joachim Viide's avatar Joachim Viide Committed by GitHub

Add "Vary: Accept" header to /_next/image responses (#26788)

This pull request adds "Vary: Accept" header to responses from the image optimizer (i.e. the /_next/image endpoint).

The image optimizer prefers re-encoding JPG files to WebP, but some browsers (such as Safari 14 on Catalina) do not yet support WebP. In such cases the optimizer uses the Accept header sent by the browser to send out a JPG response. Thus the optimizer's response may depend on the Accept header.

Potential caching proxies can be informed of this fact by adding "Vary: Accept" to the response headers. Otherwise WebP data may be served to browsers that do not support it, for example in the following scenario:
 * A browser that supports WebP requests the JPG. The optimizer re-encodes it to WebP. The proxy caches the WebP data.
 * After this another browser that doesn't support WebP requests the JPG. The proxy sends the WebP data to the browser.

- [x] Integration tests added
- [x] Make sure the linting passes
parent 93f62542
...@@ -419,6 +419,7 @@ function setResponseHeaders( ...@@ -419,6 +419,7 @@ function setResponseHeaders(
isStatic: boolean, isStatic: boolean,
isDev: boolean isDev: boolean
) { ) {
res.setHeader('Vary', 'Accept')
res.setHeader( res.setHeader(
'Cache-Control', 'Cache-Control',
isStatic isStatic
......
...@@ -58,6 +58,7 @@ function runTests({ w, isDev, domains }) { ...@@ -58,6 +58,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
expect(isAnimated(await res.buffer())).toBe(true) expect(isAnimated(await res.buffer())).toBe(true)
}) })
...@@ -70,6 +71,7 @@ function runTests({ w, isDev, domains }) { ...@@ -70,6 +71,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
expect(isAnimated(await res.buffer())).toBe(true) expect(isAnimated(await res.buffer())).toBe(true)
}) })
...@@ -82,6 +84,7 @@ function runTests({ w, isDev, domains }) { ...@@ -82,6 +84,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
expect(isAnimated(await res.buffer())).toBe(true) expect(isAnimated(await res.buffer())).toBe(true)
}) })
...@@ -95,6 +98,9 @@ function runTests({ w, isDev, domains }) { ...@@ -95,6 +98,9 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
// SVG is compressible so will have accept-encoding set from
// compression
expect(res.headers.get('Vary')).toMatch(/^Accept(,|$)/)
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
const actual = await res.text() const actual = await res.text()
const expected = await fs.readFile( const expected = await fs.readFile(
...@@ -113,6 +119,7 @@ function runTests({ w, isDev, domains }) { ...@@ -113,6 +119,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toMatch(/^Accept(,|$)/)
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
const actual = await res.text() const actual = await res.text()
const expected = await fs.readFile( const expected = await fs.readFile(
...@@ -133,6 +140,7 @@ function runTests({ w, isDev, domains }) { ...@@ -133,6 +140,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
}) })
...@@ -147,6 +155,7 @@ function runTests({ w, isDev, domains }) { ...@@ -147,6 +155,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
}) })
...@@ -244,6 +253,7 @@ function runTests({ w, isDev, domains }) { ...@@ -244,6 +253,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w) await expectWidth(res, w)
}) })
...@@ -257,6 +267,7 @@ function runTests({ w, isDev, domains }) { ...@@ -257,6 +267,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w) await expectWidth(res, w)
}) })
...@@ -270,6 +281,7 @@ function runTests({ w, isDev, domains }) { ...@@ -270,6 +281,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w) await expectWidth(res, w)
}) })
...@@ -283,6 +295,7 @@ function runTests({ w, isDev, domains }) { ...@@ -283,6 +295,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
// FIXME: await expectWidth(res, w) // FIXME: await expectWidth(res, w)
}) })
...@@ -296,6 +309,7 @@ function runTests({ w, isDev, domains }) { ...@@ -296,6 +309,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
// FIXME: await expectWidth(res, w) // FIXME: await expectWidth(res, w)
}) })
...@@ -311,6 +325,7 @@ function runTests({ w, isDev, domains }) { ...@@ -311,6 +325,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w) await expectWidth(res, w)
}) })
...@@ -326,6 +341,7 @@ function runTests({ w, isDev, domains }) { ...@@ -326,6 +341,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w) await expectWidth(res, w)
}) })
...@@ -346,6 +362,7 @@ function runTests({ w, isDev, domains }) { ...@@ -346,6 +362,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w) await expectWidth(res, w)
}) })
...@@ -448,6 +465,7 @@ function runTests({ w, isDev, domains }) { ...@@ -448,6 +465,7 @@ function runTests({ w, isDev, domains }) {
expect(res1.headers.get('Cache-Control')).toBe( expect(res1.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res1.headers.get('Vary')).toBe('Accept')
const etag = res1.headers.get('Etag') const etag = res1.headers.get('Etag')
expect(etag).toBeTruthy() expect(etag).toBeTruthy()
await expectWidth(res1, w) await expectWidth(res1, w)
...@@ -460,6 +478,7 @@ function runTests({ w, isDev, domains }) { ...@@ -460,6 +478,7 @@ function runTests({ w, isDev, domains }) {
expect(res2.headers.get('Cache-Control')).toBe( expect(res2.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res2.headers.get('Vary')).toBe('Accept')
expect((await res2.buffer()).length).toBe(0) expect((await res2.buffer()).length).toBe(0)
const query3 = { url: '/test.jpg', w, q: 25 } const query3 = { url: '/test.jpg', w, q: 25 }
...@@ -469,6 +488,7 @@ function runTests({ w, isDev, domains }) { ...@@ -469,6 +488,7 @@ function runTests({ w, isDev, domains }) {
expect(res3.headers.get('Cache-Control')).toBe( expect(res3.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res3.headers.get('Vary')).toBe('Accept')
expect(res3.headers.get('Etag')).toBeTruthy() expect(res3.headers.get('Etag')).toBeTruthy()
expect(res3.headers.get('Etag')).not.toBe(etag) expect(res3.headers.get('Etag')).not.toBe(etag)
await expectWidth(res3, w) await expectWidth(res3, w)
...@@ -486,6 +506,9 @@ function runTests({ w, isDev, domains }) { ...@@ -486,6 +506,9 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
// bmp is compressible so will have accept-encoding set from
// compression
expect(res.headers.get('Vary')).toMatch(/^Accept(,|$)/)
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
const json2 = await fsToJson(imagesDir) const json2 = await fsToJson(imagesDir)
...@@ -501,6 +524,7 @@ function runTests({ w, isDev, domains }) { ...@@ -501,6 +524,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, 400) await expectWidth(res, 400)
}) })
...@@ -516,6 +540,7 @@ function runTests({ w, isDev, domains }) { ...@@ -516,6 +540,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate` `public, max-age=${isDev ? 0 : 60}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
const png = await res.buffer() const png = await res.buffer()
...@@ -540,6 +565,7 @@ function runTests({ w, isDev, domains }) { ...@@ -540,6 +565,7 @@ function runTests({ w, isDev, domains }) {
expect(res1.headers.get('Cache-Control')).toBe( expect(res1.headers.get('Cache-Control')).toBe(
'public, max-age=315360000, immutable' 'public, max-age=315360000, immutable'
) )
expect(res1.headers.get('Vary')).toBe('Accept')
await expectWidth(res1, w) await expectWidth(res1, w)
// Ensure subsequent request also has immutable header // Ensure subsequent request also has immutable header
...@@ -548,6 +574,7 @@ function runTests({ w, isDev, domains }) { ...@@ -548,6 +574,7 @@ function runTests({ w, isDev, domains }) {
expect(res2.headers.get('Cache-Control')).toBe( expect(res2.headers.get('Cache-Control')).toBe(
'public, max-age=315360000, immutable' 'public, max-age=315360000, immutable'
) )
expect(res2.headers.get('Vary')).toBe('Accept')
await expectWidth(res2, w) await expectWidth(res2, w)
} }
}) })
...@@ -873,6 +900,7 @@ describe('Image Optimizer', () => { ...@@ -873,6 +900,7 @@ describe('Image Optimizer', () => {
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=31536000, must-revalidate` `public, max-age=31536000, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept')
await expectWidth(res, 64) await expectWidth(res, 64)
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment