Written by
Mani
Founder, BusinessAddons.com
Learn how to save time and resources by implementing an intelligent rebuild scheduling system for your Hugo website.
Managing a Hugo website can be a time-consuming task, especially when it comes to scheduling content updates and rebuilds. Many developers resort to inefficient methods like frequent cron jobs or manual updates, which waste resources and valuable time.
In this guide, we’ll explore an innovative solution that leverages Hugo’s JSON endpoint and third-party services to create an intelligent rebuild scheduling system for your website.
Traditional approaches to Hugo site rebuilds often involve:
Our solution offers a smarter approach:
This guide is particularly useful for scenarios where content updates are tied to specific events or dates, such as:
By automating Hugo site rebuilds based on these use cases, organizations can ensure their website’s content is dynamically updated in response to real-world events, reducing the need for manual updates and allowing for more strategic content planning.
Before diving into the implementation, ensure you have:
Here’s the flow of what we’re building.
...
This guide is designed to work with any Git CMS, like Decap or Cloud Cannon. It focuses on using keys from your post’s frontmatter. This means you can add these keys in any CMS you prefer, manually or otherwise. Rebuilds are managed by the code we’re sharing later, and your website hosting provider, not your CMS.
If you're finding the technical details of this post overwhelming, we're here to help. Scheduling a consultation can provide you with personalized guidance tailored to your specific needs.
Create a new file in your project’s root directory, inside a folder called ‘build_scripts’. We’ve assumed ./build_scripts/rebuild_at_generator.js This script will:
Here’s a summary of what the script does:
Run The Script: Using node build_scripts/rebuild_at_generator.js ..
(.. stands for relative path to your project, my case it’s one at the root of build_scripts folder)
This script effectively creates a structured list of dates when the site needs to be rebuilt, based on future content publish dates, expiry dates, and other relevant timestamps.
To make the rebuild data accessible:
---
outputs: ["json","html"]
build:
list: local # Prevent this from coming up on the sitemap
publishResources: true # Prevent this from coming up on the sitemap
render: always # Prevent this from coming up on the sitemap
draft: false
---
Create the following files: ./layout/rebuild-at/list.json
The list.json template does the following:
This template ensures that your Hugo site exposes an endpoint with all the necessary rebuild dates, including those from scheduled posts, expiring content, and upcoming events.
Run your Hugo server and access localhost:1313/rebuild-at/index.json
to view a formatted list of rebuild times.
Example output:
[
{
"date": "2024-07-16T14:00:00Z",
"source": "rebuild_at" // meaning this is a scheduled post for a future date
},
{
"date": "2024-07-21T05:00:00Z",
"source": "events" // meaning this is an event's conclusion date
}
]
This list will include times for:
PS: If you don’t like this endpoint to be publicly exposed, you can consider moving it to a serverless function endpoint with any custom authentication you have in mind.
Notes for CMS Users: If you’re using a CMS such as Netlify CMS or CloudCannon, it’s crucial not to provide an Admin UI for
data/rebuild-at/data.json
, as the build script will overwrite this file each time. The purpose of this guide’s approach is to let you set values in your frontmatter anywhere in your project and create a build a system that automates the rebuild process. We’ll also be adding this file to.gitignore
in a later step.
Use Google AppScript to create a service that:
This script ensures that your site is rebuilt at the exact times needed, based on your content schedule, without wasting resources on unnecessary rebuilds.
By implementing this intelligent rebuild scheduling system, you can significantly optimize your Hugo website management process. This solution eliminates the need for wasteful cron jobs or time-consuming manual updates, ensuring your site rebuilds exactly when needed.
As you apply these techniques, you’ll find yourself spending less time on maintenance and more time creating great content for your Hugo website. The combination of Hugo’s powerful content management capabilities with this custom scheduling system provides an efficient and resource-friendly approach to keeping your site up-to-date.
If you’d like to view the full source code, you van visit this Github Repo.