Skip to content

Commit

Permalink
fix(#39706): add avif support for node serve static (#39733)
Browse files Browse the repository at this point in the history
The PR fixes #39706 by adding `avif` mime type directly to `send`. The PR also removes the previous avif workaround for image optimizer.

Note: The PR is still a workaround for now. I will submit a PR to `pillarjs/send` to help them update `mime` to fix the issue once and for all. But now `send.mime.define` just works.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
  • Loading branch information
SukkaW authored Aug 18, 2022
1 parent f7eed07 commit 3466862
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/next/server/serve-static.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { IncomingMessage, ServerResponse } from 'http'
import send from 'next/dist/compiled/send'

// TODO: Remove this once "send" has updated the "mime", or next.js use custom version of "mime"
// Although "mime" has already add avif in version 2.4.7, "send" is still using mime@1.6.0
send.mime.define({
'image/avif': ['avif'],
})

export function serveStatic(
req: IncomingMessage,
res: ServerResponse,
Expand All @@ -21,10 +27,6 @@ export function serveStatic(
}

export function getContentType(extWithoutDot: string): string | null {
if (extWithoutDot === 'avif') {
// TODO: update "mime" package
return 'image/avif'
}
const { mime } = send
if ('getType' in mime) {
// 2.0
Expand All @@ -35,10 +37,6 @@ export function getContentType(extWithoutDot: string): string | null {
}

export function getExtension(contentType: string): string | null {
if (contentType === 'image/avif') {
// TODO: update "mime" package
return 'avif'
}
const { mime } = send
if ('getExtension' in mime) {
// 2.0
Expand Down
Binary file not shown.
7 changes: 7 additions & 0 deletions test/integration/file-serving/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ const runTests = () => {
expect(await res.text()).toBe('hi')
})

it('should serve avif image with correct content-type', async () => {
// vercel-icon-dark.avif is downloaded from https://vercel.com/design and transformed to avif on avif.io
const res = await fetchViaHTTP(appPort, '/vercel-icon-dark.avif')
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('image/avif')
})

// checks against traversal requests from
// https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Directory%20Traversal/Intruder/traversals-8-deep-exotic-encoding.txt

Expand Down

0 comments on commit 3466862

Please sign in to comment.