From 74295b6caf7cfcac93d402468dc41301260fe479 Mon Sep 17 00:00:00 2001 From: Matt Mower Date: Sun, 19 Jan 2020 19:20:54 -0800 Subject: [PATCH] optimizer: Add meta tags for singleDoc self-host Add meta tags to AMP documents to support self-hosted AMP runtime support when AMP pages are viewed in single-doc mode. --- packages/optimizer/lib/transformers/RewriteAmpUrls.js | 11 +++++++++++ .../spec/end-to-end/hello-world/expected_output.html | 1 + .../expected_output.html | 1 + .../rewrites_hosts/expected_output.html | 1 + 4 files changed, 14 insertions(+) diff --git a/packages/optimizer/lib/transformers/RewriteAmpUrls.js b/packages/optimizer/lib/transformers/RewriteAmpUrls.js index 7782a143e..ba0e3dfbd 100644 --- a/packages/optimizer/lib/transformers/RewriteAmpUrls.js +++ b/packages/optimizer/lib/transformers/RewriteAmpUrls.js @@ -67,6 +67,11 @@ class RewriteAmpUrls { } node = node.nextSibling; } + + if (!this._usesAmpCacheUrl(host)) { + const versionlessHost = calculateHost({ampUrlPrefix: params.ampUrlPrefix}); + referenceNode = this._addMeta(head, referenceNode, 'runtime-host', versionlessHost); + } } _usesAmpCacheUrl(url) { @@ -93,6 +98,12 @@ class RewriteAmpUrls { insertAfter(parent, preload, node); return preload; } + + _addMeta(parent, node, name, content) { + const meta = createElement('meta', {name, content}); + insertAfter(parent, meta, node); + return meta; + } } module.exports = RewriteAmpUrls; diff --git a/packages/optimizer/spec/end-to-end/hello-world/expected_output.html b/packages/optimizer/spec/end-to-end/hello-world/expected_output.html index 62d1e50c0..55b0a3faf 100644 --- a/packages/optimizer/spec/end-to-end/hello-world/expected_output.html +++ b/packages/optimizer/spec/end-to-end/hello-world/expected_output.html @@ -4,6 +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 9edb65665..ca16813d5 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 @@ -8,6 +8,7 @@ + \ 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 855ea6d04..08628ad22 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 @@ -8,6 +8,7 @@ + \ No newline at end of file