Documentation ¶
Overview ¶
Package staticfiles is an asset manager for versioning static files in web applications.
It collects asset files (CSS, JS, images, etc.) from a different locations (including subdirectories), appends hash sum of each file to its name and copies files to the target directory to be served by http.FileServer.
This approach allows to serve files without having to clear a CDN or browser cache every time the files was changed. This also allows to use aggressive caching on CDN and HTTP headers to implement so called "cache hierarchy strategy" (https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#invalidating_and_updating_cached_responses).
Index ¶
Constants ¶
const ManifestFilename string = "staticfiles.json"
Manifest file name. It will be stored in the Storage.OutputDir directory.
const ManifestVersion int = 1
Variables ¶
var ErrManifestVersionMismatch = errors.New("manifest version mismatch")
Functions ¶
func PostProcessCSS ¶
func PostProcessCSS(storage *Storage, file *StaticFile) error
PostProcessCSS fixes files references in CSS files to point to the hashed versions of the files in the following cases:
@import "path/file.ext" url("path/file.ext") sourceMappingURL=file.ext.map
Types ¶
type ManifestScheme ¶
Manifest contains mapping of the original relative file paths to the storage relative file paths.
type PostProcessRule ¶
type PostProcessRule func(*Storage, *StaticFile) error
PostProcessRule describes the type of a post-process rule functions.
type StaticFile ¶
type Storage ¶
type Storage struct { OutputDir string FilesMap map[string]*StaticFile OutputDirList bool Enabled bool Verbose bool // toggles verbose output to the standard logger // contains filtered or unexported fields }
func NewStorage ¶
NewStorage returns new Storage initialized with the root directory and registered rule to post-process CSS files.
func (*Storage) AddIgnorePattern ¶ added in v0.4.0
func (*Storage) AddInputDir ¶
func (*Storage) CollectStatic ¶
CollectStatic collects files from the Storage.inputDirs (including subdirectories), appends hash sum of each file to its name, applies post-processing rules and copies files and manifest to the Storage.OutputDir directory.
func (*Storage) Open ¶ added in v0.2.0
Open implements http.FileSystem interface to be used primarily in http.FileServer
func (*Storage) RegisterRule ¶
func (s *Storage) RegisterRule(rule PostProcessRule)