Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Force browser to reload cached static file with versioning?


After deploying a new version of a website the browser loads everything from its cache from the old webpage until a hard, force refresh is done.

In ASP.NET MVC if the file becomes in Bundle, it handled by Optimization framework. a version added to your file link, and if a change occurs in your bundle's file a new token generate. follow below code :

for example, js file name is: datatables

when you put it in a bundle with the same name, you will see the

datatables?v=anY9_bo7KitrGnXQr8ITP3ylmhQe9NDzSjgLpLQWQFE1

as a file name. change datatables and watch again the name of the file in the browser, surely it will change:

datatables?v=r8yhQBxKyDgrOGyqr1ndtdG92Ije09nqTY7yogrOSTk1

But there's two questions:

  • What we can do if our file wasn't in Bundle?
  • Is a way to force the browser to refresh cache?
like image 694
Hamed Moghadasi Avatar asked Jun 18 '17 11:06

Hamed Moghadasi


People also ask

How do I force the browser to reload cached JS?

It works, all the time, for everyone. But, if you're not using it, and you just need to reload that one CSS or JS file occasionally in your own browser... just open it in its own tab and hit SHIFT-reload (or CTRL-F5)!

Does Chrome cache static files?

The browser caches static files. When testing, it's useful to disable the cache. In chrome, go to developer tools, click on the Network tab, then check "Disable cache".

How do I force a browser to reload CSS?

General solutionPressing Ctrl + F5 (or Ctrl + Shift + R ) to force a cache reload.


2 Answers

Assuming you cannot use bundling for some reason, the solution suggested by the original poster is good enough, however it's better to put the logic inside a helper method.

It makes the code testable, it helps to change the logic without changing .cshtml , and also helps to not repeat the filename twice. Then you can have have a much cleaner code:

<script src="@Url.ContentWithVersion("~/scripts/main.js")"></script>

To do so, you can add ContentWithVersion extension method to the existing UrlHelper:

using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
public static class UrlHelperExtensions
{
    public static string ContentWithVersion(this UrlHelper urlHelper, string path)
    {
        if (urlHelper == null)
            throw new ArgumentNullException(nameof(urlHelper));
        var result = urlHelper.Content(path);
        var file = HttpContext.Current.Server.MapPath(path);
        if (File.Exists(file))
            result += $"?v={File.GetLastWriteTime(file).ToString("yyyyMMddHHmmss")}";
        return result;
    }
}
like image 161
Reza Aghaei Avatar answered Oct 01 '22 01:10

Reza Aghaei


we have one solution with some different way for implementation. we use above solution for it.

datatables?v=1

we can handle the version of the file, it's mean that every time that we change our file, change the version of it too. but it's not a suitable way.

another way used Guide, it wasn't suitable too, because each time it fetches the file and doesn't use from the browser cache.

datatables?v=Guid.NewGuid()

The last way that is the best Way is :

when file change occur , change version too. check follow code :

<script src="~/scripts/[email protected](Server.MapPath("/scripts/main.js")).ToString("yyyyMMddHHmmss")"></script>

by this way, when you change the file, LastWriteTime change too, so the version of the file will change and in the next when you open the browser, it detects a new file and fetch it.

like image 31
Hamed Moghadasi Avatar answered Oct 01 '22 01:10

Hamed Moghadasi