Unverified Commit 6874adbb authored by Alex Castle's avatar Alex Castle Committed by GitHub

Make the image post-processor ignore SVG images (#16732)

This is a small change to the image post-processor logic. When it's looking for images to preload, it will now ignore SVGs, as these are rarely the relevant images for LCP.
parent aa568a54
...@@ -191,8 +191,10 @@ class ImageOptimizerMiddleware implements PostProcessMiddleware { ...@@ -191,8 +191,10 @@ class ImageOptimizerMiddleware implements PostProcessMiddleware {
} }
function isImgEligible(imgElement: HTMLElement): boolean { function isImgEligible(imgElement: HTMLElement): boolean {
let imgSrc = imgElement.getAttribute('src')
return ( return (
imgElement.hasAttribute('src') && !!imgSrc &&
sourceIsSupportedType(imgSrc) &&
imageIsNotTooSmall(imgElement) && imageIsNotTooSmall(imgElement) &&
imageIsNotHidden(imgElement) imageIsNotHidden(imgElement)
) )
...@@ -243,6 +245,11 @@ function imageIsNotHidden(imgElement: HTMLElement): boolean { ...@@ -243,6 +245,11 @@ function imageIsNotHidden(imgElement: HTMLElement): boolean {
return true return true
} }
// Currently only filters out svg images--could be made more specific in the future.
function sourceIsSupportedType(imgSrc: string): boolean {
return !imgSrc.includes('.svg')
}
// Initialization // Initialization
registerPostProcessor( registerPostProcessor(
'Inline-Fonts', 'Inline-Fonts',
......
...@@ -6,6 +6,7 @@ const Page = () => { ...@@ -6,6 +6,7 @@ const Page = () => {
<link rel="preload" href="already-preloaded.jpg" /> <link rel="preload" href="already-preloaded.jpg" />
<img src="already-preloaded.jpg" /> <img src="already-preloaded.jpg" />
<img src="tiny-image.jpg" width="20" height="20" /> <img src="tiny-image.jpg" width="20" height="20" />
<img src="vector-image.svg" />
<img src="hidden-image-1.jpg" hidden /> <img src="hidden-image-1.jpg" hidden />
<div hidden> <div hidden>
<img src="hidden-image-2.jpg" /> <img src="hidden-image-2.jpg" />
......
...@@ -6,6 +6,7 @@ function Home({ stars }) { ...@@ -6,6 +6,7 @@ function Home({ stars }) {
<link rel="preload" href="already-preloaded.jpg" /> <link rel="preload" href="already-preloaded.jpg" />
<img src="already-preloaded.jpg" /> <img src="already-preloaded.jpg" />
<img src="tiny-image.jpg" width="20" height="20" /> <img src="tiny-image.jpg" width="20" height="20" />
<img src="vector-image.svg" />
<img src="hidden-image-1.jpg" hidden /> <img src="hidden-image-1.jpg" hidden />
<div hidden> <div hidden>
<img src="hidden-image-2.jpg" /> <img src="hidden-image-2.jpg" />
......
...@@ -53,6 +53,12 @@ function checkImagesOnPage(path) { ...@@ -53,6 +53,12 @@ function checkImagesOnPage(path) {
'<link rel="preload" href="hidden-image-2.jpg" as="image"/>' '<link rel="preload" href="hidden-image-2.jpg" as="image"/>'
) )
}) })
it('should not preload SVG images', async () => {
const html = await renderViaHTTP(appPort, path)
expect(html).not.toContain(
'<link rel="preload" href="vector-image.svg" as="image"/>'
)
})
it('should preload exactly two eligible images', async () => { it('should preload exactly two eligible images', async () => {
const html = await renderViaHTTP(appPort, path) const html = await renderViaHTTP(appPort, path)
expect(html).toContain( expect(html).toContain(
......
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