RawContent and Content Fingerprints in Hugo
Updated  2022-June-13

Page contents

News

2021-August-17  Published this evolving⁠[1] article.

Prerequisites

This article assumes you know the basics about the Hugo static site generator.

 

Anatomy of a Hugo page’s primary source file

Abstractly a Hugo page’s primary⁠[2] source file looks like this:

front matter

main matter
 

Details about this are in Infinite Ink’s Hugo Tutorial in the section Anatomy of a content file.

 

Using a page’s main matter in a Hugo layout file

In a Hugo layout file, the main matter of a page can be accessed with the following page variables.

.RawContent

The main matter as it appears in the source file, i.e., everything below the front matter.

.Content

The main matter after it has been processed by Hugo with the help of a markup renderer, such as Asciidoctor, go-org, Goldmark, or Pandoc.

 

Fingerprinting or hashing a page

One way to check if a page has changed is to check its fingerprint or hash. For example, a hash of this page’s RawContent is:

3BC625

The Hugo shortcode that produces the above hash includes one of the following lines.

{{ .Page.RawContent | sha256 | truncate 6 "" | upper }}
{{ substr (.Page.RawContent | sha256 | upper) 0 6 }}
{{ slicestr (.Page.RawContent | sha256 | upper) 0 6 }}

Each of the above three lines produces this:

3BC625

 

If a page does not use any external snippets,⁠[3] it’s often sufficient to use the above .Page.RawContent fingerprint. But if a page uses external snippets, you need to fingerprint .Page.Content with, for example, this:

{{ .Page.Content | sha256 | truncate 6 "" | upper }}

 

Fingerprinting the Content, rather than the RawContent, is needed because it’s possible that the RawContent is unchanged but one (or more) external snippet has changed.

A hash of the Content of this page is in the Page Meta section below.

References

See also

Endnotes


1. Many Infinite Ink articles, including this one, are evergreen and regularly updated.
2. It is possible for a Hugo page’s primary source file to pull snippets from other (non-primary) source files.
3. Snippets can be specified in front matter, Hugo layout files (including Hugo shortcodes), AsciiDoc attributes, and AsciiDoc includes.

Comments and questions 📝 🤔 👎 👍

Your public comment or question might immediately improve this page or help me to (eventually) improve this page.