Skip to content

Commit 381d1f9

Browse files
authored
feat(next/image): add support for decoding prop (#70678)
- Backport #70298 to `14.2.x`
1 parent dbbec6a commit 381d1f9

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

docs/02-app/02-api-reference/01-components/image.mdx

+13-1
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,24 @@ For example, when upgrading an existing website from `<img>` to `<Image>`, you m
461461
/>
462462
```
463463

464+
### decoding
465+
466+
A hint to the browser indicating if it should wait for the image to be decoded before presenting other content updates or not. Defaults to `async`.
467+
468+
Possible values are the following:
469+
470+
- `async` - Asynchronously decode the image and allow other content to be rendered before it completes.
471+
- `sync` - Synchronously decode the image for atomic presentation with other content.
472+
- `auto` - No preference for the decoding mode; the browser decides what's best.
473+
474+
Learn more about the [`decoding` attribute](https://developer.mozilla.org/docs/Web/HTML/Element/img#loading).
475+
464476
### Other Props
465477

466478
Other properties on the `<Image />` component will be passed to the underlying
467479
`img` element with the exception of the following:
468480

469481
- `srcSet`. Use [Device Sizes](#devicesizes) instead.
470-
- `decoding`. It is always `"async"`.
471482

472483
## Configuration Options
473484

@@ -1020,6 +1031,7 @@ This `next/image` component uses browser native [lazy loading](https://caniuse.c
10201031

10211032
| Version | Changes |
10221033
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1034+
| `v14.2.15` | `decoding` prop added. |
10231035
| `v14.2.0` | `overrideSrc` prop added. |
10241036
| `v14.1.0` | `getImageProps()` is stable. |
10251037
| `v14.0.0` | `onLoadingComplete` prop and `domains` config deprecated. |

packages/next/src/shared/lib/get-img-props.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ export function getImgProps(
252252
placeholder = 'empty',
253253
blurDataURL,
254254
fetchPriority,
255+
decoding = 'async',
255256
layout,
256257
objectFit,
257258
objectPosition,
@@ -673,7 +674,7 @@ export function getImgProps(
673674
fetchPriority,
674675
width: widthInt,
675676
height: heightInt,
676-
decoding: 'async',
677+
decoding,
677678
className,
678679
style: { ...imgStyle, ...placeholderStyle },
679680
sizes: imgAttributes.sizes,

test/unit/next-image-get-img-props.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,27 @@ describe('getImageProps()', () => {
277277
['src', '/override.png'],
278278
])
279279
})
280+
it('should handle decoding=sync', async () => {
281+
const { props } = getImageProps({
282+
alt: 'a nice desc',
283+
src: '/test.png',
284+
decoding: 'sync',
285+
width: 100,
286+
height: 200,
287+
})
288+
expect(warningMessages).toStrictEqual([])
289+
expect(Object.entries(props)).toStrictEqual([
290+
['alt', 'a nice desc'],
291+
['loading', 'lazy'],
292+
['width', 100],
293+
['height', 200],
294+
['decoding', 'sync'],
295+
['style', { color: 'transparent' }],
296+
[
297+
'srcSet',
298+
'/_next/image?url=%2Ftest.png&w=128&q=75 1x, /_next/image?url=%2Ftest.png&w=256&q=75 2x',
299+
],
300+
['src', '/_next/image?url=%2Ftest.png&w=256&q=75'],
301+
])
302+
})
280303
})

0 commit comments

Comments
 (0)