From 2af9cfb3cde2f251f17928264a0f5bd3159c9855 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 25 May 2021 18:06:34 +0100 Subject: [PATCH] Ensure that the host is only replaced once --- src/Optimizer/Transformer/RewriteAmpUrls.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Optimizer/Transformer/RewriteAmpUrls.php b/src/Optimizer/Transformer/RewriteAmpUrls.php index 2dfe2cb48..f7dc63be5 100644 --- a/src/Optimizer/Transformer/RewriteAmpUrls.php +++ b/src/Optimizer/Transformer/RewriteAmpUrls.php @@ -116,7 +116,7 @@ private function adaptForEsmSupport(Document $document, $host) $href = $node->getAttribute(Attribute::HREF); if ($node->tagName === Tag::SCRIPT && $this->usesAmpCacheUrl($src)) { $newUrl = $this->replaceUrl($src, $host); - $node->setAttribute(Attribute::SRC, $this->replaceUrl($src, $host)); + $node->setAttribute(Attribute::SRC, $newUrl); if ($usesEsm) { $this->addEsm($document, $node); } else { @@ -139,7 +139,7 @@ private function adaptForEsmSupport(Document $document, $host) && $this->usesAmpCacheUrl($href) ) { - if ($usesEsm && $this->shouldPreload($href)) { + if ($usesEsm && substr_compare($href, 'v0.js', -5) === 0) { // Only preload .mjs runtime in ESM mode. $node->parentNode->removeChild($node); } else { @@ -175,6 +175,12 @@ private function usesAmpCacheUrl($url) */ private function replaceUrl($url, $host) { + // Preloads for scripts/styles that were already created for the adapted host need to be skipped, + // otherwise we end up with the lts or rtv suffix being added twice. + if (strpos($url, $host) === 0) { + return $url; + } + return str_replace(Amp::CACHE_HOST, $host, $url); }