Skip to content

Commit 0bfa3ec

Browse files
authored
feat(next/image): add support for decoding prop (#70298)
- Fixes #49772
1 parent e3688fb commit 0bfa3ec

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,24 @@ For example, when upgrading an existing website from `<img>` to `<Image>`, you m
467467
/>
468468
```
469469

470+
### decoding
471+
472+
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`.
473+
474+
Possible values are the following:
475+
476+
- `async` - Asynchronously decode the image and allow other content to be rendered before it completes.
477+
- `sync` - Synchronously decode the image for atomic presentation with other content.
478+
- `auto` - No preference for the decoding mode; the browser decides what's best.
479+
480+
Learn more about the [`decoding` attribute](https://developer.mozilla.org/docs/Web/HTML/Element/img#loading).
481+
470482
### Other Props
471483

472484
Other properties on the `<Image />` component will be passed to the underlying
473485
`img` element with the exception of the following:
474486

475487
- `srcSet`. Use [Device Sizes](#devicesizes) instead.
476-
- `decoding`. It is always `"async"`.
477488

478489
## Configuration Options
479490

@@ -1022,7 +1033,7 @@ This `next/image` component uses browser native [lazy loading](https://caniuse.c
10221033

10231034
| Version | Changes |
10241035
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1025-
| `v15.0.0` | `contentDispositionType` configuration default changed to `attachment`. |
1036+
| `v15.0.0` | `decoding` prop added. `contentDispositionType` configuration default changed to `attachment`. |
10261037
| `v14.2.0` | `overrideSrc` prop added. |
10271038
| `v14.1.0` | `getImageProps()` is stable. |
10281039
| `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
@@ -254,6 +254,7 @@ export function getImgProps(
254254
placeholder = 'empty',
255255
blurDataURL,
256256
fetchPriority,
257+
decoding = 'async',
257258
layout,
258259
objectFit,
259260
objectPosition,
@@ -684,7 +685,7 @@ export function getImgProps(
684685
fetchPriority,
685686
width: widthInt,
686687
height: heightInt,
687-
decoding: 'async',
688+
decoding,
688689
className,
689690
style: { ...imgStyle, ...placeholderStyle },
690691
sizes: imgAttributes.sizes,

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

+23
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,27 @@ describe('getImageProps()', () => {
302302
['src', '/override.png'],
303303
])
304304
})
305+
it('should handle decoding=sync', async () => {
306+
const { props } = getImageProps({
307+
alt: 'a nice desc',
308+
src: '/test.png',
309+
decoding: 'sync',
310+
width: 100,
311+
height: 200,
312+
})
313+
expect(warningMessages).toStrictEqual([])
314+
expect(Object.entries(props)).toStrictEqual([
315+
['alt', 'a nice desc'],
316+
['loading', 'lazy'],
317+
['width', 100],
318+
['height', 200],
319+
['decoding', 'sync'],
320+
['style', { color: 'transparent' }],
321+
[
322+
'srcSet',
323+
'/_next/image?url=%2Ftest.png&w=128&q=75 1x, /_next/image?url=%2Ftest.png&w=256&q=75 2x',
324+
],
325+
['src', '/_next/image?url=%2Ftest.png&w=256&q=75'],
326+
])
327+
})
305328
})

0 commit comments

Comments
 (0)