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

fix(#39706): add avif support for node serve static #39733

Merged
merged 2 commits into from Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions packages/next/server/serve-static.ts
@@ -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
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