cachinggrailsrequirejsexpires-header

In Grails, disable name hashing but keep expires header for hashandchange resources plugin?


I am using grails resources plugin. On client I am using require.js to fetch js.

my require.js config -

baseUrl: '/js/lib', 

With resources plugin enabled -

  1. browser would make request for /js/lib/abc.js wasting ~300ms
  2. On reaching server it will be redirected to /static/2432yi4h32kh4232h4k2h34ll.js
  3. Browser will find this file in its cache and serve it.

So I disabled cached-resources plugin using -

grails.resources.mappers.hashandcache.excludes = ['**/*.js']

and new require.js config -

baseUrl: '/static/js/lib',    
urlArgs: "bust=" + application_version,

Removing cached-resources solved redirect issue but also removed the expires header which was being set for js files causing browsers to not cache js files at all.

How can I only disable the name hashing in cached-resources and keep the expires headers it sets. Otherwise, are there any plugins for Grails I can use to set these headers and they work well with Resources plugin.

I am using Tomcat and Haproxy to serve content.


Solution

  • I think best solution is to put the hashed js file name in require definition, not the original clear name.

    You can echo the hashed name using the resource external tag

    <r:external uri="js/custom.js"/>
    <script type="text/javascript">
        var urlOfCSSToLoadInJSCode = '${r.external(uri:"css/custom.css").encodeAsJavaScript()}';
    </script>
    <r:external uri="icons/favicon.ico"/>