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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?