Skip to content

Commit

Permalink
Ensure that the host is only replaced once
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed May 25, 2021
1 parent ce1a960 commit 2af9cfb
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Optimizer/Transformer/RewriteAmpUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 2af9cfb

Please sign in to comment.