recipes/template-asset-bundling
Template Asset Bundling
Setting up a Go application with template rendering and asset bundling.
This example demonstrates how to integrate asset bundling into a Go web application using Fiber, gofiber/template for HTML template rendering, Tailwind CSS for utility-first styling, and Parcel as a zero-configuration asset bundler. Parcel processes and hashes the CSS assets, which are then served as static files by Fiber.
Prerequisites
Ensure you have the following installed:
Project Structure
template-asset-bundling/
βββ app.go # Fiber application entry point
βββ handlers/
β βββ handlers.go # Route handlers (Home, About, NotFound)
βββ views/
β βββ layouts/
β β βββ main.html # Base layout template
β βββ partials/ # Reusable template partials
β βββ index.html # Home page template
β βββ about.html # About page template
β βββ 404.html # Not found template
βββ assets/
β βββ app.css # Tailwind CSS source (input)
βββ public/
β βββ assets/ # Compiled assets output (git-ignored)
βββ package.json # Node dependencies and npm scripts
βββ .postcssrc # PostCSS configuration for ParcelSetup
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/template-asset-bundlingInstall Node.js dependencies:
npm installInstall Go dependencies:
go mod download
Running
Development
Run the asset watcher and the Go server in separate terminals:
# Terminal 1 β watch and rebuild assets on change
npm run dev
# Terminal 2 β start the Fiber server (template hot-reload enabled)
go run app.goOpen http://localhost:3000 in your browser.
Production
Build optimized assets first, then run the server with APP_ENV=production to disable template hot-reloading:
npm run build
APP_ENV=production go run app.goHow It Works
npm run devruns Parcel in watch mode, compilingassets/app.css(Tailwind source) into hashed output files underpublic/assets/.- The
getCssAssettemplate function walkspublic/assets/at render time to find the correct hashed filename and injects the<link>tag automatically. - In development (
APP_ENVis notproduction),engine.Reload(true)re-parses templates on every request so changes are reflected without restarting the server.