0

I would like user's browser to cache external .css and .js files. However, each time I made changes, I would want user's browser to be able to load the updated external files instead of using the previous cache.

I found out that by adding parameter when including ext .css and .js files will help. But when I did this, I found out that browser no longer cached the files but kept on loading the external files each time I visited the page. May I know how I can solve this?

1
  • It's mostly a matter of headers, including entity tags ("Etag"). Commented Jul 25, 2010 at 14:26

3 Answers 3

4

Basically you have 2 options:

  • Use Etag. If you hosting provider allows it you could configure Etag in a .htaccess file: <Directory /path/to/directory>
    FileETag INode MTime Size
    </Directory>

  • Not an parameter, add the version as a part of the filename. E.g.: style_10.css

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

1 Comment

hippo, may i know how to validate css and js external files with etag? tried insert header etag in php file but it seems browser can't detect changes in js file when i update it and need to refresh browser then only can see changes. Can u pls give me any example that i can refer too? I am new to this.. thanks
2

To understand caching I recommend Mark Nottingham's caching tutorial: http://www.mnot.net/cache_docs/.

If you set the proper "Expires" headers the files will be cached even if you add dynamic URL parameters. For that is indeed the most efficient way to handle your problem.

Comments

0

Most web servers will use the files' modified dates to report whether files have changed to the browser. And browsers should correctly ask for files with an If-Modified header so that cached copies will be used.

Are you sure the browser isn't already doing the right thing?

1 Comment

yeah.. i have tried it. after updates css file, the layput of my website was inconsistent with the updated css file till i refresh my browser.. you have any idea how to solve this prob?

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.