diff --git a/packages/optimizer/lib/HtmlDomHelper.js b/packages/optimizer/lib/HtmlDomHelper.js index f14f84c97..e2b2bfdb6 100644 --- a/packages/optimizer/lib/HtmlDomHelper.js +++ b/packages/optimizer/lib/HtmlDomHelper.js @@ -39,7 +39,11 @@ function findMetaViewport(head) { */ function findRuntimeScript(head) { for (let node = head.firstChild; node !== null; node = node.nextSibling) { - if (node.tagName === 'script' && node.attribs.src.match(/^https:\/\/.+\/v0(\.js|\.mjs)$/)) { + if ( + node.tagName === 'script' && + node.attribs.src && + node.attribs.src.match(/^https:\/\/.+\/v0(\.js|\.mjs)$/) + ) { return node; } } diff --git a/packages/optimizer/spec/lib/HtmlDomHelper.test.js b/packages/optimizer/spec/lib/HtmlDomHelper.test.js index 7a83494ef..196099501 100644 --- a/packages/optimizer/spec/lib/HtmlDomHelper.test.js +++ b/packages/optimizer/spec/lib/HtmlDomHelper.test.js @@ -14,7 +14,7 @@ * limitations under the License. */ -const {findMetaViewport} = require('../../lib/HtmlDomHelper'); +const {findMetaViewport, findRuntimeScript} = require('../../lib/HtmlDomHelper'); const treeParser = require('../../lib/TreeParser'); const {firstChildByTag} = require('../../lib/NodeUtils'); @@ -54,3 +54,37 @@ test('findMetaViewport returns the correct tag', async () => { const result = findMetaViewport(head); expect(result).toEqual(head.children[3]); }); + +test('findRuntimeScript returns runtime v0.mjs', async () => { + const root = await treeParser.parse(`
+ + + `); + const html = firstChildByTag(root, 'html'); + const head = firstChildByTag(html, 'head'); + const result = findRuntimeScript(head); + expect(result).toEqual(head.children[3]); +}); + +test('findRuntimeScript returns runtime v0.js', async () => { + const root = await treeParser.parse(` + + + `); + const html = firstChildByTag(root, 'html'); + const head = firstChildByTag(html, 'head'); + const result = findRuntimeScript(head); + expect(result).toEqual(head.children[3]); +}); + +test('findRuntimeScript ignores empty src', async () => { + const root = await treeParser.parse(` + + + +`); + const html = firstChildByTag(root, 'html'); + const head = firstChildByTag(html, 'head'); + const result = findRuntimeScript(head); + expect(result).toEqual(head.children[5]); +});