diff --git a/src/html/add-headers.js b/src/html/add-headers.js index 968f6a4d9..f0f32712c 100644 --- a/src/html/add-headers.js +++ b/src/html/add-headers.js @@ -12,20 +12,27 @@ function addHeaders({ response: { headers, document } }) { const linkHeader = []; - document.querySelectorAll('link').forEach(({ href, rel }) => { + document.querySelectorAll('link').forEach(({ href, rel, as }) => { if (href && rel && !href.match(/; rel="${rel}"`); + if (as && rel.indexOf('preload' > -1)) { + // https://www.w3.org/TR/preload/#as-attribute + linkHeader.push(`<${href}>; rel="${rel}"; as="${as}"`); + } else { + linkHeader.push(`<${href}>; rel="${rel}"`); + } } }); - if (linkHeader.length > 0) { - headers.Link = linkHeader.join(); - } - document.querySelectorAll('meta').forEach(({ httpEquiv, content }) => { - if (httpEquiv && content && !headers[httpEquiv]) { + if (httpEquiv === 'Link') { + linkHeader.push(content); + } else if (httpEquiv && content && !headers[httpEquiv]) { headers[httpEquiv] = content; } }); + + if (linkHeader.length > 0) { + headers.Link = linkHeader.join(); + } } module.exports = addHeaders; diff --git a/test/testHTMLProduction.js b/test/testHTMLProduction.js index 54ca5b1e3..818afb4dc 100644 --- a/test/testHTMLProduction.js +++ b/test/testHTMLProduction.js @@ -80,7 +80,7 @@ describe('Testing HTML Pipeline in Production', () => { }); - it('html.pipe adds headers from meta and link tags', async () => { + it('html.pipe adds headers from meta tags', async () => { const result = await pipe( (context) => { context.response = { @@ -94,10 +94,7 @@ describe('Testing HTML Pipeline in Production', () => { - - - - +
${context.content.document.body.innerHTML} @@ -124,9 +121,84 @@ describe('Testing HTML Pipeline in Production', () => { assert.equal(result.response.headers.Expires, '3000', 'allows setting through meta http-equiv'); assert.equal(result.response.headers.Exceeds, undefined, 'ignores invalid meta tags'); assert.equal(result.response.headers.Foo, 'bar', 'does not override existing headers'); + assert.equal(result.response.headers.Link, '; rel=preload; as=image', 'allows setting Link header through meta http-equiv'); + }); + + it('html.pipe adds headers from link tags', async () => { + const result = await pipe( + (context) => { + context.response = { + status: 201, + headers: { + Foo: 'bar', + }, + body: ` + +