From 5c15f113d7cba166af202aeb68fff7b01c258033 Mon Sep 17 00:00:00 2001 From: Sebastian Benz Date: Wed, 8 Jul 2020 11:05:17 +0200 Subject: [PATCH] Only inject runtime-host meta tag if host is given (#855) * Only include host in runtime-host meta tag * Print warning if ampUrlPrefix is not absolute * Don't inject runtime-host meta tag if ampUrlPrefix is not absolute. This will fallback to cdn.ampproject.org. --- .../optimizer/lib/transformers/RewriteAmpUrls.js | 12 ++++++------ .../hello-world/expected_output.paired.html | 2 +- .../expected_output.html | 1 - .../rewrites_hosts/expected_output.html | 1 - 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/optimizer/lib/transformers/RewriteAmpUrls.js b/packages/optimizer/lib/transformers/RewriteAmpUrls.js index fa1f631ff..c1f41dea2 100644 --- a/packages/optimizer/lib/transformers/RewriteAmpUrls.js +++ b/packages/optimizer/lib/transformers/RewriteAmpUrls.js @@ -57,6 +57,7 @@ const {calculateHost} = require('../RuntimeHostHelper'); class RewriteAmpUrls { constructor(config) { this.esmModulesEnabled = config.experimentEsm; + this.log = config.log; } transform(root, params) { const html = firstChildByTag(root, 'html'); @@ -109,13 +110,12 @@ class RewriteAmpUrls { // runtime-host and amp-geo-api meta tags should appear before the first script if (!this._usesAmpCacheUrl(host) && !params.lts) { - let versionlessHost; - if (this.isAbsoluteUrl_(params.ampUrlPrefix)) { - versionlessHost = calculateHost({ampUrlPrefix: params.ampUrlPrefix}); - } else { - versionlessHost = '.'; + try { + const url = new URL(host); + this._addMeta(head, 'runtime-host', url.origin); + } catch (e) { + this.log.warn('ampUrlPrefix must be an absolute URL'); } - this._addMeta(head, 'runtime-host', versionlessHost); } if (params.geoApiUrl && !params.lts) { this._addMeta(head, 'amp-geo-api', params.geoApiUrl); 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 e06165c4a..b4fca7e60 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 @@ -4,7 +4,7 @@ - + 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 88d0a5f1f..5593c89ec 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 @@ -1,7 +1,6 @@ - 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 56513e73d..7414a159d 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 @@ -1,7 +1,6 @@ -