Cache busting using Scripts.Render but without a url

When using Bundles with EnableOptimisations set to true, the output URL contains a hash so that the browser cannot load the file from the cache if it has changed.

@Scripts.Render("~/content/js/global") 

Outputs:

 <script src="/content/js/global?v=PqstRRGF8qsUsJfHu6NBBBp6eDxYBz1JCbHY6CQJVks3"></script> 

However, some files in our application refer to only one page. We directly refer to them:

 @Scripts.Render("~/areas/areaname/content/js/page-name.js") 

The output URL in this case has no hash and therefore is cached, which causes problems when releasing changes to these files.

We do not change each link individually or do not have to manually change the URLs each time we change files.

How can I globally add a hash (or version number) to all script and stylish urls not ?

+5
source share
1 answer

I got a completely different solution, but I came across a question containing an idea that would help me and hope that this helps others.

You can manually set the default tag formats and include the version number.

 string versionNumber = "1.2.3.4"; // get from assembly or config setting Styles.DefaultTagFormat = string.Format("<link href='{{0}}?v={0}' rel='stylesheet'/>", versionNumber); Scripts.DefaultTagFormat = string.Format("<script src='{{0}}?v={0}'></script>", versionNumber); 

The only problem I see is that your URL already contains the request. Will this add a second ? , not & .

+5
source

Source: https://habr.com/ru/post/1243147/


All Articles