-
Notifications
You must be signed in to change notification settings - Fork 25
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
Conversation
private function isAmpRuntimeScriptNeeded(Document $document) | ||
{ | ||
// TODO: What condition should be used here... same as isAmpRuntimeCssNeeded()? | ||
return false; | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
if ($this->isAmpRuntimeScriptNeeded($document)) { | ||
$document->links->addPreload($this->getAmpRuntimeScriptHost(), RequestDestination::SCRIPT); | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
a2f6e51
to
6c91427
Compare
There was a problem hiding this 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.
if ($this->isAmpRuntimeScriptNeeded($document)) { | ||
$document->links->addPreload($this->getAmpRuntimeScriptHost(), RequestDestination::SCRIPT); | ||
} |
There was a problem hiding this comment.
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.
if (! $link instanceof Element) { | ||
throw FailedToCreateLink::forLink($link); | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Co-authored-by: Weston Ruter <westonruter@google.com>
1021344
to
5831c16
Compare
This PR adds two transformers to the Optimizer:
AmpRuntimePreloads
This adds preloads for the AMP runtime scripts (WIP) and styles, if they are needed.
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
:TODOS:
preconnect
without the best-practice-adviseddns-prefetch
?prefetch
,prerender
, 'dns-prefetch` on its own...?