From 9e7533cbb32b85cdc6116973e8c9ad198bd40cb8 Mon Sep 17 00:00:00 2001 From: Matt Mower Date: Wed, 9 Oct 2019 14:05:01 -0400 Subject: [PATCH] optimizer: Do not append rtv/{rtv}/ to ampUrlPrefix (#517) * optimizer: Do not append rtv/{rtv}/ to ampUrlPrefix BREAKING CHANGE When both ampUrlPrefix and ampRuntimeVersion are specified, do not append rtv/{rtv}/ to the prefix. If version information is expected in the URL, it can be included in ampUrlPrefix. * optimizer: Update tests for revised ampUrlPrefix handling * optimizer: Revise version in ampUrlPrefix notice --- packages/optimizer/README.md | 20 ++++++++++++------- .../lib/transformers/RewriteAmpUrls.js | 2 +- .../hello-world/expected_output.html | 10 +++++----- .../expected_output.html | 13 ------------ .../expected_output.html | 13 ++++++++++++ .../input.html | 0 6 files changed, 32 insertions(+), 26 deletions(-) delete mode 100644 packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_adds_version/expected_output.html create mode 100644 packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_excludes_version/expected_output.html rename packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/{rewrites_host_and_adds_version => rewrites_host_and_excludes_version}/input.html (100%) diff --git a/packages/optimizer/README.md b/packages/optimizer/README.md index 8528ddcc8..7ba4d530f 100644 --- a/packages/optimizer/README.md +++ b/packages/optimizer/README.md @@ -187,9 +187,13 @@ const optimizer = AmpOptimizer.create({ ### Self-hosted AMP components -It's possible to rewrite the AMP framework and component imports to a different domain than `cdn.ampproject.org`. +It's possible to rewrite the AMP framework and component imports to a different domain than `cdn.ampproject.org`. These options tailor URL rewriting performed by `ampOptimizer.transformHtml`: -Example: +- `ampUrlPrefix`: Replace `https://cdn.ampproject.org/` with another origin or relative path. + + **Notice:** The behavior of `ampUrlPrefix` when used in conjunction with `ampRuntimeVersion` changed beginning with version 1.1.2. Prior to 1.1.2, `rtv/{rtv}/` was automatically appended to `ampUrlPrefix` when `ampRuntimeVersion` was specified. Since version 1.1.2, `ampUrlPrefix` is not modified when `ampRuntimeVersion` is also specified. + +Examples: ``` const ampOptimizer = require('@ampproject/toolbox-optimizer'); @@ -200,12 +204,14 @@ const originalHtml = ` ... ` -// Additional options can be passed as the second argument -const optimizedHtml = await ampOptimizer.transformHtml(originalHtml, { - ampUrl: 'canonical.amp.html', - // this will rewrite https://cdn.ampproject.org/v0.js to /amp/v0.js +// this will rewrite https://cdn.ampproject.org/v0.js to /amp/v0.js +const optimizedHtmlA = await ampOptimizer.transformHtml(originalHtml, { ampUrlPrefix: '/amp' }); -console.log(optimizedHtml); +// this will rewrite https://cdn.ampproject.org/v0.js to /amp/v0.js +const optimizedHtmlB = await ampOptimizer.transformHtml(originalHtml, { + ampRuntimeVersion: '001515617716922', + ampUrlPrefix: '/amp' +}); ``` diff --git a/packages/optimizer/lib/transformers/RewriteAmpUrls.js b/packages/optimizer/lib/transformers/RewriteAmpUrls.js index 3067c3d33..d7005f2cb 100644 --- a/packages/optimizer/lib/transformers/RewriteAmpUrls.js +++ b/packages/optimizer/lib/transformers/RewriteAmpUrls.js @@ -49,7 +49,7 @@ class RewriteAmpUrls { if (!head) return; let ampUrlPrefix = params.ampUrlPrefix || AMP_CACHE_HOST; - if (params.ampRuntimeVersion) { + if (params.ampRuntimeVersion && !params.ampUrlPrefix) { ampUrlPrefix = appendRuntimeVersion(ampUrlPrefix, params.ampRuntimeVersion); } 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 c02dec466..6a0d3b4b0 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,11 +4,11 @@ - - - - - + + + + + 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 deleted file mode 100644 index b80ebc361..000000000 --- a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_adds_version/expected_output.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_excludes_version/expected_output.html b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_excludes_version/expected_output.html new file mode 100644 index 000000000..6b553b0c8 --- /dev/null +++ b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_excludes_version/expected_output.html @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_adds_version/input.html b/packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_excludes_version/input.html similarity index 100% rename from packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_adds_version/input.html rename to packages/optimizer/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_excludes_version/input.html