Template 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
├── tailwind.config.js # Tailwind CSS configuration
└── .postcssrc # PostCSS configuration for Parcel
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.gitcd recipes/template-asset-bundling -
Install Node.js dependencies:
npm install -
Install 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.go
Open 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.go
How 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.