doudao8283 2016-01-01 17:54
浏览 79
已采纳

Go HTML模板中的自动资产修订文件名

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?
  • 写回答

2条回答 默认 最新

  • douzhicui2209 2016-01-01 19:06
    关注

    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 }}" />
    

    <kbd>playground example</kbd>

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?