Skip to content

Commit 0bbbfa4

Browse files
Ryuurockstyfle
andauthored
Fix next/image noscript tag to only render when lazy (#32918)
Noscript is not required for Image that are loaded immediately ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` Co-authored-by: Steven <[email protected]>
1 parent c320249 commit 0bbbfa4

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

packages/next/client/image.tsx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -693,26 +693,28 @@ export default function Image({
693693
}}
694694
style={{ ...imgStyle, ...blurStyle }}
695695
/>
696-
<noscript>
697-
<img
698-
{...rest}
699-
{...generateImgAttrs({
700-
src,
701-
unoptimized,
702-
layout,
703-
width: widthInt,
704-
quality: qualityInt,
705-
sizes,
706-
loader,
707-
})}
708-
decoding="async"
709-
data-nimg={layout}
710-
style={imgStyle}
711-
className={className}
712-
// @ts-ignore - TODO: upgrade to `@types/react@17`
713-
loading={loading || 'lazy'}
714-
/>
715-
</noscript>
696+
{isLazy && (
697+
<noscript>
698+
<img
699+
{...rest}
700+
{...generateImgAttrs({
701+
src,
702+
unoptimized,
703+
layout,
704+
width: widthInt,
705+
quality: qualityInt,
706+
sizes,
707+
loader,
708+
})}
709+
decoding="async"
710+
data-nimg={layout}
711+
style={imgStyle}
712+
className={className}
713+
// @ts-ignore - TODO: upgrade to `@types/react@17`
714+
loading={loading || 'lazy'}
715+
/>
716+
</noscript>
717+
)}
716718

717719
{priority ? (
718720
// Note how we omit the `href` attribute, as it would only be relevant

test/unit/image-rendering.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,30 @@ describe('Image rendering', () => {
2020
expect(img.attr('src')).toContain('test.png')
2121
expect(img.attr('srcset')).toContain('test.png')
2222
})
23+
24+
it('should only render noscript element when lazy loading', async () => {
25+
const element = React.createElement(Image, {
26+
src: '/test.png',
27+
width: 100,
28+
height: 100,
29+
loading: 'eager',
30+
})
31+
const element2 = React.createElement(Image, {
32+
src: '/test.png',
33+
width: 100,
34+
height: 100,
35+
priority: true,
36+
})
37+
const elementLazy = React.createElement(Image, {
38+
src: '/test.png',
39+
width: 100,
40+
height: 100,
41+
})
42+
const $ = cheerio.load(ReactDOM.renderToString(element))
43+
const $2 = cheerio.load(ReactDOM.renderToString(element2))
44+
const $lazy = cheerio.load(ReactDOM.renderToString(elementLazy))
45+
expect($('noscript').length).toBe(0)
46+
expect($2('noscript').length).toBe(0)
47+
expect($lazy('noscript').length).toBe(1)
48+
})
2349
})

0 commit comments

Comments
 (0)