1

Simple question.

Do browsers cache PHP generated CSS and script files automatically, just like CSS/JS files?

1
  • 1
    Use firebug or the webkit debugger and it will tell you whether the browser is using a cached version or not. Commented Aug 28, 2012 at 17:21

3 Answers 3

2

Sure, barring explicit acts to prevent caching. The browser has no way of knowing if the file was a static or dynamically generated resource.

Sign up to request clarification or add additional context in comments.

4 Comments

I have no idea, but couldn't it also work the other way around? That is, do browsers cache files if they are not explicitly permitted in the HTTP headers?
It is not always true, especially when using sessions because PHP will send a Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0.
@MathieuImbert: "barring explicit acts to prevent caching". Sending no-cache headers is one means of preventing caching.
@EricJ. Yes sorry I missed your first sentence.
1

If the URL remains the same, and there aren't hints in the HTTP responses to tell the browser otherwise, they can be cached.

If the URL includes dynamic information, the browser probably won't be able to take advantage of caching.

Changing the URL by adding a timestamp as a dummy parameter (e.g. http://host/myfile.php?t=17279273) is one of the ways you can prevent caching since the browser sees the slight change as a new resource.

2 Comments

Nice technique on the timestamp!
even better: add the timestamp of the last file modification, so the resource remains cached as long as no change occurs (ex. in php: $url="http://host/myfile.php?t=".filemtime("myfile.php");
1

Jonathon's answer suggesting the addition of a timestamp to prevent caching is a good one.

A useful tip along these lines is to append the creation/last modified date of a file. Doing this means that while unchanged the browser will cache the file, but when you update the file those changes are forced to your users.

It's not always the best option, but worth noting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.