A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

Anders Hattestad
Oct 13, 2011
  4272
(0 votes)

Use client cache for resource files (css/js/images) without hassle

I usually creates a virtual path (VirtualPathNativeProvider)  that all my css, images and javascript files is placed in. One reason for this is that later the editors have access to these files with out having to access the server.

The problem comes of course when these files changes. If the current VPP folder have set

Code Snippet
  1. <staticFile expirationTime="4.0:0:0" />

the web browser will not get these new files before they force a cache reset. One way around this “problem” is to not cache it on the client. But that have some downsides.

My solution is to create my own VPP file system based on the VirtualPathNativeProvider.

The whole concept is that I remove a part of the path that contains Changed_ like this

Code Snippet
  1. Regex filter = new Regex("/Changed_[^/]*/");
  2. string FilterCacheKeyPath(string virtualPath)
  3. {
  4.     virtualPath = filter.Replace(virtualPath, "/");
  5.     return virtualPath;
  6. }

Then I replace all the methods to use the striped version of the path/filename

Code Snippet
  1. public class FilSystemWithCacheKey : EPiServer.Web.Hosting.VirtualPathNativeProvider
  2. {
  3.     public FilSystemWithCacheKey(string name, NameValueCollection configParameters)
  4.         : base(name, configParameters)
  5.     {
  6.     }
  7.  
  8.     public override bool FileExists(string virtualPath)
  9.     {
  10.         return base.FileExists(FilterCacheKeyPath(virtualPath));
  11.     }
  12.     public override bool DirectoryExists(string virtualPath)
  13.     {
  14.         return base.DirectoryExists(FilterCacheKeyPath(virtualPath));
  15.     }
  16.     public override System.Web.Hosting.VirtualFile GetFile(string virtualPath)
  17.     {
  18.         return base.GetFile(FilterCacheKeyPath(virtualPath));
  19.     }
  20.     public override System.Web.Hosting.VirtualDirectory GetDirectory(string virtualPath)
  21.     {
  22.         return base.GetDirectory(FilterCacheKeyPath(virtualPath));
  23.     }
  24.     public override EPiServer.Security.AccessLevel QueryAccess(string virtualPath, System.Security.Principal.IPrincipal user)
  25.     {
  26.         return base.QueryAccess(FilterCacheKeyPath(virtualPath), user);
  27.     }
  28.     Regex filter = new Regex("/Changed_[^/]*/");
  29.     string FilterCacheKeyPath(string virtualPath)
  30.     {
  31.         virtualPath = filter.Replace(virtualPath, "/");
  32.         return virtualPath;
  33.     }

Then the fun begins Smile

I created a method that have path and filename as input

  1. %=FilSystemWithCacheKey.GetCachePath("/Framework/","styles/screen.css") %>

That will return a path with the last changed inside the first path.

image

Code Snippet
  1. public static string GetCachePath(string path, string filename)
  2. {
  3.     var newUrl = HttpContext.Current.Cache["VersionPath_" + path + filename] as string;
  4.     if (newUrl == null)
  5.     {
  6.         VirtualPathHandler instance = VirtualPathHandler.Instance;
  7.         var dir = instance.GetDirectory(path, false) as NativeDirectory;
  8.         var dirs = new List<string>();
  9.         dirs.Add((dir as NativeDirectory).LocalPath);
  10.         newUrl = path + "Changed_" + LastAccessed((dir as NativeDirectory).LocalPath, dirs) + "/" + filename;
  11.         HttpContext.Current.Cache.Insert("VersionPath_" + path + filename, newUrl, new CacheDependency(dirs.ToArray()));
  12.     }
  13.     return newUrl;
  14. }
  15. public static string LastAccessed(string path, List<string> paths)
  16. {
  17.     Stack<DirectoryInfo> dirs = new Stack<DirectoryInfo>();
  18.     FileInfo mostRecent = null;
  19.  
  20.     dirs.Push(new DirectoryInfo(path));
  21.  
  22.     while (dirs.Count > 0)
  23.     {
  24.         DirectoryInfo current = dirs.Pop();
  25.  
  26.         Array.ForEach(current.GetFiles(), delegate(FileInfo f)
  27.         {
  28.             if (mostRecent == null || mostRecent.LastWriteTime < f.LastWriteTime)
  29.                 mostRecent = f;
  30.         });
  31.  
  32.         Array.ForEach(current.GetDirectories(), delegate(DirectoryInfo d)
  33.         {
  34.             paths.Add(d.FullName);
  35.             dirs.Push(d);
  36.         });
  37.     }
  38.     return mostRecent.LastWriteTime.ToString("ddMMyyyy_HHmmss"); ;
  39. }

I add this to the Cache and add dependencies to all sub directories to the path.

since all my css links to the images relative to the path all changed to the /Framework/ files will force a new path and therefore a new client cache.

Oct 13, 2011

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP: Learning Optimizely Just Got Easier: Introducing the Optimizely Learning Centre

On the back of my last post about the Opti Graph Learning Centre, I am now happy to announce a revamped interactive learning platform that makes...

Graham Carr | Jan 31, 2026

Scheduled job for deleting content types and all related content

In my previous blog post which was about getting an overview of your sites content https://world.optimizely.com/blogs/Per-Nergard/Dates/2026/1/sche...

Per Nergård (MVP) | Jan 30, 2026

Working With Applications in Optimizely CMS 13

💡 Note:  The following content has been written based on Optimizely CMS 13 Preview 2 and may not accurately reflect the final release version. As...

Mark Stott | Jan 30, 2026

Experimentation at Speed Using Optimizely Opal and Web Experimentation

If you are working in experimentation, you will know that speed matters. The quicker you can go from idea to implementation, the faster you can...

Minesh Shah (Netcel) | Jan 30, 2026

How to run Optimizely CMS on VS Code Dev Containers

VS Code Dev Containers is an extension that allows you to use a Docker container as a full-featured development environment. Instead of installing...

Daniel Halse | Jan 30, 2026

A day in the life of an Optimizely OMVP: Introducing Optimizely Graph Learning Centre Beta: Master GraphQL for Content Delivery

GraphQL is transforming how developers query and deliver content from Optimizely CMS. But let's be honest—there's a learning curve. Between...

Graham Carr | Jan 30, 2026