<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cakephp.nu blog &#187; mod_magnet</title>
	<atom:link href="http://www.cakephp.nu/t/mod_magnet/feed" rel="self" type="application/rss+xml" />
	<link>http://www.cakephp.nu</link>
	<description>Cake and cookies by Jippi</description>
	<lastBuildDate>Wed, 11 Aug 2010 09:15:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Faster Page Loads &#8211; Bundle Your CSS and Javascript with lighttpd</title>
		<link>http://www.cakephp.nu/faster-page-loads-bundle-your-css-and-javascript-lighttpd-mod_magnet-lua</link>
		<comments>http://www.cakephp.nu/faster-page-loads-bundle-your-css-and-javascript-lighttpd-mod_magnet-lua#comments</comments>
		<pubDate>Tue, 18 Mar 2008 15:23:40 +0000</pubDate>
		<dc:creator>Christian Winther</dc:creator>
				<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[mod_magnet]]></category>

		<guid isPermaLink="false">http://www.cakephp.nu/lighttpd-mod_magnet-written-lua-concat-script</guid>
		<description><![CDATA[
Hello again, Preface This time I have been busy playing with Lighttpd and mod_magnet. I found a blog post where darix mentions a post from sitepoint where they demostrate a technique to speed up the HTTP GET of javascript and CSS files. Its quite simple really, instead of doing &#38;lt;link href=&#34;/css/styles1.css&#34; rel=&#34;stylesheet&#34; type=&#34;text/css&#34; /&#38;gt; &#38;lt;link [...]
]]></description>
			<content:encoded><![CDATA[<p>Hello again,</p>
<h3>Preface</h3>
<p>This time I have been busy playing with <a target="_blank" href="http://www.lighttpd.net/">Lighttpd</a> and <a target="_blank" href="http://trac.lighttpd.net/trac/wiki/Docs:ModMagnet">mod_magnet</a>. I found <a target="_blank" href="http://nordisch.org/2007/4/10/hah-it-s-not-only-me">a blog post </a>where <a target="_blank" href="http://nordisch.org/">darix </a> mentions a post from sitepoint where they demostrate <a target="_blank" href="http://www.sitepoint.com/blogs/2007/04/10/faster-page-loads-bundle-your-css-and-javascript/">a technique to speed up the HTTP GET of javascript and CSS files</a>. </p>
<p>Its quite simple really, instead of doing</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&amp;lt;link href=&quot;/css/styles1.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&amp;gt;
&amp;lt;link href=&quot;/css/styles2.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&amp;gt;
&amp;lt;link href=&quot;/css/styles3.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&amp;gt;</pre></div></div>

<p>you simply do</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&amp;lt;link href=&quot;/css/styles1.css,styles2.css,styles3.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&amp;gt;</pre></div></div>

<p>and then this script will automagic concat them together into one big file, and make lighty serve that one instead. (The same goes for javascript files, or anything really)</p>
<p>This could save ALOT of HTTP GET&#8217;s to your server, and increase the overall loadtime and performance of your site alot, especially if you use alot of Web2.0 stuff <img src='http://www.cakephp.nu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>However, the Lua source for their script was not made public, so nobody could benifit from their otherwise impressive speed gains. <img src='http://www.cakephp.nu/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Until now.. or, well, almost &#8211; its not their script, but my own attempt to mimic what they did in pure <a target="_blank" href="http://www.lua.org/">Lua</a> <img src='http://www.cakephp.nu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I tried to keep the dependencies down to a minimum, however I decided to use one, md5.<br />
You can find the source <a target="_blank" href="http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lmd5">here</a> (<a href="http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/5.1/lmd5.tar.gz">5.1</a>, <a href="http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/5.0/lmd5.tar.gz">5.0</a>)</p>
<h3>Config</h3>
<dl>
<dt>charset (Line 42)</dt>
<dd>Set it so it matches the headers in your lighttpd.conf file. Must match your mod_compress settings aswell to utilize that feature</dd>
<dt>prefix (Line 43)</dt>
<dd>Could be anything really, its just a mean to &#8220;namespace&#8221; your cached files so you can run the script with multiple configs</dd>
<dt>rootPath (Line 44)</dt>
<dd>The path to look for the files to concat, default should be fine for most  DOC_ROOT/js  and DOC_ROOT/css</dd>
<dt>concatRoot (Line 45)</dt>
<dd>The folder to store the concated files, can be anywhere <img src='http://www.cakephp.nu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </dd>
</dl>
<h3>Setup</h3>
<ul>
<li>Put the source below somewhere on your disk, I asume you name it bundle.lua</li>
<li>Enable mod_magnet in lighttpd.conf</li>
<li>Add <u>magnet.attract-physical-path-to = ( &#8220;YOUR_PATH/bundle.lua&#8221; )</u> to your config where needed</li>
<li>Restart lighttpd</li>
<li>Modify your css/js links to utilize the new feature:)</li>
</ul>
<p>Optimal you can enable mod_compress aswell, bundle.lua works out of the box with any other lighttpd module you may have <img src='http://www.cakephp.nu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Source</h3>
<p><a target="_blank" href="http://www.cakephp.nu/files/mod_magnet/bundle_0.1.lua">download source</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
</pre></td><td class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">--- get stat information on a path</span>
<span style="color: #808080; font-style: italic;">-- @param path to stat</span>
<span style="color: #808080; font-style: italic;">-- @param the ftype to return</span>
<span style="color: #808080; font-style: italic;">-- @return false if ftype or path does not exist</span>
<span style="color: #b1b100;">function</span> file_info<span style="color: #66cc66;">&#40;</span>path, ftype<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">local</span> attr <span style="color: #66cc66;">=</span> lighty.stat<span style="color: #66cc66;">&#40;</span>path<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">if</span> attr <span style="color: #b1b100;">and</span> attr<span style="color: #66cc66;">&#91;</span>ftype<span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100;">then</span>
        <span style="color: #b1b100;">return</span> attr<span style="color: #66cc66;">&#91;</span>ftype<span style="color: #66cc66;">&#93;</span>
    <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">false</span>
<span style="color: #b1b100;">end</span>
&nbsp;
<span style="color: #808080; font-style: italic;">--- Wrapper for reading a full file into a string</span>
<span style="color: #808080; font-style: italic;">-- @param filename Full path to the file</span>
<span style="color: #808080; font-style: italic;">-- @return a string with the content of the file</span>
<span style="color: #b1b100;">function</span> read_file<span style="color: #66cc66;">&#40;</span>filename<span style="color: #66cc66;">&#41;</span> 
    <span style="color: #b1b100;">local</span> content <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>
    <span style="color: #b1b100;">if</span> file_info<span style="color: #66cc66;">&#40;</span>filename, <span style="color: #ff0000;">&quot;is_file&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span>
        <span style="color: #b1b100;">local</span> file <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">io.open</span><span style="color: #66cc66;">&#40;</span>filename, <span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #66cc66;">&#41;</span>
        content <span style="color: #66cc66;">=</span> file:<span style="color: #b1b100;">read</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;*a&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">io.close</span><span style="color: #66cc66;">&#40;</span>file<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">return</span> content
<span style="color: #b1b100;">end</span>
&nbsp;
<span style="color: #808080; font-style: italic;">--- Wrapper for writeing content to a file</span>
<span style="color: #808080; font-style: italic;">-- @param filename Full path to the destionation file</span>
<span style="color: #808080; font-style: italic;">-- @param content The string to write</span>
<span style="color: #b1b100;">function</span> write_cache<span style="color: #66cc66;">&#40;</span>filename, content<span style="color: #66cc66;">&#41;</span> 
    <span style="color: #b1b100;">local</span> file <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">io.open</span><span style="color: #66cc66;">&#40;</span>filename, <span style="color: #ff0000;">&quot;w&quot;</span><span style="color: #66cc66;">&#41;</span>
    file:<span style="color: #b1b100;">write</span><span style="color: #66cc66;">&#40;</span>content<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">io.close</span><span style="color: #66cc66;">&#40;</span>file<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">end</span>
&nbsp;
<span style="color: #808080; font-style: italic;">--- Concat multiple files into one file </span>
<span style="color: #808080; font-style: italic;">-- @param lighty lighty global variable passed to the method</span>
<span style="color: #808080; font-style: italic;">-- @param match The files that will be concat into a file</span>
<span style="color: #808080; font-style: italic;">-- @param fileExtension Do !NOT! include the dot ( . )</span>
<span style="color: #b1b100;">function</span> combine_files<span style="color: #66cc66;">&#40;</span>lighty, files, fileExtension<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">&quot;md5&quot;</span> 
&nbsp;
    <span style="color: #b1b100;">local</span> charset       <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;; charset=utf-8&quot;</span>
    <span style="color: #b1b100;">local</span> prefix        <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;cache-&quot;</span>
    <span style="color: #b1b100;">local</span> rootPath      <span style="color: #66cc66;">=</span> lighty.env<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;physical.doc-root&quot;</span><span style="color: #66cc66;">&#93;</span> .. fileExtension .. <span style="color: #ff0000;">&quot;/&quot;</span>
    <span style="color: #b1b100;">local</span> concatRoot     <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;/tmp/cache/&quot;</span>
    <span style="color: #b1b100;">local</span> lastModified  <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
&nbsp;
    <span style="color: #b1b100;">for</span> file <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">string</span>.gmatch<span style="color: #66cc66;">&#40;</span>files, <span style="color: #ff0000;">&quot;(.-<span style="color: #000099; font-weight: bold;">\.</span>&quot;</span> .. fileExtension .. <span style="color: #ff0000;">&quot;),?&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
        <span style="color: #b1b100;">local</span> fullPath <span style="color: #66cc66;">=</span> rootPath .. file
        modTime <span style="color: #66cc66;">=</span> file_info<span style="color: #66cc66;">&#40;</span>fullPath, <span style="color: #ff0000;">&quot;st_mtime&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>modTime<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;number&quot;</span> <span style="color: #b1b100;">then</span>
            lastModified <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">math.max</span><span style="color: #66cc66;">&#40;</span>lastModified, modTime<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>            
     <span style="color: #b1b100;">end</span>
&nbsp;
     <span style="color: #b1b100;">local</span> hash <span style="color: #66cc66;">=</span> lastModified .. <span style="color: #ff0000;">&quot;-&quot;</span> .. md5.sumhexa<span style="color: #66cc66;">&#40;</span>files<span style="color: #66cc66;">&#41;</span>
     lighty.header<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Etag&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'&quot;'</span> .. hash ..<span style="color: #ff0000;">'&quot;'</span>
&nbsp;
    <span style="color: #b1b100;">local</span> cacheFile <span style="color: #66cc66;">=</span> prefix .. hash .. <span style="color: #ff0000;">'.'</span> .. fileExtension
    <span style="color: #b1b100;">if</span> <span style="color: #b1b100;">not</span> file_info<span style="color: #66cc66;">&#40;</span>concatRoot .. cacheFile, <span style="color: #ff0000;">&quot;is_file&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span>
        <span style="color: #b1b100;">local</span> content <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>
        <span style="color: #b1b100;">for</span> file <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">string</span>.gmatch<span style="color: #66cc66;">&#40;</span>match, <span style="color: #ff0000;">&quot;(.-<span style="color: #000099; font-weight: bold;">\.</span>&quot;</span> .. fileExtension .. <span style="color: #ff0000;">&quot;),?&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
            content <span style="color: #66cc66;">=</span> content .. <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            content <span style="color: #66cc66;">=</span> content .. <span style="color: #ff0000;">&quot;/**<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            content <span style="color: #66cc66;">=</span> content .. <span style="color: #ff0000;">&quot; *<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            content <span style="color: #66cc66;">=</span> content .. <span style="color: #ff0000;">&quot; *  &quot;</span> .. file .. <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            content <span style="color: #66cc66;">=</span> content .. <span style="color: #ff0000;">&quot; *<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            content <span style="color: #66cc66;">=</span> content .. <span style="color: #ff0000;">&quot; */<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            content <span style="color: #66cc66;">=</span> content .. <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            content <span style="color: #66cc66;">=</span> content .. read_file<span style="color: #66cc66;">&#40;</span>rootPath .. file<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>
        write_cache<span style="color: #66cc66;">&#40;</span>concatRoot .. cacheFile, content <span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
    lighty.env<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;physical.path&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> concatRoot .. cacheFile
<span style="color: #b1b100;">end</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span> file_info<span style="color: #66cc66;">&#40;</span>lighty.env<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;physical.path&quot;</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">&quot;is_file&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span>
    css <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">string</span>.match<span style="color: #66cc66;">&#40;</span>lighty.env<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;physical.path&quot;</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">&quot;css/(.*<span style="color: #000099; font-weight: bold;">\.</span>css)&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #b1b100;">if</span> css <span style="color: #b1b100;">then</span>
        <span style="color: #b1b100;">return</span> combine_files<span style="color: #66cc66;">&#40;</span>lighty, css, <span style="color: #ff0000;">&quot;css&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    js <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">string</span>.match<span style="color: #66cc66;">&#40;</span>lighty.env<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;physical.path&quot;</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">&quot;js/(.*<span style="color: #000099; font-weight: bold;">\.</span>js)&quot;</span><span style="color: #66cc66;">&#41;</span>	    
	<span style="color: #b1b100;">if</span> js <span style="color: #b1b100;">then</span>
        <span style="color: #b1b100;">return</span> combine_files<span style="color: #66cc66;">&#40;</span>lighty, js, <span style="color: #ff0000;">&quot;js&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #b1b100;">end</span> 
<span style="color: #b1b100;">end</span></pre></td></tr></table></div>


	Tags: <a href="http://www.cakephp.nu/t/english" title="english" rel="tag">english</a>, <a href="http://www.cakephp.nu/t/lighttpd" title="lighttpd" rel="tag">lighttpd</a>, <a href="http://www.cakephp.nu/t/lua" title="lua" rel="tag">lua</a>, <a href="http://www.cakephp.nu/t/mod_magnet" title="mod_magnet" rel="tag">mod_magnet</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.cakephp.nu/faster-page-loads-bundle-your-css-and-javascript-lighttpd-mod_magnet-lua/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc

Served from: www.cakephp.nu @ 2012-02-05 16:25:18 -->
