CodeIgniter Forums
CodeIgniter Caching Problem with HTTP Headers (Content-Type) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: CodeIgniter Caching Problem with HTTP Headers (Content-Type) (/showthread.php?tid=33563)



CodeIgniter Caching Problem with HTTP Headers (Content-Type) - El Forum - 08-31-2010

[eluser]bertd[/eluser]
Hi,

I was wondering if anybody here could help me out with a little question I have.

I have a CSS file that is loaded through a controller & view. Please consider this example:

Code:
class Css extends Controller {
    function Css()
    {
        parent::Controller();

        $this->load->library('cssmin');
        $cssmin_options = array(
            'preserveComments'=>true,
            'relativePath'=>$this->config->item('base_url').'resources/images/'
        );
        
        $this->cssmin->config($cssmin_options);
    }

    function main
    {
        header("Content-type: text/css");

        $this->output->cache(15);
        $this->data['css'] = $this->cssmin->minify(file_get_contents('./resources/stylesheets/main.css'));
        $this->load->view('css/css', $this->data);
    }
}

As you can see, I minify my CSS file with a custom library to save bandwidth. This is off course a very CPU intensive process so after the visitor has requested the CSS file, there really is no need to minify the file for every HTTP request the browser of the user makes. That is why I use
Code:
$this->output->cache(15);
to cache the file for at least 15 minutes.

This works perfect for the first request the users' browser makes. The second time, the CSS files seem to have the Content-type text/html, so no styling is applied at all!

How can I use caching and make sure the Content-Type header is also cached, so codeigniter does not fall back to text/html but uses text/css??

Thanks!

Gr,
Bert


CodeIgniter Caching Problem with HTTP Headers (Content-Type) - El Forum - 08-31-2010

[eluser]pickupman[/eluser]
Checkout carabiner listed in my signature. Minify css & js, and you set how long it is cached for.


CodeIgniter Caching Problem with HTTP Headers (Content-Type) - El Forum - 08-31-2010

[eluser]bertd[/eluser]
Hi,

Thanks for your reply.

I will definitely look into Carabiner, there seem to be a lot of possibilities.
But for now: is there no quick solution for my problem? I have also tried adding the
Code:
header("Content-type: text/css");
line to the view instead of the controller but that also does not work...


CodeIgniter Caching Problem with HTTP Headers (Content-Type) - El Forum - 08-31-2010

[eluser]pickupman[/eluser]
The problem with CI caching that it renders a full html page. On the next pages request, CI will look in its cache before it processes the URI. If a match is found in the cache, the file is output as html. No php is run, so your headers will not work on subsequent attempts.

Just use carabiner. You will thank me later. Edit the carabiner config to point to your assets folder then use:
Code:
$this->carabiner->css('your-css.css');
$this->carabiner->display('css');

And you are done, caching, minifying, combining all in a few simple lines of code.


CodeIgniter Caching Problem with HTTP Headers (Content-Type) - El Forum - 08-31-2010

[eluser]bertd[/eluser]
Allright,

Thanks for you help, I really appreciate it.
I'll change my code so it uses the Carabiner library...

Thanks!