开发者

Combining CSS in a single request

Does anyon开发者_JS百科e know how to achieve something like TypeKit when combining multiple CSS request? Maybe I'am not aware of that but when you list some fonts the site would generate (maybe dynamic) CSS like 567,568,569.css lo load the font-file. I thought of it as dynamic as it would change if you use other combination (in this case font ID).


I use the technique described by Carpetsmoker, but I didn't like the fact that the PHP script is invoked every time. On Apache, you can set the following rewrite rule (in .htaccess):

RewriteCond %{REQUEST_URI} ^/css/cache
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^css/cache/(.*)$ /css/csscacher.php?files=$1 [L]

So say a request comes in for /css/cache/file1.css-file2.css, Apache will test for its existence. If it doesn't exist, the request will be forwarded to the csscacher.php script with the filename passed as the value of the "files" param. csscacher.php will load and combine the multiple files, send the result to the browser, but also write the result to /css/cache/file1.css-file2.css. All subsequent requests will be served as a static file.

To clear the cache, you'd just delete everything in the /css/cache folder. csscacher.php will recreate them from the source files as requests come in. More detail here.


You could also just use

@import url('reset.css');

at the top of your main css fiel to import other css files on the fly.


Have a look at the Google minify project. It offers a good solution to combine and also compress your JavaScript or CSS files. It is a PHP library that you can set up on your webserver with a script that takes a list of JS or CSS files and outputs a concatenated version. It also caches the result.


The implementation could be separated into three steps. Firstly, define a control wraps all the reference JS files.

Secondly, during the rendering of that control, using any kind of algorithm (e.g. encoding / encrypting) for all file paths to a string, and generate the script tag with a src which points to a certain handler with that generated as querystring.

e.g. Image we have two files: a.js and b.js, we have a control wraps them and generates the script tag like:

<script type='text/javascript' src='/js.php?include=encodeab'></script>

Thirdly, when client side displays the html page and sends request for that script tag, a certain server side handler (js.php in above case) will decode / decrypt that querystring to a list of included files, then read content of them, combile together and output to stream.

Hope this helps.


Be wary using dynamic js or css files as you may accidentally force the user to download them on each page (instead of using browser caching).

You can include multiple javascript/php files into one file, then give it a header of type javascript:

header("Content-type: text/javascript");
include('javascript1.php');
include('javascript2.js');

The same holds true for CSS.

Resources: http://www.javascriptkit.com/javatutors/externalphp.shtml http://www.webmasterworld.com/php/4239826.htm


You can use something along the lines of:

<?php
  header('Content-Type: text/css');

  if (isset($_GET['files']))
  {
    $files = explode(',', $_GET['files']);
    foreach ($files as $file)
    {
      # BEWARE!
      # What happens if the file is ../../../../../../../../etc/passwd?
      $file = str_replace('..', '', ltrim($file, '/'));
      include($file);
    }
  }
?>

test.html

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="css.php?files=style1.css,style2.css" />
  </head>

  <body>
    <h1>This should be red</h1>
    <p>red border</p>
  </body>
</html>

style1.css

p { border: 1px solid red; }

style2.css

h1 { color: red; }

This is a simple example, you can easily expand it to allow javascript files. Another good optimisation would be setting the Last modified headers based on the mtime of the .css files (use stat()) ... But the general idea should be clear.

BEWARE, to be honest, I'm not sure if the escaping/parsing of the $_GET['files'] is enough ... Please research this topic to be sure, this can a very dangerous security problem :-)

Hope this is enough to get you in the right direction.


You can do something close by calling a dynamic JS file: start with a php file and then in it:

<?php
    if(isset($_GET['jsOne'])){
        include'example.com/js/one.js'; // points to some .js file 
    } 
    if(isset($_GET['jsTwo'])){
        include'example.com/js/two.js'; // points to some other .js file
    }
    if(isset($_GET['jsThree'])){
        include'example.com/js/three.js'; // points to yet a another .js file
    }
?>

and in the header just have:

<script type="text/javascript" src="js/allScripts.php?jsOne=yes&jsThree=yes"> and so on

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