I am trying to use the data-lift="with-resource-id" parameter to the tag as described in the Lift Cookbook (http://cookbook.liftweb.net/#AvoidAssetCaching) to avoid asset caching in the browser. I've copied the code sample provided in the cookbook and modified it to my environment in order to introduce a random value into the parameter path.
My assets are stored in two root directories -- one called "css" and one called "js" for css and javascript respectively.
My code looks like:
import net.liftweb.http._
import net.liftweb.util._
object AssetCacheBuster {
def init() : Unit = {
val resourceId = Helpers.nextFuncName
LiftRules.attachResourceId = (path: String) => {
val PathRegex = """\/cached(\/css\/|\/js\/)(\S+)""".r
try {
val PathRegex(root, rest) = path
"/cached" + root + resourceId + "/" + rest
} catch {
case e: scala.MatchError => path
}
}
// Remove the cache/{resourceId} from the request if there is one
LiftRules.statelessRewrite.prepend( NamedPF("BrowserCacheAssist") {
case RewriteRequest(ParsePath("cached" :: "css" :: id :: file :: Nil, suffix, _, _), _, _) =>
RewriteResponse("css" :: file :: Nil, suffix)
case RewriteRequest(ParsePath("js" :: id :: file :: Nil, suffix, _, _), _, _) =>
RewriteResponse("js" :: file :: Nil, suffix)
})
}
}
I embed the css files, for example, with a call like:
<link data-lift="with-resource-id" rel="stylesheet" type="text/css" href="/cached/css/standard.css" />
The way I expect it to work is that attachResourceId logic will recognize embedded css files by the path "/cached/css" and inject a unique value in the path. So, for example, /cached/css/standard.css becomes /cached/css/F7017951738702RYSX0/standard.css. By inspecting elements using Chrome, I can see that this is indeed occurring, so I believe this is working as expected.
In the rewriting logic at the bottom, I expect it to look for requests that start with "/cached/css" and remove the /cached and unique id components. By tracing in the debugger, this too seems to be working. In the debugger I can see the the resulting url it is trying to rewrite is "/css/standard.css". And I can verify that if I enter this value in my browser URL, that content does indeed get served. Yet, the browser is showing an error (which I can see via Chrome's console) that the .css file is not found.