Automatically Updating the Last Modified Date of Hugo Posts: Streamlining Management Steps and Enhancing SEO


August 21, 2023 Program

Automatically Updating the Last Modified Date of Hugo Posts: Streamlining Management Steps and Enhancing SEO


Switching from setting the last modified date in Front Matter to using the file modification date as the default post update date in Hugo simplifies the cumbersome process and boosts SEO ranking.

Foreword

🔗

There are often situations where posts need to be edited or updated that show updated time on the post, usually to improve SEO or inform readers that the content is the latest. Originally, the Front Matter was used, and within each post's content, the lastmod field along with a date would be added to display the last editing time of the post. However, this process was quite tedious as it required editing the field every time an post was modified. With a large number of posts, this procedure became intricate. Upon researching, it was discovered that it's possible to directly add a default Front Matter reference order in the config.toml file. This allows automatic insertion of any desired value for lastmod in cases where it's not explicitly used in the post. This achieves the desired effect of default lastmod display.

Solution

🔗

Simply by adding the following syntax to config.toml:

toml
[frontmatter]
lastmod = ["lastmod" ,':fileModTime', ":git", "date"]

:fileModTime : The last modification time of the file.
lastmod : The original lastmod date.
:git : It is said to be possible to use the commit time from git as the display date, but further research has not been conducted here.
date: The publication date of the post.

This method involves modifying the default data for lastmod (prioritizing the one listed first in the order; if not found, moving on to the next) to be displayed.

So, the syntax can be as simple as this.

toml
[frontmatter]
lastmod = ["lastmod" ,':fileModTime']

If an post doesn't have the lastmod field, then the default behavior is to use :fileModTime as the last modification time of the post.

However, this approach would result in each post displaying the last modification time, since typically there is a difference between the post's date and :fileModTime.

My approach involves modifying root\themes\{your theme}\layouts\partials\post_meta\date.html to achieve the effect of hiding the modification date for certain posts.

Code:

Html
{{- if not .Date.IsZero }}
<div class="meta__item-datetime meta__item">
	{{ partial "svg/calendar.svg" (dict "class" "meta__icon") -}}
	<time class="meta__text" datetime="{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}">
		{{- .Date.Format (.Site.Params.dateformat | default "January 02, 2006") -}}
	</time>
	{{- $dateDiff := .Lastmod.Sub .Date -}}
	{{- if ge $dateDiff.Hours 100 }}
	<time class="meta__text" datetime="{{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }}">(
		{{- T "meta_lastmod" }}: {{ .Lastmod.Format (.Site.Params.dateformat | default "January 02, 2006") -}}
	)</time>
	{{- end -}}
</div>
{{- end }}

The modification involves the following changes, with the logic being to calculate the time difference between post date.Date and modification date.Lastmod.
If the time difference is greater than 100 hours, then the modification date is displayed.
(This value can be adjusted according to personal preference. Since I'm accustomed to the timeframe between writing an post and publishing it being usually less than a week, as long as the modification date is more than a week after the post date, it indicates content update and the modification date is shown.)

HTML
{{- $dateDiff := .Lastmod.Sub .Date -}}
{{- if ge $dateDiff.Hours 100 }}
<time class="meta__text" datetime="{{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }}">(
		{{- T "meta_lastmod" }}: {{ .Lastmod.Format (.Site.Params.dateformat | default "January 02, 2006") -}}
	)</time>
{{- end -}}

Conclusion

🔗

By employing this method, it's possible to efficiently manage modification dates and even use only the latest modification date as the publication date. This creates a perception for readers that the content is freshly published and entirely new. As for how browsers might interpret this and the impact on SEO ranking, that's a bit uncertain 😎.

HugoSEO



Avatar

Alvin

Software engineer, interested in financial knowledge, health concepts, psychology, independent travel, and system design.

Related Posts






Discussion (0)



  or   

No comments yet.