Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter Caching Problem with HTTP Headers (Content-Type)
#1

[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
#2

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

[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...
#4

[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.
#5

[eluser]bertd[/eluser]
Allright,

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

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB