Unverified Commit 7198cbe0 authored by Alex Castle's avatar Alex Castle Committed by GitHub

Add at attribute to image preload link (#16328)

Fixes a small error in the image preload post-processor. Adds a needed 'as' attribute to the link.
parent fe61948d
...@@ -179,7 +179,8 @@ class ImageOptimizerMiddleware implements PostProcessMiddleware { ...@@ -179,7 +179,8 @@ class ImageOptimizerMiddleware implements PostProcessMiddleware {
let imagePreloadTags = _data.preloads.images let imagePreloadTags = _data.preloads.images
.filter((imgHref) => !preloadTagAlreadyExists(markup, imgHref)) .filter((imgHref) => !preloadTagAlreadyExists(markup, imgHref))
.reduce( .reduce(
(acc, imgHref) => acc + `<link rel="preload" href="${imgHref}"/>`, (acc, imgHref) =>
acc + `<link rel="preload" href="${imgHref}" as="image"/>`,
'' ''
) )
return result.replace( return result.replace(
......
...@@ -30,31 +30,37 @@ function runTests() { ...@@ -30,31 +30,37 @@ function runTests() {
function checkImagesOnPage(path) { function checkImagesOnPage(path) {
it('should not preload tiny images', async () => { it('should not preload tiny images', async () => {
const html = await renderViaHTTP(appPort, path) const html = await renderViaHTTP(appPort, path)
expect(html).not.toContain('<link rel="preload" href="tiny-image.jpg"/>') expect(html).not.toContain(
'<link rel="preload" href="tiny-image.jpg" as="image"/>'
)
}) })
it('should not add a preload if one already exists', async () => { it('should not add a preload if one already exists', async () => {
let html = await renderViaHTTP(appPort, path) let html = await renderViaHTTP(appPort, path)
html = html.replace( html = html.replace(
'<link rel="preload" href="already-preloaded.jpg"/>', '<link rel="preload" href="already-preloaded.jpg" as="image"/>',
'' ''
) )
expect(html).not.toContain( expect(html).not.toContain(
'<link rel="preload" href="already-preloaded.jpg"/>' '<link rel="preload" href="already-preloaded.jpg" as="image"/>'
) )
}) })
it('should not preload hidden images', async () => { it('should not preload hidden images', async () => {
const html = await renderViaHTTP(appPort, path) const html = await renderViaHTTP(appPort, path)
expect(html).not.toContain( expect(html).not.toContain(
'<link rel="preload" href="hidden-image-1.jpg"/>' '<link rel="preload" href="hidden-image-1.jpg" as="image"/>'
) )
expect(html).not.toContain( expect(html).not.toContain(
'<link rel="preload" href="hidden-image-2.jpg"/>' '<link rel="preload" href="hidden-image-2.jpg" 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('<link rel="preload" href="main-image-1.jpg"/>') expect(html).toContain(
expect(html).not.toContain('<link rel="preload" href="main-image-2.jpg"/>') '<link rel="preload" href="main-image-1.jpg" as="image"/>'
)
expect(html).not.toContain(
'<link rel="preload" href="main-image-2.jpg" as="image"/>'
)
}) })
} }
......
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