Skip to content

Commit 500012f

Browse files
authored
Merge pull request #261 from ampproject/update/rewrite-amp-urls-preloading
2 parents 4bd2dd2 + 5040acf commit 500012f

16 files changed

+53
-32
lines changed

src/Optimizer/Transformer/RewriteAmpUrls.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private function addEsm(Document $document, Element $scriptNode)
194194
$scriptUrl = $scriptNode->getAttribute(Attribute::SRC);
195195
$esmScriptUrl = preg_replace('/\.js$/', '.mjs', $scriptUrl);
196196

197-
if ($this->shouldPreload($scriptUrl)) {
197+
if ($this->shouldPreload($scriptUrl, $document)) {
198198
$document->links->addModulePreload($esmScriptUrl, Tag::SCRIPT, Attribute::CROSSORIGIN_ANONYMOUS);
199199
}
200200

@@ -224,7 +224,7 @@ private function addEsm(Document $document, Element $scriptNode)
224224
*/
225225
private function addPreload(Document $document, $href, $type)
226226
{
227-
if (! $this->shouldPreload($href)) {
227+
if (! $this->shouldPreload($href, $document)) {
228228
return;
229229
}
230230

@@ -276,11 +276,16 @@ private function adaptForSelfHosting(Document $document, $host, $errors)
276276
/**
277277
* Check whether a given URL should be preloaded.
278278
*
279-
* @param string $url Url to check.
279+
* @param string $url Url to check.
280+
* @param Document $document Document on which to check.
280281
* @return bool Whether the provided URL should be preloaded.
281282
*/
282-
private function shouldPreload($url)
283+
private function shouldPreload($url, Document $document)
283284
{
285+
if ($document->documentElement->hasAttribute(Amp::NO_BOILERPLATE_ATTRIBUTE)) {
286+
return false;
287+
}
288+
284289
return substr_compare($url, 'v0.js', -5) === 0
285290
||
286291
substr_compare($url, 'v0.css', -6) === 0;

tests/Optimizer/Transformer/RewriteAmpUrlsTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testThrowsOnMutuallyExclusiveFlags()
4040
public function testOrderingOfScripts()
4141
{
4242
$document = Document::fromHtml(
43-
TestMarkup::DOCTYPE . '<html amp><head>' . TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT
43+
TestMarkup::DOCTYPE . '<html amp i-amphtml-no-boilerplate><head>' . TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT
4444
. TestMarkup::LINK_CANONICAL . TestMarkup::LINK_GOOGLE_FONT_PRECONNECT
4545
. TestMarkup::SCRIPT_AMPRUNTIME
4646
. '</head><body></body></html>'
@@ -53,8 +53,7 @@ public function testOrderingOfScripts()
5353

5454
$this->assertCount(0, $errors);
5555
$this->assertSimilarMarkup(
56-
TestMarkup::DOCTYPE . '<html amp><head>' . TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT
57-
. '<link as="script" crossorigin="anonymous" href="https://cdn.ampproject.org/v0.mjs" rel="modulepreload">'
56+
TestMarkup::DOCTYPE . '<html amp i-amphtml-no-boilerplate><head>' . TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT
5857
. TestMarkup::LINK_CANONICAL . TestMarkup::LINK_GOOGLE_FONT_PRECONNECT
5958
. '<script async crossorigin="anonymous" nomodule src="https://cdn.ampproject.org/v0.js"></script>'
6059
. '<script async crossorigin="anonymous" src="https://cdn.ampproject.org/v0.mjs" type="module"></script>'

tests/spec/end-to-end/body-only/expected_output.full.html

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/spec/end-to-end/body-only/expected_output.lts.html

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/spec/end-to-end/hello-world/expected_output.fast.html

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/spec/end-to-end/hello-world/expected_output.full.html

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/spec/end-to-end/hello-world/expected_output.lts.html

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/spec/end-to-end/hello-world/expected_output.paired.html

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/spec/end-to-end/markdown/expected_output.full.html

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/spec/end-to-end/markdown/expected_output.lts.html

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)