Skip to content

tmthrgd/vfshash

Repository files navigation

vfshash

GoDoc Build Status

A package to make a http.FileSystem content-addressable for use with shurcooL/vfsgen. It adds a truncated cryptographic digest to file names.

This package is useful for giving web assets content-addressable URLs which can safely be used with long cache times (a year or more).

Usage

import "go.tmthrgd.dev/vfshash"

Follow the usage instructions for shurcooL/vfsgen and wrap the fs with NewFileSystem to generate a content-addressable file system.

var fs http.FileSystem = http.Dir("/path/to/assets")

fs = vfshash.NewFileSystem(fs)

err := vfsgen.Generate(fs, vfsgen.Options{})
if err != nil {
	log.Fatalln(err)
}

Embedded in the file system is a manifest that maps the original names for files to their content-addressable equivalents. This can be accessed with the AssetNames API.

names := vfshash.NewAssetNames(assets)

// returns /example-deadbeef.css
names.Lookup("/example.css")

// returns /does.not.exist.txt as is
names.Lookup("/does.not.exist.txt")

// opens /example-deadbeef.js
names.Open("/example.js")

AssetNames implements http.FileSystem so it can be passed to http.FileServer to serve assets with their original names.

If the http.FileSystem passed to NewAssetNames doesn't contain the manifest, Lookup will return the name as is. This makes development easier as a regular http.Dir can be passed in without problem.

License

BSD 3-Clause License

About

A companion to shurcooL/vfsgen that adds cache busting hashes to file names.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages