Netlify recently announced support for HTTP/2 server push, and we have now added it to the gohugo.io sites for the main CSS and JS bundles, along with server-side 301 redirect support.

If you navigate to https://gohugo.io and look in the Chrome developer network console, you should now see Push as the new source (“Initiator”) for the CSS and JSS:

Setting up this in Hugo was easy:

1. Configure Netlify Output Formats

Add a new custom media type and two new output formats to config.toml. For more on output formats in Hugo, see Custom Output Formats.

  1. [outputs]
  2. home = [ "HTML", "RSS", "REDIR", "HEADERS" ]
  3. [mediaTypes]
  4. [mediaTypes."text/netlify"]
  5. suffix = ""
  6. delimiter = ""
  7. [outputFormats]
  8. [outputFormats.REDIR]
  9. mediatype = "text/netlify"
  10. baseName = "_redirects"
  11. isPlainText = true
  12. notAlternative = true
  13. [outputFormats.HEADERS]
  14. mediatype = "text/netlify"
  15. baseName = "_headers"
  16. isPlainText = true
  17. notAlternative = true

2. Add Template For the _headers File

Add layouts/index.headers:

  1. /*
  2. X-Frame-Options: DENY
  3. X-XSS-Protection: 1; mode=block
  4. X-Content-Type-Options: nosniff
  5. Referrer-Policy: origin-when-cross-origin
  6. */
  7. Link: <{{ "dist/app.bundle.js" | relURL }}>; rel=preload; as=script
  8. Link: <{{ "dist/main.css" | relURL }}>; rel=preload; as=style

The template above creates both a security header definition and a HTTP/2 server push configuration.

Also note that this is a template for the home page, so the full Page with its Site and many variables are available. You can also use partial to include other templates.

3. Add Template For the _redirects File

Add layouts/index.redir:

  1. # Netlify redirects. See https://www.netlify.com/docs/redirects/
  2. {{ range $p := .Site.Pages -}}
  3. {{ range .Aliases }}
  4. {{ . | printf "%-35s" }} {{ $p.RelPermalink -}}
  5. {{ end -}}
  6. {{- end -}}

The template above creates 301 redirects for your aliases, so you will probably want to turn off aliases in your config.toml: disableAliases = true.