Hugo Framework Netlify Confugartion File Netlify.toml Introduction
Contents
The netlify.toml
file is a configuration file used to customize and automate the deployment of a site on Netlify. When deploying a Hugo site on Netlify, netlify.toml
allows you to define the build process, environment variables, and deployment settings.
Here’s a detailed introduction to using netlify.toml
for a Hugo site:
Basic Structure of netlify.toml
The netlify.toml
file is written in the TOML format and typically resides in the root of your project. A simple example for a Hugo site looks like this:
|
|
Key Sections
[build]
Section This defines the build settings for your site.command
: Specifies the build command to generate the static site. For Hugo, the default is:1
command = "hugo --gc --minify"
--gc
: Enables garbage collection to clean up unused resources.--minify
: Minifies the HTML, CSS, and JS for optimized delivery.
publish
: The directory where Hugo generates the static site. For Hugo, it’s usually:1
publish = "public"
[build.environment]
Section Sets environment variables for the build process.HUGO_VERSION
: Defines the Hugo version to use during the build. This is essential because Netlify doesn’t always default to the latest Hugo version.1
HUGO_VERSION = "0.115.2"
Replace
"0.115.2"
with the desired Hugo version.HUGO_ENV
: Sets the environment to production or development. For live sites, use:1
HUGO_ENV = "production"
Other environment variables (if needed):
1 2
GO_VERSION = "1.20" # If using Go modules GIT_SUBMODULE_STRATEGY = "recursive" # If using Git submodules
[[redirects]]
Section Sets up redirects for your site. This is useful for SEO, custom routes, or handling 404s.Example: Redirect
/old-page
to/new-page
:1 2 3 4 5
[[redirects]] from = "/old-page" to = "/new-page" status = 301 force = true
[[headers]]
Section Configures custom HTTP headers for your site.Example: Add a security header:
1 2 3 4 5
[[headers]] for = "/*" # Applies to all paths [headers.values] Content-Security-Policy = "default-src 'self'" X-Frame-Options = "DENY"
[[plugins]]
Section Adds Netlify Build Plugins for additional functionality.Example: Install a plugin to optimize images:
1 2
[[plugins]] package = "@netlify/plugin-image-optim"
Example Full Configuration for a Hugo Site
|
|
Deployment Workflow
Add
netlify.toml
to Your Hugo Project Place thenetlify.toml
file in the root of your project.Push Changes to Git Netlify automatically detects changes when you push to the connected repository. Ensure the
netlify.toml
file is committed:1 2 3
git add netlify.toml git commit -m "Add netlify.toml for Hugo deployment" git push
Trigger Build on Netlify
- Log in to Netlify and link your repository if it’s not already connected.
- Netlify will read the
netlify.toml
file and build the site accordingly.
Common Issues and Tips
Incorrect Hugo Version:
- Ensure the
HUGO_VERSION
innetlify.toml
matches the one used locally.
- Ensure the
Using Git Submodules:
- Add
GIT_SUBMODULE_STRATEGY = "recursive"
to handle submodules during deployment.
- Add
Using Go Modules:
- Set
GO_VERSION
to ensure the correct version of Go is used. - Go modules dependencies are fetched automatically by Hugo.
- Set
Custom Domain and Redirects:
- Configure redirects in
netlify.toml
for better control of URL structure.
- Configure redirects in
Performance Optimization:
- Use the
--gc
and--minify
flags in the build command. - Consider adding plugins like
@netlify/plugin-lighthouse
to analyze site performance.
- Use the
With a properly configured netlify.toml
, deploying a Hugo site to Netlify becomes efficient and automated, ensuring a consistent and optimized build process.
For more advanced configurations and options, refer to the Netlify documentation.
Reference:
Netlify Documentation Netlify Build Plugins Netlify Redirects Netlify Headers Netlify Environment Variables Netlify Build Command Netlify Build Plugins