recipes/template-asset-bundling

Template Asset Bundling

Setting up a Go application with template rendering and asset bundling.

Github StackBlitz

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 Parcel

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/template-asset-bundling
  2. Install Node.js dependencies:

    npm install
  3. 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 dev runs Parcel in watch mode, compiling assets/app.css (Tailwind source) into hashed output files under public/assets/.
  • The getCssAsset template function walks public/assets/ at render time to find the correct hashed filename and injects the <link> tag automatically.
  • In development (APP_ENV is not production), engine.Reload(true) re-parses templates on every request so changes are reflected without restarting the server.