Question

I've noticed a solution that might help you to control caching of javascript files.

There is a method SPUtility.MakeBrowserCacheSafeLayoutsUrl. MSDN says this method: returns a new URL that will not be cached by the browser when the associated file changes.

It sounds awesome. I've tried using this method like so:

 <script type="text/jscript" src="<%=SPUtility.MakeBrowserCacheSafeLayoutsUrl("projectName/scripts/plugins/tapes_report_definition.js", false)%>" ></script>

So, it worked. It generated ?rev={MD5Hash_of_my_file.js} part at the end of the url. But after I changed this .js file, new MD5 hash did not get generated. ?rev part remained the same. I've tried iisreset, redeploy etc, but nothing helped. I've checked this method with Reflector and learned that MakeBrowserCacheSafeLayoutsUrl does not generate MD5 hash all the time, but gets it from internal SPVolitileCache object.

I thought I could clear this cache by deleting all files inside of these two folders:

  1. %APPDATA%\Microsoft\Web Server Extensions\Cache
  2. %USERPROFILE%\AppData\Local\Microsoft\WebsiteCache

Deletion did not help.SPUtility.MakeBrowserCacheSafeLayoutsUrl still returns the same value after javascript file is changed.

I've got three questions:

  1. How do I refresh SPVolitileCache after all?
  2. How do you force SPUtility.MakeBrowserCacheSafeLayoutsUrl generate new ?rev= without purging cache?
  3. Considering all above, does it even make sense to use SPUtility.MakeBrowserCacheSafeLayoutsUrl?

Any additional information will be greatly appreciated!

Was it helpful?

Solution

I've checked it again and I confirm that this method is totally viable. The URL of a file gets refreshed after iisreset. This is exactly what I wanted to see.

OTHER TIPS

I would like to add this just as a comment but stackexchange won't allow that. So, here is my answer: For anyone looking to solve the issue of cached script elements inside a Content Editor web part (or a JSLink to a .js file), know that the SPUtility.MakeBrowserCacheSafeUrl method is a SERVER ONLY method and must be embedded on an ASPX page. Be sure you understand that you cannot just plop the code into a .js file or your Content Editor web part and have it function. It will not.

That being said, the method really is pretty worthless because you might as well just use the ScriptLink tag. ScriptLink will add the ?rev for you. The only time you might be tempted to use this method is if you are embedding script tags into a page which you should never do. That totally goes against the entire SOD framework and best practices and will likely not work with MDS.

The reason I found this page was because I have an InfoPath Form Web Part on a page and the previous developer used a separate .js file to make some buttons for it. The .js file for the buttons is referenced in a Content Editor web part. This is probably a common scenario but it is problematic because the file reference does not have a trailing ?rev unless added manually. Unless there is an actual client-side method that helps to generate some sort of ?rev, and I don't see how there could be, my vote is that this method is useless.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top