Assets

Embedded Assets

Register embedded public files and serve them from the application binary.

By Guillermo Alvarez - Published - Updated

Embed views and public files

Application files are embedded from app/files.go:

//go:embed views public
var Files embed.FS

var Views = lazyapp.MustSub(Files, "views")
var Public = lazyapp.MustSub(Files, "public")

The public directory can include stylesheets, images, generated JavaScript, importmaps, and static error pages.

Register public files

Pass the embedded public file system to lazyapp.New:

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

lazyapp registers those files with lazyassets, installs asset helpers, and serves public files through the app handler.

Templates link logical paths:

{{stylesheet "/styles.css"}}
<img src="{{asset_path "/images/logo.svg"}}" alt="Logo">

GoLazy maps the logical path to the correct permanent fingerprinted path when one exists.

Keep generated outputs committed

Generated files under app/public are embedded like any other public file:

app/public/assets/importmap.json
app/public/assets/lazyshaft/app/app-<hash>.js
app/public/styles.css

Run the generating command before tests and production builds.