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

Conversation

schlessera
Copy link
Collaborator

@schlessera schlessera commented May 3, 2021

This PR adds two transformers to the Optimizer:

  1. AmpRuntimePreloads

    This adds preloads for the AMP runtime scripts (WIP) and styles, if they are needed.

  2. GoogleFontsPreconnect

    This adds a preconnect for the Google Fonts domain if Google Fonts are being used, and adds a DNS prefetch as a fallback.

It also adds an abstraction for managing resource hints via the Dom\Document:
image

TODOS:

  • When should the AMP runtime script be preloaded (if at all)?
  • Should it be possible to use preconnect without the best-practice-advised dns-prefetch?
  • Should we add other resource hints right away, even if we don't need them, i.e. prefetch, prerender, 'dns-prefetch` on its own...?

@schlessera schlessera changed the title Add GoogleFontsPreconnect transformer Add resource hints transformers May 3, 2021
@schlessera schlessera added this to the 0.6.0 milestone May 3, 2021
src/Dom/ResourceHintManager.php Outdated Show resolved Hide resolved
src/Dom/ResourceHintManager.php Outdated Show resolved Hide resolved
Comment on lines +42 to +46
private function isAmpRuntimeScriptNeeded(Document $document)
{
// TODO: What condition should be used here... same as isAmpRuntimeCssNeeded()?
return false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is locked by #17, yes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm not sure yet what the best practices are here.

src/Optimizer/Transformer/GoogleFontsPreconnect.php Outdated Show resolved Hide resolved
src/Optimizer/Transformer/GoogleFontsPreconnect.php Outdated Show resolved Hide resolved
Comment on lines +27 to +29
if ($this->isAmpRuntimeScriptNeeded($document)) {
$document->links->addPreload($this->getAmpRuntimeScriptHost(), RequestDestination::SCRIPT);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently being handled by the RewriteAmpUrls transformer. Should the logic be moved here? At the very least it seems RewriteAmpUrls should make use of the LinkManager in its createPreload method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made RewriteAmpUrls reuse the link manager. I also extended the link manager so that it ensure no duplicate links are being produced. This currently uses both href and rel for the uniqueness key.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this isn't important right now, it is possible for the rel to be list of tokens and not just a single value. That could cause problems here.

@schlessera schlessera force-pushed the add/31-browser-hints-transformer branch from a2f6e51 to 6c91427 Compare May 25, 2021 17:27
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, other than a couple minor things that can be addressed now or later.

src/Optimizer/Transformer/RewriteAmpUrls.php Show resolved Hide resolved
Comment on lines +27 to +29
if ($this->isAmpRuntimeScriptNeeded($document)) {
$document->links->addPreload($this->getAmpRuntimeScriptHost(), RequestDestination::SCRIPT);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this isn't important right now, it is possible for the rel to be list of tokens and not just a single value. That could cause problems here.

Comment on lines +268 to +270
if (! $link instanceof Element) {
throw FailedToCreateLink::forLink($link);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this condition ever happen?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do something fishy with the document, you might end up with a DOMElement that is not an Element.

Also, PhpStan is happier with this restriction.

src/Dom/LinkManager.php Outdated Show resolved Hide resolved
@schlessera schlessera force-pushed the add/31-browser-hints-transformer branch from 1021344 to 5831c16 Compare June 7, 2021 00:14
@schlessera schlessera merged commit b31e538 into main Jun 10, 2021
@schlessera schlessera deleted the add/31-browser-hints-transformer branch June 10, 2021 06:56
@schlessera schlessera mentioned this pull request Jun 15, 2021
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants