Creating a blog using Hugo and Github Pages
Why I chose Hugo and Github Pages for this blog
I’ve been wanting to start a blog for a very long time. I’ve setup WordPress blogs, a Ghost blog, and –because i’m a developer– i’ve definitely tried building my own blog.
Since I never got in the habit of writing blogs enough, the hosting was never worth it. I’d get no traffic.
With Github Pages, the hosting is taken care of for you. Hugo is completely open source, so it’s free as well. This gave me the fastest option to go from idea to blog to hosting. Once it’s setup, outside of the domain, it’s always going to be live. I can build my habits without the cost.
Getting Started with Hugo
Setting up Hugo on Ubuntu 24.04
- Install Hugo extended version using
go install
(the extended version is needed for SCSS capabilities) - Create a new blog:
|
|
Choosing and Configuring a Theme
- Browse themes at https://themes.gohugo.io/
- I chose the m10c theme (https://themes.gohugo.io/themes/hugo-theme-m10c/)
- Configure the theme in
hugo.toml
- Customize theme colors and styles in
/themes/[theme_name]/assets/css/components/*.scss
Common Theme Setup Issues
If you see these warnings:
|
|
Solution: Pass your theme explicitly when running the server:
|
|
Configuring Your Blog
Setting up URL Structure
To get WordPress-style permalinks, configure your hugo.toml
:
|
|
This transforms /content/blog/2024/post.md
into https://domani.com/blog/2024/11/04/post
Customizing Code Blocks
- View available styles at Chroma Style Gallery
- Configure in
hugo.toml
:
|
|
For more control:
- Generate custom CSS:
hugo gen chromastyles --style=monokai > syntax.css
- Move to
/static/css/syntax.css
- Import in layout:
<link rel="stylesheet" href="/css/syntax.css">
- Fix overflow issues by adding:
|
|
Creating Content
Create new posts using:
|
|
Posts use front matter for metadata. You can create default templates in archetypes.
Deployment
Setting up Github Pages
- Create a repository named
username.github.io
- Upload your Hugo files
- Set up Github Actions for continuous deployment
Github Actions Configuration
If Github Pages only returns XML, check action logs for WARN found no layout file
. Fix by adding the theme to your workflow:
|
|
Lighthouse Tests and Optimizations
Initial lighthouse test results showed room for improvement. This is still very good out of the box, especially for how easy it was to setup. All the required fixes were my fault rather than anything hugo was doing.
Optimizing Images
- Converted images to WebP format
- Reduced file sizes significantly
Optimizing Fonts
Combined fonts into one url and used preconnects to shave off some time by establishing a connection early.
|
|
Added meta description to blog post
The SEO was taking a 9 point hit because I forgot to add a meta description. Since hugo uses front matter, all I had to do was add a description tag to the top of the page.
|
|
Improving Accessibility
Fixed contrast ratios for code blocks. Google devtools has a great feature that highlights the exact html blocks that are giving you issues. As seen here: comment, page number, cli-text
Added an underline to all links so I didn’t have to change the color and deal with contrast.
Added a favicon
The Best Practices
was complaining about errors in the console. It turned out that I was missing a favicon.ico. I converted my avatar into an icon file and that put me back at 100%.
Final Results
After optimizations, achieved near-perfect Lighthouse scores:
Note: For best benchmark results, test in Chrome incognito mode to avoid extension interference.