Unverified Commit 1b733423 authored by Alex Castle's avatar Alex Castle Committed by GitHub

Use Sharp if available for Image Optimization (#27346)

* Use sharp for image transformations when available

* Refactor resizeImage and add sharp warning

* only show sharp warning once per instance

* Modify sharp error message

* Add documentation for optional sharp dependency

* Update docs/basic-features/image-optimization.md
Co-authored-by: 's avatarSteven <steven@ceriously.com>

* Import types for sharp

* Update lockfile

* Add testing against sharp

* use fs-extra for node 12

* Rename test sharp path variable

* Apply suggestions from code review
Co-authored-by: 's avatarSteven <steven@ceriously.com>

* update squoosh specific test

* Apply suggestions from code review
Co-authored-by: 's avatarSteven <steven@ceriously.com>

* update tests

* Apply suggestions from code review
Co-authored-by: 's avatarSteven <steven@ceriously.com>
Co-authored-by: 's avatarSteven <steven@ceriously.com>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 16511294
...@@ -128,6 +128,8 @@ If you need a different provider, you can use the [`loader`](/docs/api-reference ...@@ -128,6 +128,8 @@ If you need a different provider, you can use the [`loader`](/docs/api-reference
> The `next/image` component's default loader is not supported when using [`next export`](/docs/advanced-features/static-html-export.md). However, other loader options will work. > The `next/image` component's default loader is not supported when using [`next export`](/docs/advanced-features/static-html-export.md). However, other loader options will work.
> The `next/image` component's default loader uses the ['squoosh'](https://www.npmjs.com/package/@squoosh/lib) library for image resizing and optimization. This library is quick to install and suitable for a dev server environment. For a production environment, it is strongly recommended that you install the optional [`sharp`](https://www.npmjs.com/package/sharp) library by running `yarn add sharp` in your project directory. If sharp is already installed but can't be resolved you can manually pass the path to it via the `NEXT_SHARP_PATH` environment variable e.g. `NEXT_SHARP_PATH=/tmp/node_modules/sharp`
## Caching ## Caching
The following describes the caching algorithm for the default [loader](#loader). For all other loaders, please refer to your cloud provider's documentation. The following describes the caching algorithm for the default [loader](#loader). For all other loaders, please refer to your cloud provider's documentation.
......
...@@ -426,6 +426,10 @@ ...@@ -426,6 +426,10 @@
{ {
"title": "page-data-collection-timeout", "title": "page-data-collection-timeout",
"path": "/errors/page-data-collection-timeout.md" "path": "/errors/page-data-collection-timeout.md"
},
{
"titlle": "sharp-missing-in-production",
"path": "/errors/sharp-missing-in-production.md"
} }
] ]
} }
......
# Sharp Missing In Production
#### Why This Error Occurred
The `next/image` component's default loader uses the ['squoosh'](https://www.npmjs.com/package/@squoosh/lib) library for image resizing and optimization. This library is quick to install and suitable for a dev server environment. For a production environment, it is strongly recommended that you install the optional [`sharp`](https://www.npmjs.com/package/sharp). This package was not detected when leveraging the Image Optimization in production mode (`next start`).
#### Possible Ways to Fix It
Install `sharp` by running `yarn add sharp` in your project directory.
### Useful Links
- [Image Optimization Documentation](https://nextjs.org/docs/basic-features/image-optimization)
...@@ -53,6 +53,7 @@ ...@@ -53,6 +53,7 @@
"@types/fs-extra": "8.1.0", "@types/fs-extra": "8.1.0",
"@types/http-proxy": "1.17.3", "@types/http-proxy": "1.17.3",
"@types/jest": "24.0.13", "@types/jest": "24.0.13",
"@types/sharp": "0.28.4",
"@types/string-hash": "1.1.1", "@types/string-hash": "1.1.1",
"@typescript-eslint/eslint-plugin": "4.22.0", "@typescript-eslint/eslint-plugin": "4.22.0",
"@typescript-eslint/parser": "4.22.0", "@typescript-eslint/parser": "4.22.0",
......
import loaderUtils from 'next/dist/compiled/loader-utils' import loaderUtils from 'next/dist/compiled/loader-utils'
import sizeOf from 'image-size' import sizeOf from 'image-size'
import { processBuffer } from '../../../server/lib/squoosh/main' import { resizeImage } from '../../../server/image-optimizer'
const BLUR_IMG_SIZE = 8 const BLUR_IMG_SIZE = 8
const BLUR_QUALITY = 70 const BLUR_QUALITY = 70
...@@ -38,14 +38,18 @@ function nextImageLoader(content) { ...@@ -38,14 +38,18 @@ function nextImageLoader(content) {
blurDataURL = url.href.slice(prefix.length) blurDataURL = url.href.slice(prefix.length)
} else { } else {
// Shrink the image's largest dimension // Shrink the image's largest dimension
const resizeOperationOpts = const dimension =
imageSize.width >= imageSize.height imageSize.width >= imageSize.height ? 'width' : 'height'
? { type: 'resize', width: BLUR_IMG_SIZE }
: { type: 'resize', height: BLUR_IMG_SIZE }
const resizeImageSpan = imageLoaderSpan.traceChild('image-resize') const resizeImageSpan = imageLoaderSpan.traceChild('image-resize')
const resizedImage = await resizeImageSpan.traceAsyncFn(() => const resizedImage = await resizeImageSpan.traceAsyncFn(() =>
processBuffer(content, [resizeOperationOpts], extension, BLUR_QUALITY) resizeImage(
content,
dimension,
BLUR_IMG_SIZE,
extension,
BLUR_QUALITY
)
) )
const blurDataURLSpan = imageLoaderSpan.traceChild( const blurDataURLSpan = imageLoaderSpan.traceChild(
'image-base64-tostring' 'image-base64-tostring'
......
...@@ -15,6 +15,7 @@ import { processBuffer, Operation } from './lib/squoosh/main' ...@@ -15,6 +15,7 @@ import { processBuffer, Operation } from './lib/squoosh/main'
import Server from './next-server' import Server from './next-server'
import { sendEtagResponse } from './send-payload' import { sendEtagResponse } from './send-payload'
import { getContentType, getExtension } from './serve-static' import { getContentType, getExtension } from './serve-static'
import chalk from 'chalk'
//const AVIF = 'image/avif' //const AVIF = 'image/avif'
const WEBP = 'image/webp' const WEBP = 'image/webp'
...@@ -29,6 +30,21 @@ const VECTOR_TYPES = [SVG] ...@@ -29,6 +30,21 @@ const VECTOR_TYPES = [SVG]
const BLUR_IMG_SIZE = 8 // should match `next-image-loader` const BLUR_IMG_SIZE = 8 // should match `next-image-loader`
const inflightRequests = new Map<string, Promise<undefined>>() const inflightRequests = new Map<string, Promise<undefined>>()
let sharp:
| ((
input?: string | Buffer,
options?: import('sharp').SharpOptions
) => import('sharp').Sharp)
| undefined
try {
sharp = require(process.env.NEXT_SHARP_PATH || 'sharp')
} catch (e) {
// Sharp not present on the server, Squoosh fallback will be used
}
let shouldShowSharpWarning = process.env.NODE_ENV === 'production'
export async function imageOptimizer( export async function imageOptimizer(
server: Server, server: Server,
req: IncomingMessage, req: IncomingMessage,
...@@ -322,52 +338,87 @@ export async function imageOptimizer( ...@@ -322,52 +338,87 @@ export async function imageOptimizer(
} else { } else {
contentType = JPEG contentType = JPEG
} }
try { try {
const orientation = await getOrientation(upstreamBuffer) let optimizedBuffer: Buffer | undefined
if (sharp) {
// Begin sharp transformation logic
const transformer = sharp(upstreamBuffer)
const operations: Operation[] = [] transformer.rotate()
const { width: metaWidth } = await transformer.metadata()
if (metaWidth && metaWidth > width) {
transformer.resize(width)
}
if (contentType === WEBP) {
transformer.webp({ quality })
} else if (contentType === PNG) {
transformer.png({ quality })
} else if (contentType === JPEG) {
transformer.jpeg({ quality })
}
if (orientation === Orientation.RIGHT_TOP) { optimizedBuffer = await transformer.toBuffer()
operations.push({ type: 'rotate', numRotations: 1 }) // End sharp transformation logic
} else if (orientation === Orientation.BOTTOM_RIGHT) {
operations.push({ type: 'rotate', numRotations: 2 })
} else if (orientation === Orientation.LEFT_BOTTOM) {
operations.push({ type: 'rotate', numRotations: 3 })
} else { } else {
// TODO: support more orientations // Show sharp warning in production once
// eslint-disable-next-line @typescript-eslint/no-unused-vars if (shouldShowSharpWarning) {
// const _: never = orientation console.warn(
} chalk.yellow.bold('Warning: ') +
`For production Image Optimization with Next.js, the optional 'sharp' package is strongly recommended. Run 'yarn add sharp', and Next.js will use it automatically for image optimization.\n` +
'Read more: https://nextjs.org/docs/messages/sharp-missing-in-production'
)
shouldShowSharpWarning = false
}
operations.push({ type: 'resize', width }) // Begin Squoosh transformation logic
const orientation = await getOrientation(upstreamBuffer)
let optimizedBuffer: Buffer | undefined const operations: Operation[] = []
//if (contentType === AVIF) {
//} else if (orientation === Orientation.RIGHT_TOP) {
if (contentType === WEBP) { operations.push({ type: 'rotate', numRotations: 1 })
optimizedBuffer = await processBuffer( } else if (orientation === Orientation.BOTTOM_RIGHT) {
upstreamBuffer, operations.push({ type: 'rotate', numRotations: 2 })
operations, } else if (orientation === Orientation.LEFT_BOTTOM) {
'webp', operations.push({ type: 'rotate', numRotations: 3 })
quality } else {
) // TODO: support more orientations
} else if (contentType === PNG) { // eslint-disable-next-line @typescript-eslint/no-unused-vars
optimizedBuffer = await processBuffer( // const _: never = orientation
upstreamBuffer, }
operations,
'png', operations.push({ type: 'resize', width })
quality
) //if (contentType === AVIF) {
} else if (contentType === JPEG) { //} else
optimizedBuffer = await processBuffer( if (contentType === WEBP) {
upstreamBuffer, optimizedBuffer = await processBuffer(
operations, upstreamBuffer,
'jpeg', operations,
quality 'webp',
) quality
} )
} else if (contentType === PNG) {
optimizedBuffer = await processBuffer(
upstreamBuffer,
operations,
'png',
quality
)
} else if (contentType === JPEG) {
optimizedBuffer = await processBuffer(
upstreamBuffer,
operations,
'jpeg',
quality
)
}
// End Squoosh transformation logic
}
if (optimizedBuffer) { if (optimizedBuffer) {
await writeToCacheDir( await writeToCacheDir(
hashDir, hashDir,
...@@ -551,3 +602,42 @@ export function getMaxAge(str: string | null): number { ...@@ -551,3 +602,42 @@ export function getMaxAge(str: string | null): number {
} }
return 0 return 0
} }
export async function resizeImage(
content: Buffer,
dimension: 'width' | 'height',
size: number,
extension: 'webp' | 'png' | 'jpeg',
quality: number
): Promise<Buffer> {
if (sharp) {
const transformer = sharp(content)
if (extension === 'webp') {
transformer.webp({ quality })
} else if (extension === 'png') {
transformer.png({ quality })
} else if (extension === 'jpeg') {
transformer.jpeg({ quality })
}
if (dimension === 'width') {
transformer.resize(size)
} else {
transformer.resize(null, size)
}
const buf = await transformer.toBuffer()
return buf
} else {
const resizeOperationOpts: Operation =
dimension === 'width'
? { type: 'resize', width: size }
: { type: 'resize', height: size }
const buf = await processBuffer(
content,
[resizeOperationOpts],
extension,
quality
)
return buf
}
}
node_modules
package.json
yarn.lock
\ No newline at end of file
/* eslint-env jest */ /* eslint-env jest */
import { detectContentType } from '../../../../packages/next/dist/server/image-optimizer.js' import { detectContentType } from '../../../../packages/next/dist/server/image-optimizer.js'
import { readFile } from 'fs/promises' import { readFile } from 'fs-extra'
import { join } from 'path' import { join } from 'path'
const getImage = (filepath) => readFile(join(__dirname, filepath)) const getImage = (filepath) => readFile(join(__dirname, filepath))
describe('detectContentType', () => { describe('detectContentType', () => {
it('should return jpg', async () => { it('should return jpg', async () => {
const buffer = await getImage('../public/test.jpg') const buffer = await getImage('../app/public/test.jpg')
expect(detectContentType(buffer)).toBe('image/jpeg') expect(detectContentType(buffer)).toBe('image/jpeg')
}) })
it('should return png', async () => { it('should return png', async () => {
const buffer = await getImage('../public/test.png') const buffer = await getImage('../app/public/test.png')
expect(detectContentType(buffer)).toBe('image/png') expect(detectContentType(buffer)).toBe('image/png')
}) })
it('should return webp', async () => { it('should return webp', async () => {
const buffer = await getImage('../public/animated.webp') const buffer = await getImage('../app/public/animated.webp')
expect(detectContentType(buffer)).toBe('image/webp') expect(detectContentType(buffer)).toBe('image/webp')
}) })
it('should return svg', async () => { it('should return svg', async () => {
const buffer = await getImage('../public/test.svg') const buffer = await getImage('../app/public/test.svg')
expect(detectContentType(buffer)).toBe('image/svg+xml') expect(detectContentType(buffer)).toBe('image/svg+xml')
}) })
}) })
...@@ -4316,6 +4316,13 @@ ...@@ -4316,6 +4316,13 @@
"@types/express-serve-static-core" "*" "@types/express-serve-static-core" "*"
"@types/mime" "*" "@types/mime" "*"
"@types/sharp@0.28.4":
version "0.28.4"
resolved "https://registry.yarnpkg.com/@types/sharp/-/sharp-0.28.4.tgz#7afdcf979069ddc68a915603fdaa412ada93f833"
integrity sha512-vfz+RlJU5FgXyyf9w8wc+JwwT8MFI98NZr0272umQegrggAQhTwwb8pKZn0PTtd+j0crXkZDWw5ABG/i6Lu/Lw==
dependencies:
"@types/node" "*"
"@types/source-list-map@*": "@types/source-list-map@*":
version "0.1.2" version "0.1.2"
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9" resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment