Netlification
This is a Hugo theme component with helpers to host your GoHugo generated static website on Netlify. If you don’t use Netlify, you DO NOT need this module.
Add this module |
---|
[[module.imports]] path = "github.com/davidsneighbour/hugo-modules/modules/netlification" disable = false ignoreConfig = false ignoreImports = false |
Latest Version |
modules/netlification/v1.2023.12 (2023-09-18) |
Features
Redirects: Adds redirects via HTTP headers. This redirection is faster and SEO wise better than Hugo’s method of adding meta-refresh
commands in dedicated files.
CSP: Adds Content Security Policies for improved security.
Headers: Adds headers with caching and security directives to improves security and speed.
Installation and setup
Step 1: enable modules in your own repository
1hugo mod init github.com/username/reponame
Step 2: add the module to your required modules in config.toml
:
or in your config/module.toml
:
The next time you run hugo it will download the latest version of the module.
Step 3: Add REDIR
and HEADERS
to your home output formats:
You should already have an [outputs]
section, add "REDIR", "HEADERS"
to it. Add them to the home
parameter, not to other definitions.
Configuration
Redirects
Per post
Redirection takes aliases that are defined in the pages frontmatter and creates a 301 redirect for them. This is done via HTTP headers as opposed to the redirects via HTML meta tags that Hugo is doing. This is faster and might be better for SEO.
Keep defining them via frontmatter and let Netlification do the rest.
Additional Redirects
-
A redirect for 404 errors to Hugo’s 404 page (
/layouts/404.html
) - no action by you required -
A redirect for your default netlify.com URL to your live URL via data configuration in
data/dnb/netlification/config.toml
The URL will be redirected to your
baseURL
. Right now this feature requires a trailing slash on both, baseURL and netlify parameter -
Add more redirects as required. Each redirect requires a header
[[redirects]]
followed by at least the parametersfrom
andto
:You can add a status property, if you wish to output any other code than 301 for the redirect. The status property is optional and is explicitly intended for redirect cases.
The format of these redirect tables is identical to redirects format used in the Netlify configuration file format.
Disable internal alias creation in Hugo
If you are using Netlification you can speed up Hugo’s page creation process a little bit by setting the config variable disableAliases
to true
. This will disable the default behaviour of creating an HTML file per alias to redirect via meta tags and speed up site generation.
Headers
Netlification uses considerate caching options. Stylesheets, javascripts, images and other media files are cached for a full year. Netlification expects you to use Hugo pipes to create those files, which will result in unique URLs after you change the content of the files.
Content Security Policy
Have a look in data/dnb/netlification/config.toml or data/dnb/netlification/sample-config.toml to learn more.
You can check your content security policy using these following services and audits:
Sample Configuration
Add your configuration in data/dnb/netlification/config.toml
. A sample configuration can be found in data/dnb/netlification/sample-config.toml
Updating
To update this module:
1hugo mod get -u github.com/davidsneighbour/hugo-netlification
To update all modules:
1hugo mod get -u
Extend netlification headers from other modules
hugo-netlification
offers an easy way to plug into the _headers
file. Just add a file at /data/namespacename/modulename/netlification.toml
and follow the instructions about header formats at docs.netlify.com. These rules are added after the rules by hugo-netlification
.
Notes
- Netlify’s redirects engine will process the first matching rule it finds, reading from top to bottom. Rules in the
_redirects
file are always processed first, followed by rules in the Netlify configuration file.