diff --git a/packages/optimizer/README.md b/packages/optimizer/README.md index 62778db9d..d497eafdd 100644 --- a/packages/optimizer/README.md +++ b/packages/optimizer/README.md @@ -149,6 +149,7 @@ Available options are: - [optimizeAmpBind](#optimizeampbind) - [optimizeHeroImages](#optimizeheroimages) - [preloadHeroImage](#preloadheroimage) +- [preloadEnabled](#preloadenabled) - [separateKeyframes](#separateKeyframes) - [verbose](#verbose) @@ -333,7 +334,11 @@ Hero images are optimized by server-side rendering the `img` element inside the #### `preloadHeroImage` -Deprecated, use [optimizeHeroImages](#optimizeheroimages) instead. +Deprecated, use [optimizeHeroImages](#optimizeheroimages) instead. + +#### `preloadEnabled` + +Whether the AMP runtime script (v0.js or v0.mjs) and style (v0.css, if applicable) should be preloaded. #### `separateKeyframes` diff --git a/packages/optimizer/lib/DomTransformer.js b/packages/optimizer/lib/DomTransformer.js index f9bad08aa..9d8faa8c0 100644 --- a/packages/optimizer/lib/DomTransformer.js +++ b/packages/optimizer/lib/DomTransformer.js @@ -129,6 +129,7 @@ const CONFIG_DEFAULT = { minify: true, optimizeAmpBind: true, optimizeHeroImages: true, + preloadEnabled: false, separateKeyframes: true, }; @@ -140,6 +141,7 @@ const CONFIG_BUILD = Object.assign({}, CONFIG_DEFAULT, { minify: true, optimizeAmpBind: true, optimizeHeroImages: true, + preloadEnabled: false, separateKeyframes: true, }); @@ -151,6 +153,7 @@ const CONFIG_RUNTIME = Object.assign({}, CONFIG_DEFAULT, { minify: false, optimizeAmpBind: true, optimizeHeroImages: true, + preloadEnabled: false, separateKeyframes: false, }); diff --git a/packages/optimizer/lib/transformers/RewriteAmpUrls.js b/packages/optimizer/lib/transformers/RewriteAmpUrls.js index 68559e6e1..4b866cbd3 100644 --- a/packages/optimizer/lib/transformers/RewriteAmpUrls.js +++ b/packages/optimizer/lib/transformers/RewriteAmpUrls.js @@ -60,6 +60,7 @@ const {calculateHost} = require('../RuntimeHostHelper'); class RewriteAmpUrls { constructor(config) { this.esmModulesEnabled = config.esmModulesEnabled !== false; + this.preloadEnabled = config.preloadEnabled !== false; this.log = config.log; } transform(root, params) { @@ -78,18 +79,23 @@ class RewriteAmpUrls { while (node) { if (node.tagName === 'script' && this._usesAmpCacheUrl(node.attribs.src)) { node.attribs.src = this._replaceUrl(node.attribs.src, host); - if (esm) { - preloads.push(this._addEsm(node)); - } else { - preloads.push(this._createPreload(node.attribs.src, 'script')); - } + if ( esm ) { + const preload = this._addEsm(node); + if (this.preloadEnabled && preload) { + preloads.push(preload); + } + } else if (this.preloadEnabled) { + preloads.push(this._createPreload(node.attribs.src, 'script')); + } } else if ( node.tagName === 'link' && node.attribs.rel === 'stylesheet' && this._usesAmpCacheUrl(node.attribs.href) ) { node.attribs.href = this._replaceUrl(node.attribs.href, host); - preloads.push(this._createPreload(node.attribs.href, 'style')); + if (this.preloadEnabled) { + preloads.push(this._createPreload(node.attribs.href, 'style')); + } } else if ( node.tagName === 'link' && node.attribs.rel === 'preload' && @@ -140,7 +146,7 @@ class RewriteAmpUrls { _addEsm(scriptNode) { let result = null; const esmScriptUrl = scriptNode.attribs.src.replace(/\.js$/, '.mjs'); - if (this._shouldPreload(scriptNode.attribs.src)) { + if (this.preloadEnabled && this._shouldPreload(scriptNode.attribs.src)) { const preload = createElement('link', { as: 'script', crossorigin: 'anonymous', diff --git a/packages/optimizer/spec/end-to-end/body-only/expected_output.full.html b/packages/optimizer/spec/end-to-end/body-only/expected_output.full.html index 04812ddc5..f8c4ca745 100644 --- a/packages/optimizer/spec/end-to-end/body-only/expected_output.full.html +++ b/packages/optimizer/spec/end-to-end/body-only/expected_output.full.html @@ -2,8 +2,7 @@ - - + diff --git a/packages/optimizer/spec/end-to-end/body-only/expected_output.lts.html b/packages/optimizer/spec/end-to-end/body-only/expected_output.lts.html index 80b96b4e5..07eda944f 100644 --- a/packages/optimizer/spec/end-to-end/body-only/expected_output.lts.html +++ b/packages/optimizer/spec/end-to-end/body-only/expected_output.lts.html @@ -2,8 +2,7 @@ - - + diff --git a/packages/optimizer/spec/end-to-end/hello-world/expected_output.full.html b/packages/optimizer/spec/end-to-end/hello-world/expected_output.full.html index d06e2fffd..b07bd37e1 100644 --- a/packages/optimizer/spec/end-to-end/hello-world/expected_output.full.html +++ b/packages/optimizer/spec/end-to-end/hello-world/expected_output.full.html @@ -2,9 +2,8 @@ - - - + +

Hello, AMP world!

diff --git a/packages/optimizer/spec/end-to-end/hello-world/expected_output.lts.html b/packages/optimizer/spec/end-to-end/hello-world/expected_output.lts.html index d5373b820..6760b8696 100644 --- a/packages/optimizer/spec/end-to-end/hello-world/expected_output.lts.html +++ b/packages/optimizer/spec/end-to-end/hello-world/expected_output.lts.html @@ -2,9 +2,8 @@ - - - + +

Hello, AMP world!

diff --git a/packages/optimizer/spec/end-to-end/hello-world/expected_output.paired.html b/packages/optimizer/spec/end-to-end/hello-world/expected_output.paired.html index dce606d40..102c1fc44 100644 --- a/packages/optimizer/spec/end-to-end/hello-world/expected_output.paired.html +++ b/packages/optimizer/spec/end-to-end/hello-world/expected_output.paired.html @@ -3,7 +3,6 @@ - diff --git a/packages/optimizer/spec/end-to-end/markdown/expected_output.full.html b/packages/optimizer/spec/end-to-end/markdown/expected_output.full.html index 9eca8e11e..8aaab8aa3 100644 --- a/packages/optimizer/spec/end-to-end/markdown/expected_output.full.html +++ b/packages/optimizer/spec/end-to-end/markdown/expected_output.full.html @@ -2,8 +2,7 @@ - - + diff --git a/packages/optimizer/spec/end-to-end/markdown/expected_output.lts.html b/packages/optimizer/spec/end-to-end/markdown/expected_output.lts.html index 1dfbfc8d4..8a1b8ced6 100644 --- a/packages/optimizer/spec/end-to-end/markdown/expected_output.lts.html +++ b/packages/optimizer/spec/end-to-end/markdown/expected_output.lts.html @@ -2,8 +2,7 @@ - - + diff --git a/packages/optimizer/spec/end-to-end/story/expected_output.full.html b/packages/optimizer/spec/end-to-end/story/expected_output.full.html index 5d2938b72..ac4f4da3e 100644 --- a/packages/optimizer/spec/end-to-end/story/expected_output.full.html +++ b/packages/optimizer/spec/end-to-end/story/expected_output.full.html @@ -2,8 +2,7 @@ - - + My Story + My Story + My Story + diff --git a/packages/optimizer/spec/end-to-end/trailing-template/expected_output.lts.html b/packages/optimizer/spec/end-to-end/trailing-template/expected_output.lts.html index 1d4ec459f..127b45bd3 100644 --- a/packages/optimizer/spec/end-to-end/trailing-template/expected_output.lts.html +++ b/packages/optimizer/spec/end-to-end/trailing-template/expected_output.lts.html @@ -2,8 +2,7 @@ - - + diff --git a/packages/optimizer/spec/end-to-end/trailing-template/expected_output.paired.html b/packages/optimizer/spec/end-to-end/trailing-template/expected_output.paired.html index d047faa11..0dd121109 100644 --- a/packages/optimizer/spec/end-to-end/trailing-template/expected_output.paired.html +++ b/packages/optimizer/spec/end-to-end/trailing-template/expected_output.paired.html @@ -2,8 +2,7 @@ - - + diff --git a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/adds_geoapi/expected_output.html b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/adds_geoapi/expected_output.html index 0eab7d9d0..74b368b14 100644 --- a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/adds_geoapi/expected_output.html +++ b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/adds_geoapi/expected_output.html @@ -6,8 +6,6 @@ - - \ No newline at end of file diff --git a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/adds_runtime_version/expected_output.html b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/adds_runtime_version/expected_output.html index efffeb726..f7b94acbb 100644 --- a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/adds_runtime_version/expected_output.html +++ b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/adds_runtime_version/expected_output.html @@ -6,8 +6,6 @@ - - \ No newline at end of file diff --git a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_adds_version/expected_output.html b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_adds_version/expected_output.html index 2a0eb8a27..711e7dfd3 100644 --- a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_adds_version/expected_output.html +++ b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_adds_version/expected_output.html @@ -6,8 +6,6 @@ - - \ No newline at end of file diff --git a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_hosts/expected_output.html b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_hosts/expected_output.html index 29709b7ae..6247eabce 100644 --- a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_hosts/expected_output.html +++ b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_hosts/expected_output.html @@ -7,8 +7,6 @@ - - \ No newline at end of file diff --git a/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_esm/expected_output.html b/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_esm/expected_output.html index 7d262c29c..0750c4e4c 100644 --- a/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_esm/expected_output.html +++ b/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_esm/expected_output.html @@ -7,8 +7,6 @@ - - \ No newline at end of file diff --git a/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_lts/expected_output.html b/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_lts/expected_output.html index aa570f4cf..c03ff4569 100644 --- a/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_lts/expected_output.html +++ b/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_lts/expected_output.html @@ -6,8 +6,6 @@ - - \ No newline at end of file diff --git a/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_preloads/input.html b/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_preloads/input.html index f3ab7ec0d..b38d57f03 100644 --- a/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_preloads/input.html +++ b/packages/optimizer/spec/transformers/valid/RewriteAmpUrls/adds_preloads/input.html @@ -1,3 +1,8 @@ +