Unverified Commit 5e112c06 authored by Michael Novotny's avatar Michael Novotny Committed by GitHub

Updates `next/image` eslint message to denote that costs may be incurred. (#46640)

Changes ESLint warning message to still encourage usage of `next/image`
for the best experience, but also denoting that optimization could come
with incurred costs.

## Feature

- [x] Documentation added
- [x] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm build && pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

---------
Co-authored-by: 's avatarSteven Tey <stevensteel97@gmail.com>
Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent 9720ba55
# No img element # No img element
> Prevent usage of `<img>` element to prevent layout shift and favor [optimized images](https://nextjs.org/docs/basic-features/image-optimization). > Prevent usage of `<img>` element due to slower LCP and higher bandwidth.
### Why This Error Occurred ### Why This Error Occurred
An `<img>` element was used to display an image. An `<img>` element was used to display an image instead of `<Image />` from `next/image`.
### Possible Ways to Fix It ### Possible Ways to Fix It
Use [`next/image`](https://nextjs.org/docs/api-reference/next/image) to improve performance with automatic [Image Optimization](https://nextjs.org/docs/basic-features/image-optimization). Use [`next/image`](https://nextjs.org/docs/api-reference/next/image) to improve performance with automatic [Image Optimization](https://nextjs.org/docs/basic-features/image-optimization).
> Note: If deploying to a [managed hosting provider](https://nextjs.org/docs/deployment), remember to check pricing since optimized images might be charged differently than the original images. If self-hosting, remember to install [`sharp`](https://www.npmjs.com/package/sharp) and check if your server has enough storage to cache the optimized images. > Note: If deploying to a [managed hosting provider](https://nextjs.org/docs/deployment), remember to check provider pricing since optimized images might be charged differently than the original images.
>
> Common image optimization platform pricing:
>
> - [Vercel pricing](https://vercel.com/pricing)
> - [Cloudinary pricing](https://cloudinary.com/pricing)
> - [imgix pricing](https://imgix.com/pricing)
> Note: If self-hosting, remember to install [`sharp`](https://www.npmjs.com/package/sharp) and check if your server has enough storage to cache the optimized images.
```jsx ```jsx
import Image from 'next/image' import Image from 'next/image'
...@@ -31,8 +39,6 @@ export default Home ...@@ -31,8 +39,6 @@ export default Home
If you would like to use `next/image` features such as blur-up placeholders but disable Image Optimization, you can do so using [unoptimized](https://nextjs.org/docs/api-reference/next/image#unoptimized). If you would like to use `next/image` features such as blur-up placeholders but disable Image Optimization, you can do so using [unoptimized](https://nextjs.org/docs/api-reference/next/image#unoptimized).
<br />
Or, use a `<picture>` element with the nested `<img>` element: Or, use a `<picture>` element with the nested `<img>` element:
```jsx ```jsx
......
...@@ -31,7 +31,7 @@ export = defineRule({ ...@@ -31,7 +31,7 @@ export = defineRule({
context.report({ context.report({
node, node,
message: `Using \`<img>\` could result in slower LCP and higher bandwidth. Use \`<Image />\` from \`next/image\` instead to utilize Image Optimization. See: ${url}`, message: `Using \`<img>\` could result in slower LCP and higher bandwidth. Consider using \`<Image />\` from \`next/image\` to automatically optimize images. This may incur additional usage or cost from your provider. See: ${url}`,
}) })
}, },
} }
......
...@@ -105,7 +105,7 @@ describe('ESLint', () => { ...@@ -105,7 +105,7 @@ describe('ESLint', () => {
'Error: `next/head` should not be imported in `pages/_document.js`. Use `<Head />` from `next/document` instead' 'Error: `next/head` should not be imported in `pages/_document.js`. Use `<Head />` from `next/document` instead'
) )
expect(output).toContain( expect(output).toContain(
'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization.' 'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images.'
) )
expect(output).toContain('Warning: Do not include stylesheets manually') expect(output).toContain('Warning: Do not include stylesheets manually')
expect(output).toContain( expect(output).toContain(
...@@ -143,7 +143,7 @@ describe('ESLint', () => { ...@@ -143,7 +143,7 @@ describe('ESLint', () => {
'Error: `next/head` should not be imported in `pages/_document.js`. Use `<Head />` from `next/document` instead' 'Error: `next/head` should not be imported in `pages/_document.js`. Use `<Head />` from `next/document` instead'
) )
expect(output).toContain( expect(output).toContain(
'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization.' 'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images.'
) )
expect(output).toContain('Warning: Do not include stylesheets manually') expect(output).toContain('Warning: Do not include stylesheets manually')
expect(output).toContain( expect(output).toContain(
...@@ -435,7 +435,7 @@ describe('ESLint', () => { ...@@ -435,7 +435,7 @@ describe('ESLint', () => {
'Error: `next/head` should not be imported in `pages/_document.js`. Use `<Head />` from `next/document` instead' 'Error: `next/head` should not be imported in `pages/_document.js`. Use `<Head />` from `next/document` instead'
) )
expect(output).toContain( expect(output).toContain(
'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization.' 'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images.'
) )
expect(output).toContain('Warning: Do not include stylesheets manually') expect(output).toContain('Warning: Do not include stylesheets manually')
expect(output).toContain( expect(output).toContain(
...@@ -471,7 +471,7 @@ describe('ESLint', () => { ...@@ -471,7 +471,7 @@ describe('ESLint', () => {
'Error: `next/head` should not be imported in `pages/_document.js`. Use `<Head />` from `next/document` instead' 'Error: `next/head` should not be imported in `pages/_document.js`. Use `<Head />` from `next/document` instead'
) )
expect(output).toContain( expect(output).toContain(
'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization.' 'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images.'
) )
expect(output).toContain('Warning: Do not include stylesheets manually') expect(output).toContain('Warning: Do not include stylesheets manually')
expect(output).toContain( expect(output).toContain(
...@@ -497,7 +497,7 @@ describe('ESLint', () => { ...@@ -497,7 +497,7 @@ describe('ESLint', () => {
const output = stdout + stderr const output = stdout + stderr
expect(output).toContain( expect(output).toContain(
'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization.' 'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images.'
) )
expect(output).toContain('Error: Synchronous scripts should not be used.') expect(output).toContain('Error: Synchronous scripts should not be used.')
}) })
...@@ -533,7 +533,7 @@ describe('ESLint', () => { ...@@ -533,7 +533,7 @@ describe('ESLint', () => {
const output = stdout + stderr const output = stdout + stderr
expect(output).toContain( expect(output).toContain(
'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization.' 'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images.'
) )
expect(output).toContain('Error: Synchronous scripts should not be used.') expect(output).toContain('Error: Synchronous scripts should not be used.')
}) })
...@@ -806,7 +806,7 @@ describe('ESLint', () => { ...@@ -806,7 +806,7 @@ describe('ESLint', () => {
expect(output).toContain('pages/bar.js') expect(output).toContain('pages/bar.js')
expect(output).toContain( expect(output).toContain(
'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization.' 'Warning: Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images.'
) )
expect(output).not.toContain('pages/index.js') expect(output).not.toContain('pages/index.js')
...@@ -851,7 +851,7 @@ describe('ESLint', () => { ...@@ -851,7 +851,7 @@ describe('ESLint', () => {
}), }),
expect.objectContaining({ expect.objectContaining({
message: message:
'Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization. See: https://nextjs.org/docs/messages/no-img-element', 'Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element',
}), }),
]) ])
) )
...@@ -890,7 +890,7 @@ describe('ESLint', () => { ...@@ -890,7 +890,7 @@ describe('ESLint', () => {
'img elements must have an alt prop, either with meaningful text, or an empty string for decorative images.' 'img elements must have an alt prop, either with meaningful text, or an empty string for decorative images.'
) )
expect(fileOutput).toContain( expect(fileOutput).toContain(
'Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization. See: https://nextjs.org/docs/messages/no-img-element' 'Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element'
) )
expect(fileOutput).toContain('file-linting/pages/index.js') expect(fileOutput).toContain('file-linting/pages/index.js')
...@@ -930,7 +930,7 @@ describe('ESLint', () => { ...@@ -930,7 +930,7 @@ describe('ESLint', () => {
'img elements must have an alt prop, either with meaningful text, or an empty string for decorative images.' 'img elements must have an alt prop, either with meaningful text, or an empty string for decorative images.'
) )
expect(output).toContain( expect(output).toContain(
'Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization. See: https://nextjs.org/docs/messages/no-img-element' 'Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element'
) )
expect(output).toContain('pages/index.cjs') expect(output).toContain('pages/index.cjs')
......
...@@ -81,8 +81,7 @@ ruleTester.run('no-img-element', rule, { ...@@ -81,8 +81,7 @@ ruleTester.run('no-img-element', rule, {
errors: [ errors: [
{ {
message: message:
'Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization. ' + 'Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element',
'See: https://nextjs.org/docs/messages/no-img-element',
type: 'JSXOpeningElement', type: 'JSXOpeningElement',
}, },
], ],
...@@ -104,8 +103,7 @@ ruleTester.run('no-img-element', rule, { ...@@ -104,8 +103,7 @@ ruleTester.run('no-img-element', rule, {
errors: [ errors: [
{ {
message: message:
'Using `<img>` could result in slower LCP and higher bandwidth. Use `<Image />` from `next/image` instead to utilize Image Optimization. ' + 'Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element',
'See: https://nextjs.org/docs/messages/no-img-element',
type: 'JSXOpeningElement', type: 'JSXOpeningElement',
}, },
], ],
......
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