-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Precompression for (pipelined) assets (nginx::gzip_static support). #1621
Open
Eihrister
wants to merge
1
commit into
getgrav:develop
Choose a base branch
from
Eihrister:feature/asset-precompression
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,10 @@ class Assets | |
protected $css_no_pipeline = []; | ||
protected $js_no_pipeline = []; | ||
|
||
// Default values for asset precompression. | ||
protected $precompress = false; | ||
protected $precompress_level = 9; | ||
|
||
/** | ||
* Assets constructor. | ||
* | ||
|
@@ -170,6 +174,15 @@ public function config(array $config) | |
$this->js_minify = $config['js_minify']; | ||
} | ||
|
||
// Asset precompression | ||
if (isset($config['precompress'])) { | ||
$this->precompress = $config['precompress']; | ||
} | ||
|
||
if (isset($config['precompress_level'])) { | ||
$this->precompress_level = $config['precompress_level']; | ||
} | ||
|
||
// Set collections | ||
if (isset($config['collections']) && is_array($config['collections'])) { | ||
$this->collections = $config['collections']; | ||
|
@@ -750,6 +763,10 @@ protected function pipelineCss($group = 'head', $returnURL = true) | |
// Write non-pipeline files out | ||
if (!empty($this->css_no_pipeline)) { | ||
file_put_contents($this->assets_dir . $inline_file, json_encode($this->css_no_pipeline)); | ||
|
||
if ($this->precompress) { | ||
$this->compressFile($this->assets_dir . $inline_file); | ||
} | ||
} | ||
|
||
|
||
|
@@ -774,6 +791,10 @@ protected function pipelineCss($group = 'head', $returnURL = true) | |
if (strlen(trim($buffer)) > 0) { | ||
file_put_contents($this->assets_dir . $file, $buffer); | ||
|
||
if ($this->precompress) { | ||
$this->compressFile($this->assets_dir . $file); | ||
} | ||
|
||
if ($returnURL) { | ||
return $relative_path . $this->getTimestamp(); | ||
} | ||
|
@@ -843,6 +864,10 @@ protected function pipelineJs($group = 'head', $returnURL = true) | |
// Write non-pipeline files out | ||
if (!empty($this->js_no_pipeline)) { | ||
file_put_contents($this->assets_dir . $inline_file, json_encode($this->js_no_pipeline)); | ||
|
||
if ($this->precompress) { | ||
$this->compressFile($this->assets_dir . $inline_file); | ||
} | ||
} | ||
|
||
// Concatenate files | ||
|
@@ -857,6 +882,10 @@ protected function pipelineJs($group = 'head', $returnURL = true) | |
if (strlen(trim($buffer)) > 0) { | ||
file_put_contents($this->assets_dir . $file, $buffer); | ||
|
||
if ($this->precompress) { | ||
$this->compressFile($this->assets_dir . $file); | ||
} | ||
|
||
if ($returnURL) { | ||
return $relative_path . $this->getTimestamp(); | ||
} | ||
|
@@ -868,6 +897,49 @@ protected function pipelineJs($group = 'head', $returnURL = true) | |
} | ||
} | ||
|
||
/** | ||
* Compresses the given file. | ||
* | ||
* @param string $filename File to compress | ||
* @return bool true if succesful, false if not. | ||
*/ | ||
protected function compressFile($filename = null) | ||
{ | ||
// Test if we have gzencode() | ||
if (!function_exists('gzencode')) { | ||
return false; | ||
} | ||
|
||
// Test if $filename is set | ||
if (!empty($filename)) { | ||
|
||
// Test if the source file exists and can be read | ||
if (!is_readable($filename)) { | ||
return false; | ||
} | ||
|
||
// Test if we can write to the target file | ||
$fp = fopen($filename . ".gz", "w"); | ||
if (!$fp) { | ||
return false; | ||
} | ||
|
||
// Load source file, gzencode it, and write it out | ||
$written = fwrite($fp, gzencode(file_get_contents($filename), $this->precompress_level)); | ||
fclose($fp); | ||
|
||
if ($written === false) { | ||
return false; | ||
} | ||
|
||
// Make the file modification time identical to that of the source file | ||
touch($filename . ".gz", filemtime($filename)); | ||
|
||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it not possible to perform this functionality with the |
||
/** | ||
* Return the array of all the registered CSS assets | ||
* If a $key is provided, it will try to return only that asset | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 see you have provided updates to system.yaml with translations for this field, however, those translations need to be added to the
admin/languages/en.yaml
file as the 'source' language file.