How do I correctly ENABLE browser caching using codeigniter? - codeigniter

Every time I do I search on this I get information about how to disable the browser cache.
Never anything about enabling it.
How do I get the back button to use the cache and not regenerate the page?

As far as I know you can control to force a browser to reload the data by means of these meta tags:
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Expires" content="0">
but you cannot force it to read from cache. The browser itself will do that for you if you don't explicitly specify to ignore the cache, and the page data are in fact cached and not expired.
This does not depend on CodeIgniter because it's client-side, but you might want to use the meta() function included in CI's html helper, which will simply output the corresponding meta tag. e.g:
echo meta('Cache-control', 'no-cache', 'http-equiv');
would generate the second code line above.
Note:
The 1st meta tag is specified for http/1.0 while the 2nd one is for http/1.1 but both are used to allow backwards compatibility.
If you're using xhtml instead of html remember to close the meta tags with />

Browser caching has nothing to do with codeigniter. You can use html meta tags to instruct the browser specifically not to cache pages or you can set a cache expiry for an individual page like so:
<meta http-equiv="expires" content="Mon, 10 Dec 2001 00:00:00 GMT" />
You could use a bit of php to drop tomorrows date in there. The browser (depending on settings) will usually pull as much as it can from the cache automatically, including when clicking the back button - the cache for the back button will work the same as if you were coming in from any other link.
You could set expires headers through your htaccess using something like the following on an apache server (you would have to ask about how to do this on other server types) to tell the browser that is should cache certain types of content for a given periods of time:
ExpiresByType text/html "access plus 60 seconds"
This will tell the browser to store anything of mime type text/html for 60 seconds (this includes codeigniter output) BUT DONT DO THIS if your dealing with dynamic content It will stop any dynamic page content being loaded and will stop any changes to your content being loaded by returning visitors (Obviously this second part is not such an issue with a 60 second cache).
The key thing to realise is that Your page is not one thing, it's made up of lots of parts, some of these parts should be called from cache (js, css, images, etc.) some should not (often html will fall into this category).
The browser will automatically call all the parts of your page from the cache where the cache has not expired.
Usually you would use .htaccess (or similar method) to cache your css, images, etc. (using versioning in filenames to force a reload when they change).
You should also take advantage of server side caching - codeigniter does this for whole pages but I dont tend to find this very helpful for any kind of dynamic site so I would take a look at for using phil sturgeons partial caching library for ci if you are interested in ss caching:
https://github.com/philsturgeon/codeigniter-cache
This wont stop a request being sent to the server but will mean that request requires less processing and can be served as one or several pieces of static content.

Related

ColdFusion application caches switch statement

I'm not familiar with caching in coldfusion, but it seems it's doing somethnig it's not supposed to in my website.
I have only one index page, that uses a big switch statement to determin what cfm files to include, to build my website's pages. No everything works fine, I even have a default case that refers back to the homepage when trying to access a non-existing page.
When I create a new page and try to go to it but in the meantime forgetting I need to add a cfcase first, goes to the defaultcase. If I then create the needed cfcase, it should work, but it has cached the redirect of the last time, the path it followed in the switch/case, so I still get the defaultcase. Even if remove the defaulcase from the code, it still goes there.
Is there anyway to tell coldfusion to stop caching my switch/case. The rest of the content may be chached, no problem, just not the path of the switch/case..
edit 1
Here's my code:
<html>
<head>
</head>
<body>
<cfswitch expression="#attributes.fuseaction#">
<cfcase value="home">
<cfinclude template="dsp_home.cfm" />
</cfcase>
<cfcase value="admin">
<cfinclude template="admin/dsp_login.cfm" />
</cfcase>
<cfdefaultcase>
<cf_goto fuseaction="home">
</cfdefaultcase>
</cfswitch>
</body>
</html>
attributes.fuseaction is a variable that is stored in the url of the requested page, like so: http://www.domain.com/index.cfm/fuseaction/#switch/case-variable#.
cf_goto is a custom tag that gives a 301 code and redirects to the specified page where that variable is home.
When I do what I described above, the headers still give me the 301 error code and de redirect to the default case page. So I'm at a loss what it is that's being cached here.
CF doesn't cache switch/case logic, so it's a red herring to be looking at that to solve whatever your problem actually is.
Do you - by any chance - have "Trusted Cache" switched on in CFAdmin? If so, you'll need to clear it so your CFM files recompile when they're requested, and your changes will take effect.
Failing that: we need to see your code, as per Duncan's suggestion.

How to expire browser cache without server side scripting?

I have a website made with html, css and images. My friends are seeing my website with an older version of css because their firefox browser cached the CSS. I told some of them to clear the cache and it looked fine.
But some of my friends are not technologically savvy and don't know how to clear the cache. Is there something I can put in my html code to force the website to expire the firefox browser cache if it is passed a certain date?
I do not have server side scripting abilities like perl, php, ruby etc...
If this is not possible, just say so.
put Meta HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE" in your head tag
or I like META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 22 Jul 2002 11:12:01 GMT"

