未验证 提交 6874adbb 编写于 作者: A Alex Castle 提交者: 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.
上级 aa568a54
......@@ -191,8 +191,10 @@ class ImageOptimizerMiddleware implements PostProcessMiddleware {
}
function isImgEligible(imgElement: HTMLElement): boolean {
let imgSrc = imgElement.getAttribute('src')
return (
imgElement.hasAttribute('src') &&
!!imgSrc &&
sourceIsSupportedType(imgSrc) &&
imageIsNotTooSmall(imgElement) &&
imageIsNotHidden(imgElement)
)
......@@ -243,6 +245,11 @@ function imageIsNotHidden(imgElement: HTMLElement): boolean {
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
registerPostProcessor(
'Inline-Fonts',
......
......@@ -6,6 +6,7 @@ const Page = () => {
<link rel="preload" href="already-preloaded.jpg" />
<img src="already-preloaded.jpg" />
<img src="tiny-image.jpg" width="20" height="20" />
<img src="vector-image.svg" />
<img src="hidden-image-1.jpg" hidden />
<div hidden>
<img src="hidden-image-2.jpg" />
......
......@@ -6,6 +6,7 @@ function Home({ stars }) {
<link rel="preload" href="already-preloaded.jpg" />
<img src="already-preloaded.jpg" />
<img src="tiny-image.jpg" width="20" height="20" />
<img src="vector-image.svg" />
<img src="hidden-image-1.jpg" hidden />
<div hidden>
<img src="hidden-image-2.jpg" />
......
......@@ -53,6 +53,12 @@ function checkImagesOnPage(path) {
'<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 () => {
const html = await renderViaHTTP(appPort, path)
expect(html).toContain(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册