Search code examples
templatesgoassets

Automatic asset revision filenames in Go HTML templates


I'm looking for help on implementing something that automatically includes versioned filenames in a Go HTML template. For example, in my template I have something like this in the head:

<link rel="stylesheet" href="{{ .MyCssFile }}" />

The stylesheets themselves have a chunk of MD5 hash appended to the name from a gulp script called gulp-rev

stylesheet-d861367de2.css

The purpose is to ensure new changes are picked up by browsers, but also allow caching. Here is an example implementation in Django for a better explanation:

https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#manifeststaticfilesstorage

A subclass of the StaticFilesStorage storage backend which stores the file names it handles by appending the MD5 hash of the file’s content to the filename. For example, the file css/styles.css would also be saved as css/styles.55e7cbb9ba48.css.

The purpose of this storage is to keep serving the old files in case some pages still refer to those files, e.g. because they are cached by you or a 3rd party proxy server. Additionally, it’s very helpful if you want to apply far future Expires headers to the deployed files to speed up the load time for subsequent page visits.

Now I'm wondering how to best pull this off in Go? I intend to serve the files from the built in file server.

My current thoughts are:

  • Have a loop that checks for the newest stylesheet file in a directory. Sounds slow.
  • Do some kind of redirect/rewrite to a generically named file (as in file.css is served on a request to file-hash.css).
  • Have Go manage the asset naming itself, appending the hash or timestamp.
  • Maybe its better handled with nginx or something else?

Solution

  • Write a template function to resolve the name. Here's an example template function:

    func resolveName(p string) (string, error) {
      i := strings.LastIndex(p, ".")
      if i < 0 {
        i = len(p)
      }
      g := p[:i] + "-*" + p[i:]
      matches, err := filepath.Glob(g)
      if err != nil {
        return "", err
      }
      if len(matches) != 1 {
        return "", fmt.Errorf("%d matches for %s", len(matches), p)
      }
      return matches[0], nil
    }
    

    and here's how to use it in a template when registered as the function "resolveName":

    <link rel="stylesheet" href="{{ .MyCssFile | resolveName }}" />
    

    playground example

    This function resolves the name of the file every time the template is rendered. A more clever function might cache names as they are resolved or walk the directory tree at startup to prebuild a cache.