This guide will explain how to setup a local environment that can recieve live updates from Contentful. To achieve this we use contentful-hugo's built in server along with concurrently, and Ngrok.
Concurrently is an npm package that allows you to run multiple commands at the same time, while Ngrok is a service that enables you to easily expose a local server to the internet.
npm i --save concurrently
Add a new script that runs contentful-hugo --preview --server
and hugo server
simultaneously using concurrently.
{
"scripts": {
"dev": "concurrently \"contentful-hugo --preview --server\" \"hugo server\""
}
}
Sidenote: if you have heavily nested content you may need to run hugo server --disableFastRender
instead of hugo server
to ensure that everything get's rerendered properly on change.
npm run dev
After this you will have hugo server running on port 1313
and the contentful-hugo server running on port 1414
on your local machine. Now all that's left to do is expose the local webserver to the internet. We will accomplish this with Ngrok
Open a separate terminal window and run
npm i -g ngrok
Or download it from the Ngrok website (If you download it you will need to run the Ngrok commands in the directory where it get's extracted to)
After doing this you should have access to the ngrok
command.
This is an optional step but it will give you access to more features and longer session times.
-
Go to https://ngrok.com and create a free account.
-
Navigate to "getting started" -> "setup & installation"
-
find your auth token
-
type the following command in your terminal
ngrok authtoken <your-token-goes-here>
ngrok http 1414
This command will expose port 1414 on your local machine to a web address, and bring up an ngrok terminal interface.
Copy the forwarding address listed in the Ngrok terminal interface and paste it into the URL
field. Bear in mind this forwarding address changes every time you fire up Ngrok so you'll need to swap it out each time.
You can set Triggers to "trigger for all events" and make sure the URL field is set to "POST"
Now you have live updates on your local machine whenever anything changes in Contentful.