RawContent and Content Fingerprints in Hugo
Updated 2021-September-10

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 Anatomy of a content file section.

 

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, 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, the layout file for this page includes:

RawContent {{ .Page.RawContent | sha256 | truncate 6 "" }}

which the hugo command renders as:

RawContent b836ad

 

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

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

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

On Infinite Ink I use both these fingerprints because that way I can tell if changes happened in one or both of:

  • the primary source file

  • the external snippets

For an example of these fingerprints, see the Page Meta section of this page below.⁠

 

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 call 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 👍 👎 📝

Please comment so I know I'm not speaking into the void. Also, your public comment might improve this page or help me to improve this page.