Unverified Commit 604b0129 authored by Steven's avatar Steven Committed by GitHub

fix: lazy load dynamically generated `next/image` in Safari Firefox (#46306)

- Fixes https://github.com/vercel/next.js/issues/46019
- Related to https://github.com/facebook/react/issues/25883

---------
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 5dee445e
...@@ -396,15 +396,18 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>( ...@@ -396,15 +396,18 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>(
<> <>
<img <img
{...rest} {...rest}
{...imgAttributes} // @ts-ignore - TODO: upgrade to `@types/react@17`
loading={loading}
width={widthInt} width={widthInt}
height={heightInt} height={heightInt}
decoding="async" decoding="async"
data-nimg={fill ? 'fill' : '1'} data-nimg={fill ? 'fill' : '1'}
className={className} className={className}
// @ts-ignore - TODO: upgrade to `@types/react@17`
loading={loading}
style={{ ...imgStyle, ...blurStyle }} style={{ ...imgStyle, ...blurStyle }}
// It's intended to keep `loading` before `src` because React updates
// props in order which causes Safari/Firefox to not lazy load properly.
// See https://github.com/facebook/react/issues/25883
{...imgAttributes}
ref={useCallback( ref={useCallback(
(img: ImgElementWithDataProp | null) => { (img: ImgElementWithDataProp | null) => {
if (forwardedRef) { if (forwardedRef) {
......
...@@ -559,6 +559,15 @@ const ImageElement = ({ ...@@ -559,6 +559,15 @@ const ImageElement = ({
<noscript> <noscript>
<img <img
{...rest} {...rest}
// @ts-ignore - TODO: upgrade to `@types/react@17`
loading={loading}
decoding="async"
data-nimg={layout}
style={imgStyle}
className={className}
// It's intended to keep `loading` before `src` because React updates
// props in order which causes Safari/Firefox to not lazy load properly.
// See https://github.com/facebook/react/issues/25883
{...generateImgAttrs({ {...generateImgAttrs({
config, config,
src: srcString, src: srcString,
...@@ -569,12 +578,6 @@ const ImageElement = ({ ...@@ -569,12 +578,6 @@ const ImageElement = ({
sizes: noscriptSizes, sizes: noscriptSizes,
loader, loader,
})} })}
decoding="async"
data-nimg={layout}
style={imgStyle}
className={className}
// @ts-ignore - TODO: upgrade to `@types/react@17`
loading={loading}
/> />
</noscript> </noscript>
)} )}
......
...@@ -7,7 +7,7 @@ import cheerio from 'cheerio' ...@@ -7,7 +7,7 @@ import cheerio from 'cheerio'
describe('Image rendering', () => { describe('Image rendering', () => {
it('should render Image on its own', async () => { it('should render Image on its own', async () => {
const element = React.createElement(Image, { const element = React.createElement(Image, {
alt: 'unit-image', alt: 'a nice image',
id: 'unit-image', id: 'unit-image',
src: '/test.png', src: '/test.png',
width: 100, width: 100,
...@@ -17,9 +17,20 @@ describe('Image rendering', () => { ...@@ -17,9 +17,20 @@ describe('Image rendering', () => {
const html = ReactDOM.renderToString(element) const html = ReactDOM.renderToString(element)
const $ = cheerio.load(html) const $ = cheerio.load(html)
const img = $('#unit-image') const img = $('#unit-image')
expect(img.attr('id')).toBe('unit-image') // order matters here
expect(img.attr('src')).toContain('test.png') expect(img.attr()).toStrictEqual({
expect(img.attr('srcset')).toContain('test.png') alt: 'a nice image',
id: 'unit-image',
loading: 'eager',
width: '100',
height: '100',
decoding: 'async',
'data-nimg': '1',
style: 'color:transparent',
srcset:
'/_next/image?url=%2Ftest.png&w=128&q=75 1x, /_next/image?url=%2Ftest.png&w=256&q=75 2x',
src: '/_next/image?url=%2Ftest.png&w=256&q=75',
})
}) })
it('should only render noscript element when lazy loading', async () => { it('should only render noscript element when lazy loading', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment