-
Notifications
You must be signed in to change notification settings - Fork 29.4k
[backport v14]: fix(next/image): improve and simplify detect-content-type (#82118) #82179
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d00e21a
fix(next/image): improve and simplify detect-content-type (#82118)
styfle 1f65c9e
fix types & maxAge
ztanner c253ab9
revert unintended changes & fix failing tests
ztanner 539a961
remove unused const
ztanner d0f4785
revert unintended change
ztanner 3f00274
adjust test
ztanner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ import send from 'next/dist/compiled/send' | |
// Although "mime" has already add avif in version 2.4.7, "send" is still using [email protected] | ||
send.mime.define({ | ||
'image/avif': ['avif'], | ||
'image/x-icns': ['icns'], | ||
'image/jxl': ['jxl'], | ||
'image/heic': ['heic'], | ||
}) | ||
|
||
export function serveStatic( | ||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,72 @@ export function runTests(ctx) { | |
expect(res.status).toBe(200) | ||
}) | ||
|
||
it('should maintain icns', async () => { | ||
const query = { w: ctx.w, q: 90, url: '/test.icns' } | ||
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {}) | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('Content-Type')).toContain('image/x-icns') | ||
expect(res.headers.get('Cache-Control')).toBe( | ||
`public, max-age=0, must-revalidate` | ||
) | ||
expect(res.headers.get('Vary')).toBe('Accept') | ||
expect(res.headers.get('etag')).toBeTruthy() | ||
expect(res.headers.get('Content-Disposition')).toBe( | ||
`${contentDispositionType}; filename="test.icns"` | ||
) | ||
await expectWidth(res, 256) | ||
}) | ||
|
||
it('should maintain jxl', async () => { | ||
const query = { w: ctx.w, q: 90, url: '/test.jxl' } | ||
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {}) | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('Content-Type')).toContain('image/jxl') | ||
expect(res.headers.get('Cache-Control')).toBe( | ||
`public, max-age=0, must-revalidate` | ||
) | ||
expect(res.headers.get('Vary')).toBe('Accept') | ||
expect(res.headers.get('etag')).toBeTruthy() | ||
expect(res.headers.get('Content-Disposition')).toBe( | ||
`${contentDispositionType}; filename="test.jxl"` | ||
) | ||
// JXL is a bypass type, served as-is without processing | ||
// [email protected] doesn't support JXL, so skip width check | ||
}) | ||
|
||
it('should maintain heic', async () => { | ||
const query = { w: ctx.w, q: 90, url: '/test.heic' } | ||
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {}) | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('Content-Type')).toContain('image/heic') | ||
expect(res.headers.get('Cache-Control')).toBe( | ||
`public, max-age=0, must-revalidate` | ||
) | ||
expect(res.headers.get('Vary')).toBe('Accept') | ||
expect(res.headers.get('etag')).toBeTruthy() | ||
expect(res.headers.get('Content-Disposition')).toBe( | ||
`${contentDispositionType}; filename="test.heic"` | ||
) | ||
// HEIC is a bypass type, served as-is without processing | ||
// [email protected] doesn't support HEIC, so skip width check | ||
}) | ||
|
||
it('should maintain jp2', async () => { | ||
const query = { w: ctx.w, q: 90, url: '/test.jp2' } | ||
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {}) | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('Content-Type')).toContain('image/jp2') | ||
expect(res.headers.get('Cache-Control')).toBe( | ||
`public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate` | ||
) | ||
expect(res.headers.get('Vary')).toBe('Accept') | ||
expect(res.headers.get('etag')).toBeTruthy() | ||
expect(res.headers.get('Content-Disposition')).toBe( | ||
`${contentDispositionType}; filename="test.jp2"` | ||
) | ||
await expectWidth(res, 1) | ||
}) | ||
|
||
it('should maintain animated gif', async () => { | ||
const query = { w: ctx.w, q: 90, url: '/animated.gif' } | ||
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {}) | ||
|
@@ -288,7 +354,6 @@ export function runTests(ctx) { | |
'utf8' | ||
) | ||
expect(actual).toMatch(expected) | ||
expect(ctx.nextOutput).not.toContain('The requested resource') | ||
}) | ||
} else { | ||
it('should not allow vector svg', async () => { | ||
|
@@ -305,7 +370,7 @@ export function runTests(ctx) { | |
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) | ||
expect(res.status).toBe(400) | ||
expect(await res.text()).toContain( | ||
"The requested resource isn't a valid image" | ||
'"url" parameter is valid but image type is not allowed' | ||
) | ||
}) | ||
|
||
|
@@ -315,7 +380,7 @@ export function runTests(ctx) { | |
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) | ||
expect(res.status).toBe(400) | ||
expect(await res.text()).toContain( | ||
"The requested resource isn't a valid image" | ||
'"url" parameter is valid but image type is not allowed' | ||
) | ||
}) | ||
|
||
|
@@ -330,14 +395,24 @@ export function runTests(ctx) { | |
}) | ||
} | ||
|
||
it('should not allow pdf format', async () => { | ||
const query = { w: ctx.w, q: 90, url: '/test.pdf' } | ||
const opts = { headers: { accept: 'image/webp' } } | ||
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) | ||
expect(res.status).toBe(400) | ||
expect(await res.text()).toContain( | ||
"The requested resource isn't a valid image" | ||
) | ||
}) | ||
|
||
it('should maintain ico format', async () => { | ||
const query = { w: ctx.w, q: 90, url: `/test.ico` } | ||
const opts = { headers: { accept: 'image/webp' } } | ||
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('Content-Type')).toContain('image/x-icon') | ||
expect(res.headers.get('Cache-Control')).toBe( | ||
`public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate` | ||
`public, max-age=0, must-revalidate` | ||
) | ||
expect(res.headers.get('Vary')).toMatch(/^Accept(,|$)/) | ||
expect(res.headers.get('etag')).toBeTruthy() | ||
|
@@ -933,8 +1008,8 @@ export function runTests(ctx) { | |
const opts = { headers: { accept: 'image/webp' } } | ||
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) | ||
expect(res.status).toBe(400) | ||
expect(await res.text()).toBe( | ||
`Unable to optimize image and unable to fallback to upstream image` | ||
expect(await res.text()).toContain( | ||
"The requested resource isn't a valid image" | ||
) | ||
}) | ||
|
||
|
@@ -1179,7 +1254,7 @@ export function runTests(ctx) { | |
expect(res.status).toBe(200) | ||
expect(res.headers.get('Content-Type')).toBe('image/bmp') | ||
expect(res.headers.get('Cache-Control')).toBe( | ||
`public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate` | ||
`public, max-age=0, must-revalidate` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I wouldn't expect this to change 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see the same for ico so must be because of the bypass. |
||
) | ||
// bmp is compressible so will have accept-encoding set from | ||
// compression | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.