While working for one of my clients, we discovered that Lift 3.x is placing a page.js file on partial templates loaded into the page. We discovered the problem because occasionally we found our app in a state where every angular template load fails to find its corresponding page.js. For the most part, everything we've observed is harmless but it's less than ideal. We're fetching unneeded assets, and I have my concerns that the code we place in page.js never expects other page.js files to get run in the same page load.
I added the following transform in out Boot.scala to prevent our partial HTML templates from having the page.js file:
// Lift 3.x slaps a <script> tag referencing a page.js on all HTML responses, including our angular templates
// This is a work around to avoid having the client pull an unneeded js file and avoid possible 404's.
LiftRules.responseTransformers.append( res => {
import net.liftweb.util.Helpers._
(S.request.map(_.uri), res) match {
case (Full(uri), html: XhtmlResponse) if uri.startsWith("/weave/templates/") =>
val stripped = ("script" #> "").apply(html.out).headOption.getOrElse(html.out)
html.copy(out = stripped)
case _ =>
res
}
})
This workaround isn't ideal for several reasons.
Firstly, it assumes that there are ZERO valid script tags in the directory. This was safe in our case fortunately because the directory ONLY contains partial HTML and no script tags of our own. Just beware of this if you want to apply this code to your own project.
Secondly, Lift is doing all of the work and bookkeeping to add this script file just for us to do more work and throw it away.
I feel like Lift shouldn't assume that an HTML uri needs page.js slapped in there, although that's a good default behavior. I'd like to be able to control this behavior with LiftRules. Any objections or concerns about doing so? And what would be the best way to expose this feature? My initial thought is to create a setting with type Req => Boolean that answers the question "Should this request's HTML response have a page.js added?" and by default always answers "true" to achieve our current behavior.
Looking forward to the feedback!
Thanks,
Joe