-
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 RewriteAmpUrls transformer #119
Conversation
tests/spec/transformers/valid/RewriteAmpUrls/adds_esm/input.html
Outdated
Show resolved
Hide resolved
return false; | ||
} | ||
|
||
return strpos($url, Amp::CACHE_HOST) === 0; |
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.
Minor thing, but in shouldPreload
it's using substr_compare()
. Would that not be minimally better than using strpos()
here?
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.
Done in e0dd638
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
…com/ampproject/amp-toolbox-php into add/20-rewrite-amp-urls-transformer
* @param string $name Name of the part to return. | ||
* @return string|null Part string or null if it was not found during parsing. | ||
*/ | ||
public function __get($name) |
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.
Eventually we should implement __set()
as well, since it would be very useful to be able to change out just the host
, when rewriting a URL to use the AMP Cache for example.
Either this, or make the member variables public
.
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'd recommend against that. This is supposed to behave like a value object, so it should be immutable to avoid bugs.
It could have a method to retrieve a new clone with a different host, though:
$cachedUrl = $url->withHost($cachedHost);
The important part is that the original object is not changed (as that would changed it in all the places it was passed around), but rather we return a new instance with the adapted host.
@@ -270,7 +270,7 @@ private function detectImageWithAttribute(Element $element, $attribute) | |||
} | |||
|
|||
$src = $element->getAttribute(Attribute::SRC); | |||
if ($element->tagName === Extension::IMG && Url::isValidNonDataUrl($src)) { | |||
if ($element->tagName === Extension::IMG && (new Url($src))->isValidNonDataUrl()) { |
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.
Now that Url
can throw an exception, should this method indicate that it @throws FailedToParseUrl Exception when the URL or Base URL is malformed.
? Should the exception get added to $errors
?
if (!empty($url->scheme) && !empty($url->host)) { | ||
$origin = "{$url->scheme}://{$url->host}"; | ||
$this->addMeta($document, 'runtime-host', $origin); |
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.
What about scheme-relative URLs? In that case, $url->scheme
would be empty and the $origin
would be "//{$url->host}"
. I guess that's not supported, and we'd want an explicit HTTPS URL anyway.
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.
That would fail the first check (!empty($url->scheme)
) and generate an error. Looks correct to me.
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 mean, if such URLs should be supported.
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.
Not sure what the requirements of the runtime are here. @sebastianbenz ?
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.
Weston is right, we want an explicit https URL.
/** | ||
* Query string. | ||
* | ||
* @var string|null | ||
*/ | ||
private $query; |
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.
Eventually it will be nice to add a way to access/manipulate the query vars as an array.
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.
That would be powered by a separate object, so that you'd be able to use it as part of a URL, or stand-alone when dealing with WP query vars, for example.
private function calculateHost() | ||
{ | ||
$lts = $this->configuration->get(RewriteAmpUrlsConfiguration::LTS); | ||
$rtv = $this->configuration->get(RewriteAmpUrlsConfiguration::RTV); | ||
|
||
if ($lts && $rtv) { | ||
throw InvalidConfiguration::forMutuallyExclusiveFlags( | ||
RewriteAmpUrlsConfiguration::LTS, | ||
RewriteAmpUrlsConfiguration::RTV | ||
); | ||
} | ||
|
||
$ampUrlPrefix = $this->configuration->get(RewriteAmpUrlsConfiguration::AMP_URL_PREFIX); | ||
$ampRuntimeVersion = $this->configuration->get(RewriteAmpUrlsConfiguration::AMP_RUNTIME_VERSION); | ||
|
||
$ampUrlPrefix = rtrim($ampUrlPrefix, '/'); | ||
|
||
if ($ampRuntimeVersion && $rtv) { | ||
$ampUrlPrefix = RuntimeVersion::appendRuntimeVersion($ampUrlPrefix, $ampRuntimeVersion); | ||
} elseif ($lts) { | ||
$ampUrlPrefix .= '/lts'; | ||
} | ||
|
||
return $ampUrlPrefix; | ||
} |
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 just realized that the rtv
and ampRuntimeVersion
are both empty. Instead of seeing the AMP runtime loaded with https://cdn.ampproject.org/rtv/012103261048002/v0.mjs it comes out rtv-less: https://cdn.ampproject.org/v0.mjs
However, if I hack this to force $rtv
to be true
and $ampRuntimeVersion
to be 012103261048002
, then the result is invalid AMP:
The attribute 'src' in tag 'amphtml module engine script' is set to the invalid value 'https://cdn.ampproject.org/rtv/012103261048002/v0.mjs'
I guess versioned URLs are not yet valid AMP?
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.
Only lts
is valid. RTV-specific scripts are not yet valid: https://github.com/ampproject/amphtml/blob/52407d1c72c5e2051fda427270b9503ae9900ebf/validator/validator-main.protoascii#L3109-L3256
I think this is being tracked by ampproject/amphtml#27546, although this is about self-hosting and not allowing versioned script URLs. @sebastianbenz is that right?
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.
Correct, there are no plans to support RTV versions served from cdn.ampproject.org.
This reverts commit e0dd638.
…into add/20-rewrite-amp-urls-transformer * 'main' of https://github.com/ampproject/amp-toolbox-php: Sync spec test suite - 2021-04-09 Sync local fallback files - 2021-04-09 Only construct preload if it is needed Sync local fallback files - 2021-04-08 Replace phpcs exclude-pattern with explicit file includes Delete .phpunit.result.cache Sync local fallback files - 2021-04-07 moving the curl_errno() call up, otherwise won't be reached if the response body is empty Calling curl_errno() before closing the conextion trhows an error Sync local fallback files - 2021-04-04 Sync local fallback files - 2021-04-03 Fix SSR for nested AMP components Sync spec test suite - 2021-04-02 Sync local fallback files - 2021-04-02 Sync local fallback files - 2021-04-01
This PR adds the
RewriteAmpUrls
transformer that enables the following features:geo-api
URLThis PR also moves some code from
AMP_DOM_Utils
into the library to enable the library to usecopyAttributes()
.This PR also includes changes to the
ReorderHead
transformer to make it work properly with module/nomodule script combinations.Fixes #20