Unverified Commit b9bf6863 authored by Steven's avatar Steven Committed by GitHub

Disable image optimization for Data URLs (#18804)

This PR disables image optimization and lazy loading for [Data URLs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs), because the image data is already inlined.

Fixes #18372 
Fixes #18692
parent 80fd3d5d
...@@ -310,6 +310,12 @@ export default function Image({ ...@@ -310,6 +310,12 @@ export default function Image({
lazy = false lazy = false
} }
if (src && src.startsWith('data:')) {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
unoptimized = true
lazy = false
}
useEffect(() => { useEffect(() => {
const target = thisEl.current const target = thisEl.current
......
...@@ -56,7 +56,7 @@ async function getSrc(browser, id) { ...@@ -56,7 +56,7 @@ async function getSrc(browser, id) {
} }
function getRatio(width, height) { function getRatio(width, height) {
return Math.round((height / width) * 1000) return height / width
} }
function runTests(mode) { function runTests(mode) {
...@@ -172,7 +172,10 @@ function runTests(mode) { ...@@ -172,7 +172,10 @@ function runTests(mode) {
const newHeight = await getComputed(browser, id, 'height') const newHeight = await getComputed(browser, id, 'height')
expect(newWidth).toBeLessThan(width) expect(newWidth).toBeLessThan(width)
expect(newHeight).toBeLessThan(height) expect(newHeight).toBeLessThan(height)
expect(getRatio(newWidth, newHeight)).toBe(getRatio(width, height)) expect(getRatio(newWidth, newHeight)).toBeCloseTo(
getRatio(width, height),
1
)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
...@@ -208,7 +211,10 @@ function runTests(mode) { ...@@ -208,7 +211,10 @@ function runTests(mode) {
const newHeight = await getComputed(browser, id, 'height') const newHeight = await getComputed(browser, id, 'height')
expect(newWidth).toBeLessThan(width) expect(newWidth).toBeLessThan(width)
expect(newHeight).toBeLessThan(height) expect(newHeight).toBeLessThan(height)
expect(getRatio(newWidth, newHeight)).toBe(getRatio(width, height)) expect(getRatio(newWidth, newHeight)).toBeCloseTo(
getRatio(width, height),
1
)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
...@@ -244,7 +250,10 @@ function runTests(mode) { ...@@ -244,7 +250,10 @@ function runTests(mode) {
const newHeight = await getComputed(browser, id, 'height') const newHeight = await getComputed(browser, id, 'height')
expect(newWidth).toBe(width) expect(newWidth).toBe(width)
expect(newHeight).toBe(height) expect(newHeight).toBe(height)
expect(getRatio(newWidth, newHeight)).toBe(getRatio(width, height)) expect(getRatio(newWidth, newHeight)).toBeCloseTo(
getRatio(width, height),
1
)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
...@@ -356,10 +365,7 @@ function runTests(mode) { ...@@ -356,10 +365,7 @@ function runTests(mode) {
const computedWidth = await getComputed(browser, id, 'width') const computedWidth = await getComputed(browser, id, 'width')
const computedHeight = await getComputed(browser, id, 'height') const computedHeight = await getComputed(browser, id, 'height')
expect(getRatio(computedWidth, computedHeight) / 1000.0).toBeCloseTo( expect(getRatio(computedWidth, computedHeight)).toBeCloseTo(1.333, 1)
1.333,
1
)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
......
...@@ -38,6 +38,12 @@ const Page = () => { ...@@ -38,6 +38,12 @@ const Page = () => {
width={500} width={500}
height={500} height={500}
/> />
<Image
id="data-protocol"
src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="
width={100}
height={100}
/>
<p id="stubtext">This is valid usage of the Image component</p> <p id="stubtext">This is valid usage of the Image component</p>
</div> </div>
) )
......
...@@ -44,9 +44,7 @@ describe('TypeScript Image Component', () => { ...@@ -44,9 +44,7 @@ describe('TypeScript Image Component', () => {
it('should render the valid Image usage and not print error', async () => { it('should render the valid Image usage and not print error', async () => {
const html = await renderViaHTTP(appPort, '/valid', {}) const html = await renderViaHTTP(appPort, '/valid', {})
expect(html).toMatch(/This is valid usage of the Image component/) expect(html).toMatch(/This is valid usage of the Image component/)
expect(output).not.toMatch( expect(output).not.toMatch(/Error/)
/must use "width" and "height" properties or "layout='fill'" property/
)
}) })
it('should print error when invalid Image usage', async () => { it('should print error when invalid Image usage', 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