-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: Steven <[email protected]> * 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: Steven <[email protected]> * update squoosh specific test * Apply suggestions from code review Co-authored-by: Steven <[email protected]> * update tests * Apply suggestions from code review Co-authored-by: Steven <[email protected]> Co-authored-by: Steven <[email protected]> Co-authored-by: JJ Kasper <[email protected]>
- Loading branch information
1 parent
1651129
commit 1b73342
Showing
25 changed files
with
360 additions
and
173 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 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) |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
package.json | ||
yarn.lock |
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions
10
...ptimizer/test/detect-content-type.test.js → ...zer/test/detect-content-type.unit.test.js
This file contains 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 |
---|---|---|
@@ -1,25 +1,25 @@ | ||
/* eslint-env jest */ | ||
import { detectContentType } from '../../../../packages/next/dist/server/image-optimizer.js' | ||
import { readFile } from 'fs/promises' | ||
import { readFile } from 'fs-extra' | ||
import { join } from 'path' | ||
|
||
const getImage = (filepath) => readFile(join(__dirname, filepath)) | ||
|
||
describe('detectContentType', () => { | ||
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') | ||
}) | ||
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') | ||
}) | ||
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') | ||
}) | ||
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') | ||
}) | ||
}) |
File renamed without changes.
Oops, something went wrong.