Permalink
Browse files

Use immutable caching with `immutable` in `Cache-Control` header.

Move built-in AMD modules list from `zz1.alameda-setup.ls` to backend event handler.
Add query parameters when loading AMD modules, so that updates of Bower/NPM packages and system modules will result in actual files reloading on frontend.
  • Loading branch information...
nazar-pc committed Jan 29, 2017
1 parent b9e59f9 commit 7228011d8a1f2128df0efda1070ddd56ba8b9010
@@ -18,7 +18,7 @@ RewriteBase /
ExpiresDefault "access plus 1 month"
</ifModule>
<ifModule mod_headers.c>
Header set Cache-Control "max-age=2592000, public"
Header set Cache-Control "max-age=2592000, immutable"
</ifModule>
</FilesMatch>
<Files license.txt>

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -6,10 +6,10 @@
*/
requirejs.config(
baseUrl : '/'
paths :
jssha : 'assets/js/modules/jsSHA-2.1.0'
autosize : 'assets/js/modules/autosize.min'
html5sortable : 'assets/js/modules/html5sortable-0.4.3.min'
'sprintf-js' : 'assets/js/modules/sprintf-1.0.3.min'
urlArgs : (id, url) ->
for path, hash of requirejs.contexts._.config.hashes
if url.indexOf(path) == 0
return (if url.indexOf('?') === -1 then '?' else '&') + hash
''
waitSeconds : 60
)
@@ -285,7 +285,7 @@ protected function get_htaccess () {
ExpiresDefault "access plus 1 month"
</ifModule>
<ifModule mod_headers.c>
Header set Cache-Control "max-age=2592000, public"
Header set Cache-Control "max-age=2592000, immutable"
</ifModule>
</FilesMatch>
<Files license.txt>
@@ -42,9 +42,13 @@ public static function get_config () {
$packages[] = static::find_package_main_path($d, $allowed_extensions);
}
}
$paths = _substr($paths, strlen(DIR));
$packages = array_values(array_filter($packages));
$hashes = static::get_hashes($paths, $packages);
return [
'paths' => _substr($paths, strlen(DIR)),
'packages' => array_values(array_filter($packages))
'paths' => $paths,
'packages' => $packages,
'hashes' => $hashes
];
}
/**
@@ -120,4 +124,29 @@ protected static function find_package_npm ($dir, $allowed_extensions) {
}
return false;
}
/**
* @param string[] $paths
* @param array[] $packages
*
* @return string[]
*/
protected static function get_hashes ($paths, $packages) {
$hashes = [];
foreach ($packages as $package) {
$hash =
@md5_file(DIR."/$package[location]/bower.json").
@md5_file(DIR."/$package[location]/package.json").
@md5_file(DIR."/$package[location]/$package[main].js");
$hashes[$package['location']] = $hash;
}
foreach ($paths as $path) {
$hash =
@md5_file(DIR."/$path/bower.json").
@md5_file(DIR."/$path/package.json").
@md5_file(DIR."/$path/../../meta.json").
@md5_file(DIR."/$path.js");
$hashes[$path] = $hash;
}
return _substr($hashes, 0, 5);
}
}
@@ -69,7 +69,7 @@ protected function serve_static_files () {
* @throws ExitException
*/
protected function serve_static_file ($path, $headers = []) {
$headers += ['cache-control' => 'max-age=2592000, public'];
$headers += ['cache-control' => 'max-age=2592000, immutable'];
Response::instance()->init('', fopen(DIR.$path, 'rb'), $headers);
throw new ExitException;
}
@@ -34,7 +34,7 @@ server {
location ~ \.(hash|json)$ {
return 403;
}
add_header Cache-Control "max-age=2592000, public";
add_header Cache-Control "max-age=2592000, immutable";
}
# Some headers to improve security when dealing with downloaded content
location /storage/public {
@@ -39,7 +39,7 @@ server {
location ~ \.(hash|json)$ {
return 403;
}
add_header Cache-Control "max-age=2592000, public";
add_header Cache-Control "max-age=2592000, immutable";
}
# Some headers to improve security when dealing with downloaded content
location /storage/public {
@@ -169,7 +169,7 @@ function () {
ExpiresDefault "access plus 1 month"
</ifModule>
<ifModule mod_headers.c>
Header set Cache-Control "max-age=2592000, public"
Header set Cache-Control "max-age=2592000, immutable"
Header always append X-Frame-Options DENY
Header set Content-Type application/octet-stream
</ifModule>
@@ -1,7 +1,7 @@
{
"package" : "Composer_assets",
"category" : "modules",
"version" : "1.21.0+build-49",
"version" : "1.22.0+build-50",
"description" : "Bower and NPM packages support through Composer using fxp/composer-asset-plugin for assets management, oyejorge/less.php for LESS compilation and leafo/scssphp for SCSS",
"author" : "Nazar Mokrynskyi",
"website" : "cleverstyle.org/Framework",
@@ -3,6 +3,6 @@
ExpiresDefault "access plus 1 month"
</ifModule>
<ifModule mod_headers.c>
Header set Cache-Control "max-age=2592000, public"
Header set Cache-Control "max-age=2592000, immutable"
Header set Service-Worker-Allowed /
</ifModule>
@@ -1,7 +1,7 @@
{
"package" : "Service_worker_cache",
"category" : "modules",
"version" : "0.7.0+build-11",
"version" : "0.8.0+build-12",
"description" : "Uses Service Worker to cache requests to static assets like CSS/JS/HTML/fonts/images to improve subsequent page loads, especially on bad internet connection",
"author" : "Nazar Mokrynskyi",
"website" : "cleverstyle.org/Framework",
@@ -65,14 +65,14 @@ function () {
$Page = Page::instance();
$core_url = $Config->core_url();
$base_url = $Config->base_url();
$Page->Head .= h::link(
$Page->Head .= h::link(
[
'hreflang' => 'x-default',
'href' => $Request->home_page ? $core_url : "$core_url/$relative_address",
'rel' => 'alternate'
]
);
$clangs = Cache::instance()->get(
$clangs = Cache::instance()->get(
'languages/clangs',
function () use ($Config) {
$clangs = [];
@@ -122,4 +122,15 @@ function () {
}
}
}
)
->on(
'System/Page/requirejs',
function ($data) {
$data['paths'] += [
'jssha' => DIR.'/assets/js/modules/jsSHA-2.1.0',
'autosize' => DIR.'/assets/js/modules/autosize.min',
'html5sortable' => DIR.'/assets/js/modules/html5sortable-0.4.3.min',
'sprintf-js' => DIR.'/assets/js/modules/sprintf-1.0.3.min'
];
}
);
@@ -1,7 +1,7 @@
{
"package" : "System",
"category" : "modules",
"version" : "6.45.3+build-2588",
"version" : "6.46.0+build-2589",
"description" : "Base system module of CleverStyle Framework",
"author" : "Nazar Mokrynskyi",
"website" : "cleverstyle.org/Framework",
@@ -0,0 +1,18 @@
<?php
/**
* @package CleverStyle Framework
* @category modules
* @author Nazar Mokrynskyi <nazar@mokrynskyi.com>
* @copyright Copyright (c) 2017, Nazar Mokrynskyi
* @license MIT License, see license.txt
*/
namespace cs;
file_put_contents(
DIR.'/.htaccess',
str_replace(
'Header set Cache-Control "max-age=2592000, public"',
'Header set Cache-Control "max-age=2592000, immutable"',
file_get_contents(DIR.'/.htaccess')
)
);
@@ -4,7 +4,7 @@ Allow From All
ExpiresDefault "access plus 1 year"
</ifModule>
<ifModule mod_headers.c>
Header set Cache-Control "max-age=946080000, public"
Header set Cache-Control "max-age=946080000, immutable"
Header always append X-Frame-Options DENY
Header set Content-Type application/octet-stream
</ifModule>
@@ -71,7 +71,7 @@ array(2) {
}
}
}
array(2) {
array(3) {
["paths"]=>
array(4) {
["Disabled"]=>
@@ -149,6 +149,27 @@ array(2) {
string(26) "/node_modules/package-main"
}
}
["hashes"]=>
array(9) {
["/bower_components/package-js"]=>
string(5) "%s"
["/bower_components/package-min-js"]=>
string(5) "%s"
["/node_modules/package-browser"]=>
string(5) "%s"
["/node_modules/package-js-browser"]=>
string(5) "%s"
["/node_modules/package-js-browser-min"]=>
string(5) "%s"
["/node_modules/package-jspm-main"]=>
string(5) "%s"
["/node_modules/package-main"]=>
string(5) "%s"
["/modules/Disabled/assets/js"]=>
string(5) "%s"
["/modules/System/assets/js"]=>
string(0) ""
}
}
string(25) "Do not use minified files"
string(28) "cs\Event::fire() called with"
@@ -172,7 +193,7 @@ array(2) {
}
}
}
array(2) {
array(3) {
["paths"]=>
array(4) {
["Disabled"]=>
@@ -250,4 +271,25 @@ array(2) {
string(26) "/node_modules/package-main"
}
}
["hashes"]=>
array(9) {
["/bower_components/package-js"]=>
string(5) "%s"
["/bower_components/package-min-js"]=>
string(5) "%s"
["/node_modules/package-browser"]=>
string(5) "%s"
["/node_modules/package-js-browser"]=>
string(5) "%s"
["/node_modules/package-js-browser-min"]=>
string(5) "%s"
["/node_modules/package-jspm-main"]=>
string(5) "%s"
["/node_modules/package-main"]=>
string(5) "%s"
["/modules/Disabled/assets/js"]=>
string(5) "%s"
["/modules/System/assets/js"]=>
string(0) ""
}
}
Oops, something went wrong.

0 comments on commit 7228011

Please sign in to comment.