Build Your First Static Site With Hugo

Build Your First Static Site With Hugo

Hugo Website Building: Essential Tips & Tricks

Table of Contents

  1. Optimizing Build Speed
  2. Customizing Themes
  3. Content Organization
  4. Performance Optimization
  5. Advanced Shortcodes
  6. Deployment Strategies

Optimizing Build Speed

1
2
3
# config.toml - Enable fast build mode
ignoreFiles = ["\\.js$", "\\.scss$", "node_modules"]
enableEmoji = false  # Disable if not needed

Customizing Themes

Method 1: Theme Overrides

1
2
3
4
5
6
7
your-site/
├── layouts/
   └── _default/
       └── baseof.html  # Override theme template
└── assets/
    └── css/
        └── custom.css   # Add custom styles

Method 2: Fork & Modify

1
2
3
4
git submodule add https://github.com/themename.git themes/themename
# Make direct changes to theme files
​Best Practice:​​
Always use Hugo Modules for theme management when possible:
1
2
# config.toml
theme = ["github.com/themename"]

Content Organization

Structured Content Types

1
2
3
4
5
6
7
content/
├── blog/
   ├── _index.md       # Section page
   └── post-1.md       # Regular post
└── projects/
    ├── _index.md
    └── project-1.md

Front Matter Tricks

1
2
3
4
5
6
7
8
---
title: "My Post"
date: 2023-07-15
draft: false
tags: ["hugo", "webdev"]
aliases: ["/old-url"]   # For URL redirects
toc: true              # Enable table of contents
---

Performance Optimization

Image Handling

1
2
3
4
<!-- Use Hugo's image processing -->
{{ $image := resources.Get "images/example.jpg" }}
{{ $small := $image.Resize "500x" }}
<img src="{{ $small.RelPermalink }}" alt="Optimized Image">

#Critical CSS

1
2
# Install PostCSS plugin
npm install postcss-cli autoprefixer --save-dev
1
2
3
4
5
6
7
// postcss.config.js
module.exports = {
  plugins: [
    require('autoprefixer'),
    require('cssnano')({preset: 'default'})
  ]
}

Advanced Shortcodes

Custom Alert Box

1
2
3
4
<!-- layouts/shortcodes/alert.html -->
<div class="alert alert-{{ .Get 0 }}">
  {{ .Inner }}
</div>

Responsive Video Embed

1
2
3
4
<!-- layouts/shortcodes/video.html -->
<div class="video-container">
  <iframe src="https://youtube.com/embed/{{ .Get 0 }}" frameborder="0"></iframe>
</div>

Deployment Strategies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# netlify.toml
[build]
  publish = "public"
  command = "hugo --gc --minify"

[context.production.environment]
  HUGO_VERSION = "0.120.4"
  HUGO_ENV = "production"
# .github/workflows/deploy.yml
name: Deploy Hugo
on: push
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: peaceiris/actions-hugo@v2
      - run: hugo --minify
      - uses: peaceiris/actions-gh-pages@v3