Welcome Guest, Not a member yet? Register   Sign In
Dynamic javascript and CSS files - through code igniter
#1

[eluser]bwuk[/eluser]
Hi,

I'm using CI to generate some dynamic javascript files. It all works fine, with no problems.

However, I'm now trying to cache the javascript files. I'm running on apache, and I've added the following to my .htaccess file:

Code:
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|rpm|ico|iso|gz|bz2)$ no-gzip dont-vary
Header append Vary  User-Agent env=!dont-vary
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
    Header set Cache-Control "max-age=290304000, public"
    Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>


For the dynamic javascript files, they're not being cached. However, if have a 'standard' javascript file, that is being cached.

For example, my dynamic javascript file path may be:

Code:
/media/jquery.js

and for the 'standard' javascript file:

Code:
/scripts/test.js


and I have the following setup in my .htaccess for mod_rewrite:


Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]


So, is CI doing something special with my dynamic javascript files? sending some wrong headers? content type etc?


Thanks


bwuk
#2

[eluser]bwuk[/eluser]
Ok,

so I've now tried forcing headers in my javascript controller using:

Code:
header ('Cache-Control: private, pre-check=0, post-check=0, max-age=31536000');
header ('Expires: ' . gmstrftime("%a, %d %b %Y %H:%M:%S GMT", time()       +    31536000)); //60*3

but it appears as though CI is just ignoring these. Does anyone have any suggestions?


thanks

bwuk
#3

[eluser]bwuk[/eluser]
Hi,

so I've been doing more digging on this issue.

If I change my .htaccess FilesMatch directive to:

Code:
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|php)$"> # (note the php at the end)
    Header set Cache-Control "max-age=290304000, public"
    Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>


then my javascript files are then cached....however, this leads to all of my other php files being cached....not what I want Smile

Ideally, I'd prefer to do this in CI. I've extended Controller, and added my own called Public_Controller. In there, I do some detection to determine whether or not I'm able to cache certain content. If I'm using my javascript class or my css class, then I can cache the content:

Code:
if ($this->cacheable_content())
{
    $this->output->cache(1440);
}

I've also tried:

Code:
if ($this->cacheable_content())
{
    $this->output->set_header("Cache-Control: max-age=290304000, public");
    $this->output->set_header("Expires: Thu, 15 Apr 2010 20:00:00 GMT");
}

but again, that just doesn't seem to work. I'm running out of ideas here Sad


Thanks

bwuk




Theme © iAndrew 2016 - Forum software by © MyBB