Assets

Hashing And Fingerprints

Use logical asset paths, content fingerprints, ETags, and integrity values.

By Guillermo Alvarez - Published Updated

Register public files

Startup passes embedded public files to lazyapp.New:

return lazyapp.New(lazyapp.Config{
    Name:   "sample_app",
    Drawer: Draw,
    Public: app.Public,
    Views:  app.Views,
})

Public files are registered with lazyassets and served after application route lookup.

Every registered asset keeps its logical path and receives a content-hashed permanent path:

/styles.css
/styles-<hash>.css

Use helpers so templates link the permanent path:

{{stylesheet "/styles.css"}}

For custom tags, call asset_path directly:

<script type="module" src="{{asset_path "/assets/app.js"}}"></script>

Add integrity when needed

The registry also computes a SHA-256 integrity value:

<link
  rel="stylesheet"
  href="{{asset_path "/styles.css"}}"
  integrity="{{asset_integrity "/styles.css"}}">

Use this when an asset tag should carry a browser-checkable integrity attribute.

Understand cache behavior

The logical path uses a revalidation-friendly cache policy and an ETag. The permanent path uses immutable caching because the URL changes when the bytes change.

GET /styles.css          # logical path, revalidate
GET /styles-<hash>.css   # permanent path, immutable

Stylesheets can also rewrite local url(...) references to registered permanent asset paths.

Serve from another origin

Configure a public asset base URL when assets are served from another domain or port:

return lazyapp.New(lazyapp.Config{
    Public: app.Public,
    AssetOptions: []lazyassets.Option{
        lazyassets.WithBaseURL("http://127.0.0.1:8888/buckets/assets"),
    },
})

Helpers such as asset_path, stylesheet, and CSS url(...) rewriting then return absolute URLs on that origin. The app still registers and can serve embedded assets locally; the base URL only changes generated links.

Generate assets through the registry

Generated sources use the same registry:

lazyassets.SourceFunc(func(registry *lazyassets.Registry) error {
    return registry.Add(
        "/assets/app.js",
        []byte("console.log('ready')"),
        lazyassets.ContentType("text/javascript"),
    )
})

Generated assets receive the same fingerprints, ETags, integrity values, cache policy, helper lookup, and unpack behavior as embedded public files.