Unverified Commit 48dd9954 authored by Bruno Crosier's avatar Bruno Crosier Committed by GitHub

Add fallback for <Image /> component when JavaScript is disabled in browser (#19052)

The current `<Image />` component does not fallback gracefully when JavaScript is disabled in the client / browser.

You can test this with the [official Next/Image example](https://csb-4k0kr-p8ya8f304.vercel.app/), by disabling JavaScript in the browser's DevTools. Video demo: https://streamable.com/frkvw9

This PR aims to fix this behaviour by using `<noscript></noscript>` tags to conditionally display a standard `<img>` element using the `props` passed to `<Image />` when JavaScript is disabled.

For browser sessions where JavaScript is enabled, this will not cause an increase in network requests, so there should be no downside.

One area where this PR is a bit "hacky" is that it uses a negative `margin-top` to counteract `sizerStyle.paddingTop`. From what I can tell, `sizerStyle.paddingTop` is generated on the server side, where we can not know ahead of time whether JavaScript is enabled in the browser - hence why I've opted for this solution. 

Fixes #19223
Fixes #21214
parent 9cc6ba93
...@@ -436,6 +436,27 @@ export default function Image({ ...@@ -436,6 +436,27 @@ export default function Image({
) : null} ) : null}
</div> </div>
) : null} ) : null}
{!isVisible && (
<noscript>
<img
{...rest}
{...generateImgAttrs({
src,
unoptimized,
layout,
width: widthInt,
quality: qualityInt,
sizes,
loader,
})}
src={src}
decoding="async"
sizes={sizes}
style={{ ...imgStyle, visibility: 'inherit' }}
className={className}
/>
</noscript>
)}
<img <img
{...rest} {...rest}
{...imgAttributes} {...imgAttributes}
......
...@@ -144,9 +144,12 @@ function runTests(mode) { ...@@ -144,9 +144,12 @@ function runTests(mode) {
const $html = cheerio.load(html) const $html = cheerio.load(html)
const els = [].slice.apply($html('img')) const els = [].slice.apply($html('img'))
expect(els.length).toBe(1) expect(els.length).toBe(2)
const [noscriptEl, el] = els
expect(noscriptEl.attribs.src).toBeDefined()
expect(noscriptEl.attribs.srcset).toBeDefined()
const [el] = els
expect(el.attribs.src).toBeDefined() expect(el.attribs.src).toBeDefined()
expect(el.attribs.srcset).toBeUndefined() expect(el.attribs.srcset).toBeUndefined()
expect(el.attribs.srcSet).toBeUndefined() expect(el.attribs.srcSet).toBeUndefined()
......
...@@ -17,7 +17,7 @@ describe('Image rendering', () => { ...@@ -17,7 +17,7 @@ describe('Image rendering', () => {
const $ = cheerio.load(html) const $ = cheerio.load(html)
const img = $('#unit-image') const img = $('#unit-image')
expect(img.attr('id')).toBe('unit-image') expect(img.attr('id')).toBe('unit-image')
expect(img.attr('src')).toContain('/_next/image?url=%2Ftest.png') expect(img.attr('src')).toContain('test.png')
expect(img.attr('srcset')).toContain('/_next/image?url=%2Ftest.png') expect(img.attr('srcset')).toContain('test.png')
}) })
}) })
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