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: skip resizing image if it's animated #39325

Merged
merged 7 commits into from Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions packages/next/server/image-optimizer.ts
Expand Up @@ -4,7 +4,6 @@ import { promises } from 'fs'
import { getOrientation, Orientation } from 'next/dist/compiled/get-orientation'
import imageSizeOf from 'next/dist/compiled/image-size'
import { IncomingMessage, ServerResponse } from 'http'
// @ts-ignore no types for is-animated
import isAnimated from 'next/dist/compiled/is-animated'
import contentDisposition from 'next/dist/compiled/content-disposition'
import { join } from 'path'
Expand Down Expand Up @@ -726,7 +725,9 @@ export async function resizeImage(
extension: 'avif' | 'webp' | 'png' | 'jpeg',
quality: number
): Promise<Buffer> {
if (sharp) {
if (isAnimated(content)) {
return content
} else if (sharp) {
const transformer = sharp(content)

if (extension === 'avif') {
Expand Down
4 changes: 4 additions & 0 deletions packages/next/types/misc.d.ts
Expand Up @@ -367,3 +367,7 @@ declare module 'next/dist/compiled/watchpack' {

export default Watchpack
}

declare module 'next/dist/compiled/is-animated' {
export default function isAnimated(buffer: Buffer): boolean
}
Binary file not shown.
20 changes: 20 additions & 0 deletions test/production/skip-animated-image-import-resize/index.test.ts
@@ -0,0 +1,20 @@
import { createNext, FileRef } from 'e2e-utils'
import path from 'path'

describe('Skip animated image import resize', () => {
it('should build with', async () => {
const next = await createNext({
files: {
'pages/index.js': `
import Image from 'next/image'
import cat from '../public/cat.webp'
export default function Home() {
return <Image src={cat} alt="cat" />
}`,
'public/cat.webp': new FileRef(path.join(__dirname, 'cat.webp')),
},
})
expect(true).toBe(true)
styfle marked this conversation as resolved.
Show resolved Hide resolved
await next.destroy()
})
})