mirror of
https://github.com/mrclay/minify.git
synced 2025-08-01 20:00:42 +02:00
+ mincache (writes type-map files on request)
This commit is contained in:
44
mincache/.htaccess
Normal file
44
mincache/.htaccess
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
|
||||||
|
# turn off MultiViews if enabled
|
||||||
|
Options -MultiViews
|
||||||
|
|
||||||
|
# register .var as type-map and zd for deflate encoding
|
||||||
|
AddHandler type-map .mjs .mcss
|
||||||
|
AddEncoding deflate .zd
|
||||||
|
|
||||||
|
# Necessary to add charset while using type-map
|
||||||
|
AddType application/x-javascript;charset=utf-8 js
|
||||||
|
AddType text/css;charset=utf-8 css
|
||||||
|
|
||||||
|
# Below we remove the ETag header and set a far-off Expires
|
||||||
|
# header. Since clients will aggressively cache, make sure
|
||||||
|
# to modify the URL (querystring or via mod_rewrite) when
|
||||||
|
# the resource changes
|
||||||
|
|
||||||
|
# remove ETag
|
||||||
|
FileETag None
|
||||||
|
|
||||||
|
# requires mod_expires
|
||||||
|
ExpiresActive On
|
||||||
|
# sets Expires and Cache-Control: max-age, but not "public"
|
||||||
|
ExpiresDefault "access plus 1 year"
|
||||||
|
|
||||||
|
# requires mod_headers
|
||||||
|
# adds the "public" to Cache-Control.
|
||||||
|
#
|
||||||
|
# If you get 500 errors, you may need to comment this out
|
||||||
|
#
|
||||||
|
Header set Cache-Control "public, max-age=31536000"
|
||||||
|
|
||||||
|
# mod_rewrite reqd.
|
||||||
|
RewriteEngine On
|
||||||
|
|
||||||
|
# remove any rev number
|
||||||
|
RewriteRule ^(.*)_\d+\.m(js|css)$ $1.m$2
|
||||||
|
|
||||||
|
# if mincache is NOT directly within document root, you must
|
||||||
|
# specify the path to it
|
||||||
|
RewriteCond %{DOCUMENT_ROOT}/mincache/$1 !-f
|
||||||
|
|
||||||
|
# any unfound files go to PHP
|
||||||
|
RewriteRule ^(.*)$ gen.php?req=$1 [L,QSA]
|
45
mincache/.htaccess-dev
Normal file
45
mincache/.htaccess-dev
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
# for Steve's test setup. You may delete this file
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# turn off MultiViews if enabled
|
||||||
|
Options -MultiViews
|
||||||
|
|
||||||
|
# register .var as type-map and zd for deflate encoding
|
||||||
|
AddHandler type-map .mjs .mcss
|
||||||
|
AddEncoding deflate .zd
|
||||||
|
|
||||||
|
# Necessary to add charset while using type-map
|
||||||
|
AddType application/x-javascript;charset=utf-8 js
|
||||||
|
AddType text/css;charset=utf-8 css
|
||||||
|
|
||||||
|
# Below we remove the ETag header and set a far-off Expires
|
||||||
|
# header. Since clients will aggressively cache, make sure
|
||||||
|
# to modify the URL (querystring or via mod_rewrite) when
|
||||||
|
# the resource changes
|
||||||
|
|
||||||
|
# remove ETag
|
||||||
|
FileETag None
|
||||||
|
|
||||||
|
# requires mod_expires
|
||||||
|
ExpiresActive On
|
||||||
|
# sets Expires and Cache-Control: max-age, but not "public"
|
||||||
|
ExpiresDefault "access plus 1 year"
|
||||||
|
|
||||||
|
# requires mod_headers
|
||||||
|
# adds the "public" to Cache-Control.
|
||||||
|
Header set Cache-Control "public, max-age=31536000"
|
||||||
|
|
||||||
|
# mod_rewrite reqd.
|
||||||
|
RewriteEngine On
|
||||||
|
|
||||||
|
# remove any rev number
|
||||||
|
RewriteRule ^(.*)_\d+\.m(js|css)$ $1.m$2
|
||||||
|
|
||||||
|
# if mincache is NOT directly within document root, you must
|
||||||
|
# specify the path to it
|
||||||
|
RewriteCond %{DOCUMENT_ROOT}/_3rd_party/minify/mincache/$1 !-f
|
||||||
|
|
||||||
|
# any unfound files go to PHP
|
||||||
|
RewriteRule ^(.*)$ gen.php?req=$1 [L,QSA]
|
69
mincache/README.txt
Normal file
69
mincache/README.txt
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
The files in this directory represent a new way to serve minified files via the
|
||||||
|
Apache file server. This will yield much better performance than Minify can
|
||||||
|
deliver when all requests are routed through PHP.
|
||||||
|
|
||||||
|
|
||||||
|
REQUIREMENTS
|
||||||
|
|
||||||
|
Apache server with mod_negotiation and mod_rewrite
|
||||||
|
|
||||||
|
|
||||||
|
GETTING STARTED
|
||||||
|
|
||||||
|
The "min" directory must already be set up within the DOCUMENT_ROOT.
|
||||||
|
|
||||||
|
Place the "mincache" directory in DOCUMENT_ROOT.
|
||||||
|
|
||||||
|
Request this URL: http://yourserver.com/mincache/f/test.mcss
|
||||||
|
(It should return a CSS file with a "Success" comment.)
|
||||||
|
|
||||||
|
Place CSS/JS source files in "/mincache/src" and/or specify groups in
|
||||||
|
"/min/groupsConfig.php"
|
||||||
|
|
||||||
|
|
||||||
|
UPDATING FILES
|
||||||
|
|
||||||
|
After changing any source files:
|
||||||
|
|
||||||
|
1. Delete any associated ".mcss" or ".mjs" files inside the "f" and "g"
|
||||||
|
directories. (the next request will trigger PHP to rebuild them.)
|
||||||
|
|
||||||
|
2. Add or update a revision number within the URIs in your HTML files. This is
|
||||||
|
recommended because we send a far-future Expires header and you want users
|
||||||
|
to see your changes.
|
||||||
|
E.g. /mincache/f/one,two.mjs => /mincache/f/one,two_2.mjs
|
||||||
|
/mincache/g/grpName_123.mcss => /mincache/g/grpName_124.mcss
|
||||||
|
|
||||||
|
|
||||||
|
HOW THIS WORKS
|
||||||
|
|
||||||
|
In your HTML files you'll refer to minify URIs like:
|
||||||
|
/mincache/f/one,two_12345.mjs
|
||||||
|
/mincache/g/groupName_12345.mcss
|
||||||
|
|
||||||
|
.htaccess will internally strip the revision numbers (_\d+) from the URIs.
|
||||||
|
|
||||||
|
If the files exist, they'll be served directly by Apache and with deflate
|
||||||
|
encoding where supported.
|
||||||
|
|
||||||
|
If not, .htaccess will call /mincache/gen.php to generate the files.
|
||||||
|
|
||||||
|
|
||||||
|
URIS IN THE "/f" DIRECTORY
|
||||||
|
|
||||||
|
For these URIs, Minify will combine files from a single specified source
|
||||||
|
directory, "/mincache/src" by default. E.g.:
|
||||||
|
/mincache/f/one,two.mcss = /mincache/src/one.css
|
||||||
|
+ /mincache/src/two.css
|
||||||
|
/mincache/f/jQuery.1.3,site.mjs = /mincache/src/jQuery.1.3.js
|
||||||
|
+ /mincache/src/site.js
|
||||||
|
|
||||||
|
|
||||||
|
URIS IN THE "/g" DIRECTORY
|
||||||
|
|
||||||
|
(To be implemented)
|
||||||
|
|
||||||
|
|
||||||
|
QUESTIONS?
|
||||||
|
|
||||||
|
http://groups.google.com/group/minify
|
15
mincache/config.php
Normal file
15
mincache/config.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Configuration for mincache application
|
||||||
|
* @package Minify
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Directory from which files will be combined for URIs with "/f".
|
||||||
|
* E.g. by default, the URI "/mincache/f/one,two.js" will combine
|
||||||
|
* "/mincache/src/one.js" and "/mincache/src/two.js"
|
||||||
|
*
|
||||||
|
* Keep in mind, if you move CSS files into this directory, you must
|
||||||
|
* update any relative URIs within the files accordingly.
|
||||||
|
*/
|
||||||
|
$mincache_servePath = dirname(__FILE__) . '/src';
|
101
mincache/gen.php
Normal file
101
mincache/gen.php
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* File generator for mincache directories
|
||||||
|
*
|
||||||
|
* DO NOT EDIT!
|
||||||
|
*
|
||||||
|
* @package Minify
|
||||||
|
*/
|
||||||
|
|
||||||
|
function send404() {
|
||||||
|
header('HTTP/1.0 404 Not Found');
|
||||||
|
exit('File not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
define('MINIFY_MIN_DIR', realpath(dirname(__FILE__) . '/../min'));
|
||||||
|
define('MINCACHE_DIR' , dirname(__FILE__));
|
||||||
|
|
||||||
|
// work on valid-looking URIs only
|
||||||
|
if (! isset($_GET['req'])
|
||||||
|
|| ! preg_match('@^([fg])/([^/]+)\.m(css|js)$@', $_GET['req'], $m)) {
|
||||||
|
send404();
|
||||||
|
}
|
||||||
|
|
||||||
|
$spec = array(
|
||||||
|
'mode' => $m[1]
|
||||||
|
,'group' => $m[2]
|
||||||
|
,'ext' => $m[3]
|
||||||
|
,'plainFilename' => "{$m[2]}.{$m[3]}"
|
||||||
|
,'deflatedFilename' => "{$m[2]}.{$m[3]}.zd"
|
||||||
|
,'typeMap' => MINCACHE_DIR . '/' . $m[0]
|
||||||
|
,'plainFile' => MINCACHE_DIR . "/{$m[1]}/{$m[2]}.{$m[3]}"
|
||||||
|
,'deflatedFile' => MINCACHE_DIR . "/{$m[1]}/{$m[2]}.{$m[3]}.zd"
|
||||||
|
,'ctype' => ($m[3] === 'js' ? 'application/x-javascript' : 'text/css')
|
||||||
|
);
|
||||||
|
|
||||||
|
// load configs
|
||||||
|
require MINIFY_MIN_DIR . '/config.php';
|
||||||
|
require MINCACHE_DIR . '/config.php';
|
||||||
|
|
||||||
|
// setup include path
|
||||||
|
set_include_path($min_libPath . PATH_SEPARATOR . get_include_path());
|
||||||
|
|
||||||
|
require 'Minify.php';
|
||||||
|
|
||||||
|
if ($min_documentRoot) {
|
||||||
|
$_SERVER['DOCUMENT_ROOT'] = $min_documentRoot;
|
||||||
|
} elseif (0 === stripos(PHP_OS, 'win')) {
|
||||||
|
Minify::setDocRoot(); // IIS may need help
|
||||||
|
}
|
||||||
|
|
||||||
|
$min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks;
|
||||||
|
|
||||||
|
$sources = array();
|
||||||
|
|
||||||
|
// URIs in "/f"
|
||||||
|
if ($spec['mode'] === 'f') {
|
||||||
|
$files = explode(',', $spec['group']);
|
||||||
|
foreach ($files as $file) {
|
||||||
|
$file = realpath($mincache_servePath . '/' . $file . '.' . $spec['ext']);
|
||||||
|
if ($file !== false
|
||||||
|
&& is_file($file)
|
||||||
|
&& dirname($file) === realpath($mincache_servePath)) {
|
||||||
|
// file OK
|
||||||
|
$sources[] = $file;
|
||||||
|
} else {
|
||||||
|
send404();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$output = Minify::serve('Files', array(
|
||||||
|
'files' => $sources
|
||||||
|
,'quiet' => true
|
||||||
|
,'encodeMethod' => ''
|
||||||
|
,'lastModifiedTime' => 0
|
||||||
|
));
|
||||||
|
if (! $output['success']) {
|
||||||
|
send404();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$typeMap = "URI: {$spec['deflatedFilename']}\n"
|
||||||
|
. "Content-Type: {$spec['ctype']}; qs=0.9\n"
|
||||||
|
. "Content-Encoding: deflate\n"
|
||||||
|
. "\n"
|
||||||
|
. "URI: {$spec['plainFilename']}\n"
|
||||||
|
. "Content-Type: {$spec['ctype']}; qs=0.6\n";
|
||||||
|
|
||||||
|
error_reporting(0);
|
||||||
|
if (false === file_put_contents($spec['plainFile'], $output['content'])
|
||||||
|
|| false === file_put_contents($spec['deflatedFile'], gzdeflate($output['content']))
|
||||||
|
|| false === file_put_contents($spec['typeMap'], $typeMap)) {
|
||||||
|
echo "/* File writing failed. Your mincache directories /f and /g must be writable by PHP. */\n";
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
unset($output['headers']['Last-Modified'],
|
||||||
|
$output['headers']['ETag']);
|
||||||
|
foreach ($output['headers'] as $name => $value) {
|
||||||
|
header("{$name}: {$value}");
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $output['content'];
|
6
mincache/src/test.css
Normal file
6
mincache/src/test.css
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
/*!
|
||||||
|
Success! Minify wrote this file to disk.
|
||||||
|
If you wish, you may delete "/mincache/f/test.*" and "/mincache/src/test.css".
|
||||||
|
|
||||||
|
*/
|
||||||
|
This file is { content: 'valid css.' ; }
|
Reference in New Issue
Block a user