admin管理员组

文章数量:1336632

I would like to press the CSS and JS files on my server to minimise load times, problem.

My hosting is with Streamline (big mistake, never go there) who will not activate mod_gzip and mod_deflate due to security issues.

Does anyone have another way to press these types of files (and image files too if poss) without going the way of mod_gzip and mod_deflate.

Answers would be hugely wele.

I would like to press the CSS and JS files on my server to minimise load times, problem.

My hosting is with Streamline (big mistake, never go there) who will not activate mod_gzip and mod_deflate due to security issues.

Does anyone have another way to press these types of files (and image files too if poss) without going the way of mod_gzip and mod_deflate.

Answers would be hugely wele.

Share Improve this question asked Dec 23, 2009 at 13:27 DanCDanC 1,3074 gold badges24 silver badges42 bronze badges 5
  • Do you have support for a programming language? – Gumbo Commented Dec 23, 2009 at 13:31
  • I'm not sure, how would I find out? – DanC Commented Dec 23, 2009 at 13:45
  • Ask your webhost? They probably have either ASP or PHP – Adrian Grigore Commented Dec 23, 2009 at 13:53
  • @DanC: Ask your hosting provider. – Gumbo Commented Dec 23, 2009 at 13:53
  • sorry I thought (for some reason) you meant something else. The server does support PHP, the backbone to the site is Wordpress and written in PHP – DanC Commented Dec 23, 2009 at 13:57
Add a ment  | 

5 Answers 5

Reset to default 4

Yes, the answer is Minification.

Obviously, it will not press as much as gzip or deflate. But it helps, and it is very easy to do with the right tools.

You can run your files through a script which would gzip them for you and add appropriate expiration headers. Either set up an URL rewrite, or rewrite the URLs manually:

<script src="js/somescript.js"></script>

bees

<script src="press.php?somescript.js"></script>

and in press.php, you can do something like

<?php
$file = 'js/' . basename($_SERVER['QUERY_STRING']);
if (file_exists($file)) {
    header ('Last-Modified: ' . date('r',filemtime($file));
    header ('Content-Type: text/javascript'); // otherwise PHP sends text/html, which could confuse browsers
    ob_start('ob_gzhandler');
    readfile($file);
} else {
    header('HTTP/1.1 404 Not Found');
}

Obviously this can be extended to also provide HTTP caching, and/or on-the-fly minification, further speeding up your visitors' browsing.

Instead of getting mod_gzip to gzip your CSS and JavaScript files dynamically, you can gzip them yourself, then upload them.

This does introduce another step before you upload CSS and JavaScript, but it works, and maybe even saves a tiny bit of server processing time for each request pared to mod_gzip.

On Mac OS X, gzipping a file on the mand line is as easy as, e.g.:

gzip -c styles.css > styles-gzip.css

Make sure these files get served with the right content-type header though.

Just as a sidenote: Compressing images would not be beneficial if these are already saved in a pressed format with the maximum pression that still looks good to the user.

Most programming languages support some data or file pression formats like ZLIB or GZIP. So you could use a programming language to press your files with one of these formats.

本文标签: javascriptCompressing CSS and JS without modgzip and moddeflateStack Overflow