Skip to main content
GOHUGO
Netlification

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:

1[module]
2[[module.imports]]
3    path = 'github.com/davidsneighbour/hugo-netlification'
1module:
2  imports:
3  - path: github.com/davidsneighbour/hugo-netlification
1{
2   "module": {
3      "imports": [
4         {
5            "path": "github.com/davidsneighbour/hugo-netlification"
6         }
7      ]
8   }
9}

or in your config/module.toml:

1[[imports]]
2  path = 'github.com/davidsneighbour/hugo-netlification'
1imports:
2- path: github.com/davidsneighbour/hugo-netlification
1{
2   "imports": [
3      {
4         "path": "github.com/davidsneighbour/hugo-netlification"
5      }
6   ]
7}

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:

1[outputs]
2  home = ['REDIR', 'HEADERS']
1outputs:
2  home:
3  - REDIR
4  - HEADERS
1{
2   "outputs": {
3      "home": [
4         "REDIR",
5         "HEADERS"
6      ]
7   }
8}

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.

1aliases = ['url1', 'url2', 'url3']
1aliases:
2- url1
3- url2
4- url3
1{
2   "aliases": [
3      "url1",
4      "url2",
5      "url3"
6   ]
7}

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

    1[[redirects]]
    2  netlify = 'https://eloquent-morse-196fd2.netlify.com/'
    
    1redirects:
    2- netlify: https://eloquent-morse-196fd2.netlify.com/
    
    1{
    2   "redirects": [
    3      {
    4         "netlify": "https://eloquent-morse-196fd2.netlify.com/"
    5      }
    6   ]
    7}
    

    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 parameters from and to:

    1[[redirects]]
    2  from = '/old-contact-form/'
    3  status = 200
    4  to = '/contact/'
    
    1redirects:
    2- from: /old-contact-form/
    3  status: 200
    4  to: /contact/
    
    1{
    2   "redirects": [
    3      {
    4         "from": "/old-contact-form/",
    5         "status": 200,
    6         "to": "/contact/"
    7      }
    8   ]
    9}
    

    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.

Testing output