Google Chrome Frame (GCF) problem in IE8 with cache

Maybe it has been asked somewhere, but I am trying to find my question and I am not able to find any answer.
Here's my question:
I am developing a web application and because of some major JavaScript issue in IE8, I need the user to run "Google Chrome Frame" (To enhance the speed of the web page). I was impressed that my page was working 100% fine until the time it was supposed to be refreshing and it wasn't refreshing (Ajax getJSON request using jQuery).
The problem is that it does not request the new data on the server, but it looks like it goes in the cache for the answer of that request and then return the same thing every time instead of new data.
I don't really know how to explain it, but it just does not update. Also, when I hit F5 on the page, it does not update the page, it keeps the old page (even if I hit CTRL-F5 or any other normal force-refresh button). To have the changes, I actually need to close the browser (IE8) and re-open it so it can take the new changes.
Is there anyone who know how I could disable the cache when Google Chrome Frame is active?
The meta tag I use is :
<meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache, must-revalidate">
<META HTTP-EQUIV="X-UA-COMPATIBLE" CONTENT="CHROME=1">
If you need any more details, don't hesitate to ask.
An old CGI trick would have been to encode the date as a parameter onto the request so the URL changes with each request. That generally stops any caching on the URL.
So you'd have url?01102010134532 if you encoded date and time down to miliseconds.
If I understand your requirement properly, you'd have to do this in JQuery / JS and would need to modify the parameter on the URL after each AJAX request was made, so the next one would be different to the previous

Old content appearing on site. Auto Clear Cache?

I have a website that is updated regularly and I am having a problem where old content is showing up on the page. It can be fixed by refreshing a few times or clearing the cache. I am looking for a solution so no data is stored on the PC and the site is forced to refresh each time. Perhaps an auto cache clear plugin or something of the like? Any ideas?
This may sound like a good idea at first but how many users do you hope to support in the future?
I ask because if every request should be completely refreshed every time you are going to have a LOT of traffic on your web server. And, your users are going to start complaining about page load times.
With help from tools like yslow and firebug, we've tried to analyze the portions of our pages that can be cached and those that can't. Tip of the iceburg, but...
Images to support site layout - backgrounds, buttons, etc. should be cached for a very long time. They go in a folder tree flagged by IIS as cachable for a long time. They could be delivered by a CDN long-term. If these have to change, we upload new files with new names.
Script/CSS and other, possibly-changing content goes in another folder that gets a shorter cache duration. This could be a problem if we have to fix bugs, but again, upload a new file with a new name if necessary.
Anything data-driven (our app is a catalog) is localized and gets refreshed every time.
This is still a work-in-progress for us, but we're seeing MUCH less server traffic and MUCH faster page load times.
I hope this helps!
use this, my son
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="expires" content="0">
Try putting this in your head tags
<meta http-equiv="cache-control" content="no-cache">
Edit Just a note, this doesn't force the browser not to cache it, but most browsers will listen
i suggest u to try this one , I use this way when my code still on production
<link rel="stylesheet" href="styel.css?v=<?= time(); ?>">
but when u're ready for live mode, put the latest version of ur css, or whatever u want like ?v=1.1
hope it will helps u

Refresh cache for specific image (avatar upload)

My site has an avatar upload feature that lets users upload an avatar. The problem is, if the user tries to upload another avatar, their browsers cache keeps showing their previous avatar until they do a hard refresh (Ctrl + F5) or clear their browser's cache.
Currently, this is what I do to display an avatar:
<img src="{{ secure_asset('storage/avatars/' . $user_id . '/avatar.png') }}" />
How can I refresh the cache for the avatar after the user uploads a new one so that it shows their latest avatar instead of their old one?
You could leverage "cache-busting". When you save the image, you can save a version number to the users table. So the first time a user saves an avatar, set a avatar_version column to 1. Increment the version number when uploading a new avatar. So when you grab it you can call
<img src="{{ secure_asset('storage/avatars/' . $user_id . '/avatar.png')."?version=".$user->avatar_version }}" />
If the GET parameter changes, it wont affect the file but the browser will recognize it as a different image and grab this new one.
caching is done by the browser and from the server side you don't have any control over browser caching functionality so you can not update users browser cache from the backend.
you can explicitly tell browser not to cache that specific web page by passing appropriate http headers. so that that particular page will always be loaded from the server rather than the cache.
for php you can use below headers
header('Cache-Control: no-cache');
header('Pragma: no-cache');
you can also do this with HTML as well
<meta http-equiv="Expires" content="Tue, 14 Aug 2017 12:12:12 GMT">
<meta http-equiv="Pragma" content="no-cache">

Resources