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

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

Merged
merged 4 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ function setResponseHeaders(
isStatic: boolean,
isDev: boolean
) {
res.setHeader('Vary', 'Accept')
res.setHeader(
'Cache-Control',
isStatic
Expand Down
24 changes: 24 additions & 0 deletions test/integration/image-optimizer/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
expect(isAnimated(await res.buffer())).toBe(true)
})
Expand All @@ -70,6 +71,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
expect(isAnimated(await res.buffer())).toBe(true)
})
Expand All @@ -82,6 +84,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
expect(isAnimated(await res.buffer())).toBe(true)
})
Expand All @@ -95,6 +98,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toMatch(/^Accept(,|$)/)
expect(res.headers.get('etag')).toBeTruthy()
const actual = await res.text()
const expected = await fs.readFile(
Expand All @@ -113,6 +117,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toMatch(/^Accept(,|$)/)
expect(res.headers.get('etag')).toBeTruthy()
const actual = await res.text()
const expected = await fs.readFile(
Expand All @@ -133,6 +138,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
})

Expand All @@ -147,6 +153,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
})

Expand Down Expand Up @@ -244,6 +251,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w)
})
Expand All @@ -257,6 +265,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w)
})
Expand All @@ -270,6 +279,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w)
})
Expand All @@ -283,6 +293,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
// FIXME: await expectWidth(res, w)
})
Expand All @@ -296,6 +307,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
// FIXME: await expectWidth(res, w)
})
Expand All @@ -311,6 +323,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w)
})
Expand All @@ -326,6 +339,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w)
})
Expand All @@ -346,6 +360,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, w)
})
Expand Down Expand Up @@ -448,6 +463,7 @@ function runTests({ w, isDev, domains }) {
expect(res1.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res1.headers.get('Vary')).toBe('Accept')
const etag = res1.headers.get('Etag')
expect(etag).toBeTruthy()
await expectWidth(res1, w)
Expand All @@ -460,6 +476,7 @@ function runTests({ w, isDev, domains }) {
expect(res2.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res2.headers.get('Vary')).toBe('Accept')
expect((await res2.buffer()).length).toBe(0)

const query3 = { url: '/test.jpg', w, q: 25 }
Expand All @@ -469,6 +486,7 @@ function runTests({ w, isDev, domains }) {
expect(res3.headers.get('Cache-Control')).toBe(
`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')).not.toBe(etag)
await expectWidth(res3, w)
Expand All @@ -486,6 +504,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()

const json2 = await fsToJson(imagesDir)
Expand All @@ -501,6 +520,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
await expectWidth(res, 400)
})
Expand All @@ -516,6 +536,7 @@ function runTests({ w, isDev, domains }) {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : 60}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')

const png = await res.buffer()

Expand All @@ -540,6 +561,7 @@ function runTests({ w, isDev, domains }) {
expect(res1.headers.get('Cache-Control')).toBe(
'public, max-age=315360000, immutable'
)
expect(res1.headers.get('Vary')).toBe('Accept')
await expectWidth(res1, w)

// Ensure subsequent request also has immutable header
Expand All @@ -548,6 +570,7 @@ function runTests({ w, isDev, domains }) {
expect(res2.headers.get('Cache-Control')).toBe(
'public, max-age=315360000, immutable'
)
expect(res2.headers.get('Vary')).toBe('Accept')
await expectWidth(res2, w)
}
})
Expand Down Expand Up @@ -873,6 +896,7 @@ describe('Image Optimizer', () => {
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=31536000, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
await expectWidth(res, 64)
})
})
Expand Down