summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2018-07-21 17:20:56 +0200
committeremkael <emkael@tlen.pl>2018-07-21 17:20:56 +0200
commitedbccc4468427db250de2efedd1b7c1af0d79256 (patch)
tree92cab487b40949d47f3906ab786042a870dd19d7
parentb08745d88d9232cf2c8d436c37ca556332769bd6 (diff)
HTTP2 Push headers support for JS/CSS assets
-rw-r--r--app/frontend/web/ClientScriptManager.php20
1 files changed, 20 insertions, 0 deletions
diff --git a/app/frontend/web/ClientScriptManager.php b/app/frontend/web/ClientScriptManager.php
index fb8c9fe..a26e0d6 100644
--- a/app/frontend/web/ClientScriptManager.php
+++ b/app/frontend/web/ClientScriptManager.php
@@ -192,6 +192,7 @@ class ClientScriptManager extends TClientScriptManager {
private function _renderLocalScriptFiles(THtmlWriter $writer, array $localFiles) {
if ($localFiles) {
$assetPath = $this->_compileScriptFiles($localFiles);
+ $this->_setHTTP2PushHeader($assetPath);
$writer->write(TJavascript::renderScriptFile($assetPath));
}
}
@@ -200,6 +201,7 @@ class ClientScriptManager extends TClientScriptManager {
private function _renderExternalScriptFiles(THtmlWriter $writer, array $externalFiles) {
if ($externalFiles) {
foreach ($externalFiles as $file) {
+ $this->_setHTTP2PushHeader($file);
$this->markScriptFileAsRendered($file);
$this->_appendRenderedScripts([$file], $file);
}
@@ -237,6 +239,9 @@ class ClientScriptManager extends TClientScriptManager {
$this->_renderExternalScriptFiles($writer, $externalFiles);
}
} else {
+ foreach ($files as $file) {
+ $this->_setHTTP2PushHeader($file);
+ }
parent::renderScriptFiles($writer, $files);
}
}
@@ -425,6 +430,7 @@ class ClientScriptManager extends TClientScriptManager {
}
foreach ($fileTypes as $type => $files) {
$assetPath = $this->_compileSheetFiles($files);
+ $this->_setHTTP2PushHeader($assetPath);
$this->_renderSheetFileTag($writer, $assetPath, $type);
}
}
@@ -435,6 +441,7 @@ class ClientScriptManager extends TClientScriptManager {
if ($externalFiles) {
foreach ($externalFiles as $file) {
$this->_appendRenderedSheets([$file], $file[0]);
+ $this->_setHTTP2PushHeader($file[0]);
$this->_renderSheetFileTag($writer, $file[0], $file[1] ?: 'all');
}
}
@@ -474,6 +481,9 @@ class ClientScriptManager extends TClientScriptManager {
$this->_renderExternalSheetFiles($writer, $externalFiles);
}
} else {
+ foreach ($this->getStyleSheetUrls() as $file) {
+ $this->_setHTTP2PushHeader($file);
+ }
parent::renderStyleSheetFiles($writer);
}
}
@@ -567,6 +577,16 @@ class ClientScriptManager extends TClientScriptManager {
return $this->registerStyleSheetFile($key, $file, $media ?: 'all');
}
+ private $_pushHeaders = [];
+
+ // Set Link: header for HTTP2 Push
+ private function _setHTTP2PushHeader($file) {
+ if (!in_array($file, $this->_pushHeaders)) {
+ $this->_pushHeaders[] = $file;
+ $this->getResponse()->appendHeader('Link: "<' . $file . '>; rel=preload"', FALSE);
+ }
+ }
+
}
?>