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
Update no-img-element
lint rule
#43982
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
77aa35d
Update `no-img-element` lint rule
styfle bd5c3a4
Update message
styfle 54c684f
Another update
styfle 0da4b78
Merge branch 'canary' into update-img-lint-rule
styfle 0e4aef7
fix one
styfle 9b17fb2
Fix another test
styfle 338658f
Merge branch 'canary' into update-img-lint-rule
ijjk File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,59 @@ | ||
# No Img Element | ||
# No img element | ||
|
||
> Prevent usage of `<img>` element to prevent layout shift. | ||
> Prevent usage of `<img>` element to prevent layout shift and favor [optimized images](https://nextjs.org/docs/basic-features/image-optimization). | ||
|
||
### Why This Error Occurred | ||
|
||
An `<img>` element was used to display an image. Use either `<picture>` in conjunction with `<img>` element, or use `next/image` that has better performance and automatic Image Optimization over `<img>`. | ||
An `<img>` element was used to display an image. | ||
|
||
### Possible Ways to Fix It | ||
|
||
Import and use the `<Image />` component: | ||
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 that 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. | ||
|
||
```jsx | ||
import Image from 'next/image' | ||
|
||
function Home() { | ||
return ( | ||
<> | ||
<Image | ||
src="https://example.com/test" | ||
alt="Landscape picture" | ||
width={500} | ||
height={500} | ||
/> | ||
</> | ||
<Image | ||
src="https://example.com/hero.jpg" | ||
alt="Landscape picture" | ||
width={800} | ||
height={500} | ||
/> | ||
) | ||
} | ||
|
||
export default Home | ||
``` | ||
|
||
If you would like to use `next/image` featrues 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 /> | ||
|
||
Use `<picture>` in conjunction with `<img>` element: | ||
Or, use a `<picture>` element with the nested `<img>` element: | ||
|
||
```jsx | ||
function Home() { | ||
return ( | ||
<> | ||
<picture> | ||
<source srcSet="https://example.com/test" type="image/webp" /> | ||
<img src="https://example.com/test" alt="Landscape picture" /> | ||
</picture> | ||
</> | ||
<picture> | ||
<source srcSet="https://example.com/hero.avif" type="image/avif" /> | ||
<source srcSet="https://example.com/hero.webp" type="image/webp" /> | ||
<img | ||
src="https://example.com/hero.jpg" | ||
alt="Landscape picture" | ||
width={800} | ||
height={500} | ||
/> | ||
</picture> | ||
) | ||
} | ||
``` | ||
|
||
### Useful Links | ||
|
||
- [Image Component and Image Optimization](https://nextjs.org/docs/basic-features/image-optimization) | ||
- [next/image API Reference](https://nextjs.org/docs/api-reference/next/image) | ||
- [Largest Contentful Paint (LCP)](https://nextjs.org/learn/seo/web-performance/lcp) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we didn't include the section on
unoptimized
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its on line 32 but linked to instead of copy pasta. This doc is for the lint error, not really for
next/image
itself.