admin管理员组

文章数量:1424893

I have a dynamically-generated javascript file that I want to ensure is NEVER cached by the browser. My current method is to simply append a (new) guid to the url in the script tag on each page view.

For example:

<script type="text/javascript" src="/js/dynamic.js?rand=979F861A-C487-4AA8-8BD6-84E7988BD460"></script>

My question is...is this the best way to acplish my goal?

For reference, I am using ASP.NET MVC 2 and the javascript file is being generated as a result of a controller action.

I have a dynamically-generated javascript file that I want to ensure is NEVER cached by the browser. My current method is to simply append a (new) guid to the url in the script tag on each page view.

For example:

<script type="text/javascript" src="/js/dynamic.js?rand=979F861A-C487-4AA8-8BD6-84E7988BD460"></script>

My question is...is this the best way to acplish my goal?

For reference, I am using ASP.NET MVC 2 and the javascript file is being generated as a result of a controller action.

Share Improve this question asked Jul 17, 2010 at 10:27 DanPDanP 6,4884 gold badges44 silver badges69 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 6

The method you have works just fine, an alternative would be to use DateTime.UtcNow.Ticks. You can of course use cache control headers, but if you want to be absolutely sure, the method you have is the way to go.

Though, if you're generating the file dynamically anyway, and need to include it in the page...any reason for not just sticking the JavaScript in the page? Since you want it to never cache, this will save a round-trip for the client.

You could create a custom action attribute:

public class NoCacheAttribute : ActionFilterAttribute
{  
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var cache = filterContext.HttpContext.Response.Cache;
        cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        cache.SetValidUntilExpires(false);
        cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        cache.SetCacheability(HttpCacheability.NoCache);
        cache.SetNoStore();
        base.OnResultExecuting(filterContext);
    }
}

And then decorate the controller action that generates javascript with this attribute:

[NoCache]
public ActionResult MyAction()
{
    ...
}

In this case you no longer need the random string you are appending.

You might consider using HTTP Etags

That's a usual way how people do it. You can also change the filename for each requests to be pletely sure.

something like <script src="{unique string}.js">

Yes that is good. I personally do the same but I use timestamp instead which makes sure that it is always unique.

Use the cache control HTTP headers in the response.

Cache-Control: no-cache

(This is therefore purely a server setting, no coding needed.)

Alternately, as you are using ASP.NET MVC, use routing to ignore the last element of the URL, in the HTML have

<script type="text/javascript" src="/js/dynamic.js/979F861A-C487-4AA8-8BD6-84E7988BD460"></script>

and use the MVC route to ignore the suffix (i.e. make every HTML page served have a different script URL, not just a query parameter).

本文标签: aspnet mvcHow can I ensure a dynamicallygenerated javascript file is never cachedStack Overflow