Most blogging websites are powered by server-side software such as Ghost or WordPress. This is extremely useful since they can support complex post editors, schedule posts to be released later, and allow users to register on the website.
I self-host Ghost in a dedicated virtual machine, but this mean that I have no redundancy and if I have to do a reboot of the virtual machine for updates it results in my blog content being unavailable for a few minutes. Eventually, I decided I wasn’t happy with this and decided to do something about it.
Using Ghost as a Headless CMS with Eleventy and Cloudflare Pages
A good answer seemed to be using Ghost as a headless CMS (this way I still get to keep my post scheduling functionality), and then to use a static site generator (Eleventy) to generate a static website. After the static website has been generated, is then deployed to Cloudflare Pages, making it available in data centres around the planet.
The disadvantage of this is that you lose the ability to use dynamic functionality available to website visitors, such as membership registration. But the impact of this depends on your use case and the features of Ghost you are using.
This process was even easier because Ghost already provides a Eleventy starter template for using it as a static site generator for Ghost. So let’s get started...
Fork the Eleventy Ghost Starter Template
The first step is to fork the Eleventy Ghost Starter template project on GitHub. This will let you modify the template and setup a GitHub repository to use when creating the Cloudflare Pages project.
- Go to the Eleventy Ghost Starter Template project page
- Click the Fork button at the top right of the page
- Change the repository name and description if needed
- Click the Create fork button
Now you have a GitHub repository you can modify.
Create an integration in Ghost
The next step is to create an integration for Eleventy in Ghost to grab a content API key. This will allow Eleventy to access the content of your Ghost site while it is generating the static website.
- Go to the Ghost admin area
- Go to Settings
- Under Advanced, go to Integrations
- Scroll down and click Add custom integration
- Enter a name (such as Static Website) and click Create
- Copy the Content API key
Now that we have a Content API key for Ghost, we can start modifying our forked GitHub repository.
Update Eleventy settings
The next step is to update the settings for Eleventy to match your Ghost website.
- Open the
GHOST_API_URLto be the root URL of your Ghost installation
GHOST_CONTENT_API_KEYwith the content API key from the Ghost integration
SITE_URLto be the URL your static website will be deployed to
Now that those settings are in place, we can test that Eleventy is working by running the following commands.
Now you should be able to see your static website running locally with content from your Ghost blog displayed in the start template. This is the time to modify the templates and CSS to your desired result.
Creating a Cloudflare Pages project
Now is the time to create the Cloudflare Pages project that will be used to deploy the static website.
- Go to the Cloudflare Dashboard
- Select Pages from the navigation menu
- Click Create project, and then Connect to Git
- Select the repository and click Begin setup
- Modify the project name as needed
- Set build command to be
- Set build output directory to be dist
- Expand environment variables
- Click Add variable
- Set Variable name to
- Set Variable value to
- Click Save and Deploy
A build process will now be started for your site. This will clone your GitHub repository, run Eleventy to generate the static website, and then the contents of the output directory will be deployed across the Cloudflare network.
Setup a custom domain
The next step is to setup a custom domain to match the
SITE_URL setting which you set earlier. If you don’t do this, your site will only be available at the provided Cloudflare Pages URL.
- Go to the Custom Domains tab in your Cloudflare Pages project
- Click Setup a custom domain
- Enter the domain of your website
- Click Continue
- Check the DNS settings and click Activate domain
When the process has completed, your new statically generated is available and active at your custom domain. But, we’re not quite done yet!
Add a Deploy Hook
The last step is to finish the integration of Cloudflare Pages with Ghost. At the moment, Cloudflare Pages will only deploy a new copy of the website if the GitHub repository is updated. This will work fine the template files are changed, but not if a new post is published in Ghost.
To fix this we can add a Deploy Hook to the Cloudflare Pages project and add this to the Ghost integration. This will then trigger a deployment to take place whenever Ghost calls the deploy hook URL.
- Go to the Settings tab in your Cloudflare Pages project
- Click Builds & deployments
- Scroll to the bottom and click Add deploy hook
- Enter a name for the hook e.g. Ghost
- Click add
- Copy the provided deploy hook URL
Now we need to add a webhook to our custom Ghost integration to call this URL when we need a deployment to take place.
- Go to the Ghost integration
- Click Add webhook
- Enter an appropriate name for the Webhook e.g. Cloudflare Pages – Post published
- Set Event to Post published
- Set Target URL to be the deploy hook URL
- Click Create
- Repeat the process for other events that would require a deployment, e.g. Post unpublished
Now that we have completed those steps (even if it is a little frustrating you can’t select multiple events for the single webhook), a Cloudflare Pages deployment will be triggered whenever any of those Ghost events are triggered.
Hope this helps you get your static website deployed on Cloudflare Pages while using the features of Ghost as a headless CMS!