سؤال

Can browser cache cleanup for my domain be initiated from the server?

I see many people suggesting that clearing browser cache using JavaScript could cause security challenges. But what if I clear browser cache related to only my domain (www.example.com) from the server where the site is deployed?

My requirement is that I should be able to clear .js, .css and .jpg files on demand from the browser cache. My current framework is not setup to implement token based querystring such that every server side change reflects in the url of these resources, making browser to believe it is a new resource. Of course I can do all the hard work to change my framework to reflect this change but was wondering if there is already an established way of achieving this.

If it matters, then my environment is based on Windows platform (ASP.Net MVC, IIS).

هل كانت مفيدة؟

المحلول

Of course I can do all the hard work to change my framework to reflect this change but was wondering if there is already an established way of achieving this.

No, there is no need for you to do all the hardwork. Microsoft ASP.Net Web Optimization framework will take care of it. The only think you need to do is to create proper bundles and register them in Global.asax.

Creating a bundle -

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        ScriptBundle bundle = new ScriptBundle("~/bundles/jquery");
        bundle.Include("~/Scripts/jquery-{version}.js");
        bundle.Include("~/Scripts/_references.js");

        bundles.Add(bundle);
    }
}

And then register in Global.asax -

BundleConfig.RegisterBundles(BundleTable.Bundles);

When rendered in the browser, JQuery bundle will be having version in its URL.

<script src="/bundles/jquery?v=PsWsn1syKMUFBUcJusJPfq6HqYqANhiRR1uZJvzFJ-c1"></script>

And if you change some code in the JQuery files, then a new version number will be alloted to the bundle.

This approach will take care of browser getting updated files on changes.

نصائح أخرى

Use .htaccess to prevent browser cache

 <FilesMatch "\.(html|htm|js|css)$">
   FileETag None
  <ifModule mod_headers.c>
   Header unset ETag
   Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
   Header set Pragma "no-cache"
   Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifModule>

i hope this will helps you .htaccess!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top