Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource hints transformers #179

Merged
merged 37 commits into from
Jun 10, 2021
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6fa218e
Add initial scaffolding for link management
schlessera Nov 25, 2020
82eeaf4
Ignore HTML special char entity encoding mismatches in assertSimilarM…
schlessera Nov 29, 2020
b734f1e
Add tests for Google Fonts preconnect
schlessera Nov 29, 2020
b835bad
Add logic to detect usage of Google Fonts
schlessera Nov 29, 2020
4ef17b8
Change LinkManager link placement
schlessera Nov 29, 2020
3bfa743
Improve preconnect generation according to best practices
schlessera Nov 29, 2020
e4e611d
Use resource hint terminology
schlessera Nov 29, 2020
2152d8c
Add ResourceHintManager::addPreload() helper
schlessera Nov 30, 2020
6a4c48b
Add scaffolding for adding AMP runtime script and style preloads
schlessera Nov 30, 2020
51611f0
Rename ResourceHints into GooglePreconnects to split up transformers
schlessera Nov 30, 2020
5772a5c
Harden resource hint manager reference node logic
schlessera Nov 30, 2020
5280f04
Add AmpRuntimePreloads transformer
schlessera Nov 30, 2020
dda1127
Fix line length CS issue in Dom\Document
schlessera Nov 30, 2020
c6191d1
Rename GoogleFontsPreconnectTest
schlessera Dec 3, 2020
40a50c4
Add TODO for AMP runtime is needed condition
schlessera Dec 14, 2020
e3fc16c
Add tests for AMP runtime CSS preload
schlessera Dec 14, 2020
b0f6d93
Add GoogleFontsPreconnect spec tests
schlessera Dec 15, 2020
dfcbd14
Fix test failures
schlessera May 3, 2021
e1d4687
Rename property $linkManager -> $resourceHints
schlessera May 3, 2021
2c1588c
Add tests for resource hint manager
schlessera May 3, 2021
08285b0
Include AmpRuntimePreloads in list of default transformers
schlessera May 3, 2021
26cd737
Add covers annotations for AmpRuntimePreloadsTest
schlessera May 3, 2021
34b9189
Make URL check for Google API domain stricter
schlessera May 3, 2021
1cb6796
Rename ResourceHintManager back to LinkManager
schlessera May 11, 2021
5a5dd19
Rephrase crossorigin argument
schlessera May 11, 2021
6c9aae1
Improve docblocks
schlessera May 11, 2021
ddf7ee4
Add missing resource hints
schlessera May 11, 2021
50956fe
Adapt tests
schlessera May 11, 2021
657f2ab
Rename Google Fonts constants
schlessera May 11, 2021
0890944
Use renamed constants
schlessera May 11, 2021
300a078
Add modulepreload
schlessera May 11, 2021
f8170a3
Add replacement and removal to link manager
schlessera May 25, 2021
4cd2c0d
Make RewriteAmpUrls use link manager
schlessera May 25, 2021
3b6d5a9
Ensure that the host is only replaced once
schlessera May 25, 2021
52c903d
Fix PHPStan issue
schlessera May 25, 2021
5831c16
Fix missing import
schlessera Jun 7, 2021
c3a2159
Remove unused variables
schlessera Jun 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename ResourceHints into GooglePreconnects to split up transformers
schlessera committed Jun 7, 2021
commit 51611f07ca838b0a81b933774720e597ba2b15b0
1 change: 1 addition & 0 deletions src/Optimizer/Configuration.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
use AmpProject\Optimizer\Transformer\AmpRuntimeCss;
use AmpProject\Optimizer\Transformer\ResourceHints;
use AmpProject\Optimizer\Transformer\OptimizeAmpBind;
use AmpProject\Optimizer\Transformer\GoogleFontsPreconnect;
use AmpProject\Optimizer\Transformer\PreloadHeroImage;
use AmpProject\Optimizer\Transformer\ReorderHead;
use AmpProject\Optimizer\Transformer\RewriteAmpUrls;
Original file line number Diff line number Diff line change
@@ -2,21 +2,16 @@

namespace AmpProject\Optimizer\Transformer;

use AmpProject\Amp;
use AmpProject\Dom\Document;
use AmpProject\Optimizer\ErrorCollection;
use AmpProject\Optimizer\Transformer;
use AmpProject\RequestDestination;

/**
* Transformer adding meaningful resource hints to the document.
*
* This transformer adds the following resource hints:
* 1. Google Fonts preconnect hint.
* Transformer adding resource hints to preconnect to the Google Fonts domain.
*
* @package ampproject/amp-toolbox
*/
final class ResourceHints implements Transformer
final class GoogleFontsPreconnect implements Transformer
{

/**
@@ -49,14 +44,6 @@ final class ResourceHints implements Transformer
*/
public function transform(Document $document, ErrorCollection $errors)
{
if($this->isAmpRuntimeScriptNeeded()) {
$document->resourceHints->addPreload($this->getAmpRuntimeScriptHost(), RequestDestination::SCRIPT);
}

if($this->isAmpRuntimeCssNeeded()) {
$document->resourceHints->addPreload($this->getAmpRuntimeCssHost(), RequestDestination::STYLE);
}

if ($this->usesGoogleFonts($document)) {
$document->resourceHints->addPreconnect(self::GOOGLE_FONTS_STATIC_DOMAIN);
}
@@ -77,44 +64,4 @@ private function usesGoogleFonts(Document $document)

return $links->length > 0;
}

/**
* Check whether the AMP runtime script is needed.
*
* @return bool Whether the AMP runtime script is needed.
*/
private function isAmpRuntimeScriptNeeded()
{
return true;
}

/**
* Get the host domain of the AMP runtime script to preload.
*
* @return string AMP runtime script host.
*/
private function getAmpRuntimeScriptHost()
{
return Amp::CACHE_HOST;
}

/**
* Check whether the AMP runtime CSS is needed.
*
* @return bool Whether the AMP runtime CSS is needed.
*/
private function isAmpRuntimeCssNeeded()
{
return true;
}

/**
* Get the host domain of the AMP runtime CSS to preload.
*
* @return string AMP runtime CSS host.
*/
private function getAmpRuntimeCssHost()
{
return Amp::CACHE_HOST;
}
}
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
*
* @package ampproject/amp-toolbox
*/
final class ResourceHintsTest extends TestCase
final class GooglePreconnectTest extends TestCase
{
use ErrorComparison;
use MarkupComparison;
@@ -65,7 +65,7 @@ public function dataTransform()
/**
* Test the transform() method.
*
* @covers \AmpProject\Optimizer\Transformer\ResourceHints::transform()
* @covers \AmpProject\Optimizer\Transformer\GoogleFontsPreconnect::transform()
* @dataProvider dataTransform()
*
* @param string $source String of source HTML.
@@ -76,7 +76,7 @@ public function dataTransform()
public function testTransform($source, $expectedHtml, $expectedErrors = [], $config = [])
{
$document = Document::fromHtml($source);
$transformer = new ResourceHints();
$transformer = new GoogleFontsPreconnect();
$errors = new ErrorCollection();

$transformer->transform($document, $errors);