|
| 1 | +--- |
| 2 | +slug: /api/misc/@docusaurus/eslint-plugin/prefer-ideal-image |
| 3 | +--- |
| 4 | + |
| 5 | +# prefer-ideal-image |
| 6 | + |
| 7 | +Ensure that the `<IdealImage />` component provided by the [`@docusaurus/plugin-ideal-image`](../../plugins/plugin-ideal-image.mdx) plugin is used instead of standard `<img>` tags for local assets. |
| 8 | + |
| 9 | +The `@theme/IdealImage` component automatically generates responsive images, provides lazy-loading, and adds low-quality image placeholders (LQIP) to improve LCP and user experience. |
| 10 | + |
| 11 | +## Rule Details {#details} |
| 12 | + |
| 13 | +This rule flags standard HTML `<img>` tags that point to local files, suggesting the use of `IdealImage` for better optimization. |
| 14 | + |
| 15 | +Examples of **incorrect** code for this rule: |
| 16 | + |
| 17 | +```jsx |
| 18 | +// Error: Definitely a local image via require() |
| 19 | +<img src={require('./thumbnail.png')} /> |
| 20 | + |
| 21 | +// Warning: Likely a local image via relative path |
| 22 | +<img src="./img/logo.png" /> |
| 23 | + |
| 24 | +// Warning: Root-relative path usually points to the static folder |
| 25 | +<img src="/img/hero.jpg" /> |
| 26 | +``` |
| 27 | + |
| 28 | +Examples of **correct** code for this rule: |
| 29 | + |
| 30 | +```jsx |
| 31 | +// Optimized using the IdealImage component |
| 32 | +import IdealImage from '@theme/IdealImage'; |
| 33 | + |
| 34 | +<IdealImage img={require('./thumbnail.png')} /> |
| 35 | + |
| 36 | +// External images are ignored |
| 37 | +<img src="[https://example.com/external.png](https://example.com/external.png)" /> |
| 38 | + |
| 39 | +// SVGs are ignored as IdealImage does not support them |
| 40 | +<img src="./icon.svg" /> |
| 41 | +``` |
| 42 | + |
| 43 | +## When to not use it |
| 44 | + |
| 45 | +If you have not installed or do not plan to use @docusaurus/plugin-ideal-image, you should keep this rule disabled. |
| 46 | + |
| 47 | +## Further Reading {#further-reading} |
| 48 | + |
| 49 | +- https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-ideal-image |
| 50 | +- https://web.dev/articles/browser-level-image-lazy-loading |
0 commit comments