ampproject / ampproject/amp-toolbox-php
Performance issues on `inlineCss` Transformer
- Dominant language
- PHP
- Stars
- 74
- Forks
- 25
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 3
Description
I wanted to start a discussion to see if is viable/posible to refactor the inlineCss transformer to improve the overall performance.
I found this while debugging some issues we get from time to time:
```text
Amp render failure: Cannot inline the amp-runtime CSS in unspecified version into : AmpProject\Exception\FailedToGetFromRemoteUrl: Failed to fetch the contents from the URL 'https://cdn.ampproject.org/v0.css'
```
What happens is that every-time you call the `inlineCss` transformer, two external requests are made to fetch the current AMP `Runtime Version` and the CSS styles.
I'm wondering how often those two change? (Version and Css content), isn't that something that we could cache locally to having to make those two extra request each time?
See for reference https://github.com/ampproject/amp-toolbox-php/blob/main/src/Optimizer/Transformer/AmpRuntimeCss.php#L153
```php
private function inlineCss(Element $ampRuntimeStyle, $version)
{
// Use version passed in via params if available, otherwise fetch the current prod version.
if (! empty($version)) {
$v0CssUrl = RuntimeVersion::appendRuntimeVersion(Amp::CACHE_HOST, $version) . '/' . self::V0_CSS;
} else {
$v0CssUrl = self::V0_CSS_URL;
$options = [
RuntimeVersion::OPTION_CANARY => $this->configuration->get(AmpRuntimeCssConfiguration::CANARY)
];
$version = (new RuntimeVersion($this->remoteRequest))->currentVersion($options);
}
$ampRuntimeStyle->setAttribute(Attribute::I_AMPHTML_VERSION, $version);
$styles = $this->configuration->get(AmpRuntimeCssConfiguration::STYLES);
if (empty($styles)) {
$response = $this->remoteRequest->get($v0CssUrl);
$statusCode = $response->getStatusCode();
if ($statusCode < 200 || $statusCode >= 300) {
return;
}
$styles = $response->getBody();
}
$ampRuntimeStyle->textContent = $styles;
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.