Grails Asset Pipeline: URLs with cache digest in file name within static assets

547 views Asked by At

I'm using the Grails Asset Pipeline 2.2.5 plugin on Grails 2.5.0 (the current 2.x version for both).

I am serving static HTML, JS, CSS, and image files from Amazon S3 (might move to CloudFront later, but that shouldn't affect this question), and I serve REST endpoints backed by controllers from my Grails servers.

If, e.g., I have a static HTML asset that includes an img element whose src attribute value is a URL to a static image asset, how can I make the URL include the correct cache digest fingerprint in the file name?

I shouldn't have to run any Groovy / Java / JavaScript at runtime to get correct URLs including cache digests; I should be able to calculate them at build time, and somehow write out URLs in the static files at build time including the cache digests.

Will any of the following work? Each option is followed by an example of what pre-build code would look like. For all of the options, for a cache digest 1234567890abcdef, the output should be:

<img src="abc-1234567890abcdef.jpg"/>
  1. Is Asset Pipeline smart enough to find static asset URLs in HTML and automatically rewrite them? I've verified that Asset Pipeline will rewrite relative URLs in CSS files, and insert cache digests in the file names, but I haven't found a way to get it to rewrite URLs in HTML files.

    <img src="abc.jpg"/>
    
  2. Is there a placeholder that I can add to HTML to have Asset Pipeline automatically insert the cache digest?

    <img src="abc%cache-digest-placeholder%.jpg"/>
    
  3. Can I use some other format, like GSP or GTPL (though GTPL seems to only work for JS & CSS, not HTML), to somehow figure out the URL for the image at build time, rather than at runtime (GSP would normally be evaluated at runtime, but that shouldn't be necessary to determine the cache digest of a static file; it should be known at build time)

    <img src="${assetPath(src: 'abc.jpg')}"/>
    

If it will be difficult to rewrite URLs to include cache digests, how can I turn off inserting cache digests to the names of assets?

0

There are 0 answers