deliver CSS and JS as external request

master
tinect 2019-10-20 21:25:58 +02:00
parent 31eb169152
commit cc1bf5d426
4 changed files with 51 additions and 19 deletions

View File

@ -32,7 +32,7 @@
gzip_buffers 16 8k; gzip_buffers 16 8k;
gzip_http_version 1.1; gzip_http_version 1.1;
gzip_min_length 256; gzip_min_length 256;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon; gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
location ~ ^/(fonts|js|css|img)/ { location ~ ^/(fonts|js|css|img)/ {
expires max; expires max;
@ -205,3 +205,7 @@
location @awaitingupstream { location @awaitingupstream {
rewrite ^(.*)$ /_status.502.html break; rewrite ^(.*)$ /_status.502.html break;
} }
location ~ ^/cache/(.*)$ {
try_files $uri $uri/ /resource.php?file=$1;
}

View File

@ -3,15 +3,16 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/modals/footer.php';
logger(); logger();
?> ?>
<div style="margin-bottom: 100px;"></div> <div style="margin-bottom: 100px;"></div>
<script type='text/javascript'><?php
$JSPath = '/tmp/' . $js_minifier->getDataHash() . '.js'; <?php
if(file_exists($JSPath)) { $hash = $js_minifier->getDataHash();
echo file_get_contents($JSPath); $JSPath = '/tmp/' . $hash . '.js';
} else { if(!file_exists($JSPath)) {
echo $js_minifier->minify($JSPath); $js_minifier->minify($JSPath);
cleanupJS($js_minifier->getDataHash()); cleanupJS($hash);
} }
?></script> ?>
<script src="/cache/<?=basename($JSPath)?>"></script>
<script> <script>
<?php <?php
$lang_footer = json_encode($lang['footer']); $lang_footer = json_encode($lang['footer']);

View File

@ -29,16 +29,15 @@
if ($_SERVER['REQUEST_URI'] == '/') { if ($_SERVER['REQUEST_URI'] == '/') {
$css_minifier->add('/web/css/site/index.css'); $css_minifier->add('/web/css/site/index.css');
} }
$hash = $css_minifier->getDataHash();
$CSSPath = '/tmp/' . $hash . '.css';
if(!file_exists($CSSPath)) {
$css_minifier->minify($CSSPath);
cleanupCSS($hash);
}
?> ?>
<style><?php <link rel="stylesheet" href="/cache/<?=basename($CSSPath)?>">
$CSSPath = '/tmp/' . $css_minifier->getDataHash() . '.css';
if(file_exists($CSSPath)) {
echo file_get_contents($CSSPath);
} else {
echo $css_minifier->minify($CSSPath);
cleanupCSS($css_minifier->getDataHash());
}
?></style>
<?php if (strtolower(trim($DEFAULT_THEME)) != "lumen"): ?> <?php if (strtolower(trim($DEFAULT_THEME)) != "lumen"): ?>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/<?= strtolower(trim($DEFAULT_THEME)); ?>/bootstrap.min.css"> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/<?= strtolower(trim($DEFAULT_THEME)); ?>/bootstrap.min.css">
<?php endif; ?> <?php endif; ?>

View File

@ -0,0 +1,28 @@
<?php
$pathinfo = pathinfo($_GET['file']);
$extension = strtolower($pathinfo['extension']);
$filepath = '/tmp/' . $pathinfo['basename'];
$content = '';
if (file_exists($filepath)) {
$secondsToCache = 31536000;
$expires = gmdate('D, d M Y H:i:s', time() + $secondsToCache) . ' GMT';
if ($extension === 'js') {
header('Content-Type: application/javascript');
} elseif ($extension === 'css') {
header('Content-Type: text/css');
} else {
//currently just css and js should be supported!
exit();
}
header("Expires: $expires");
header('Pragma: cache');
header('Cache-Control: max-age=' . $secondsToCache);
$content = file_get_contents($filepath);
}
echo $content;