Netlify lets you run automated builds of your website with each Git commit using CI/CD pipeline designed for web developers. Netlify can generate and publish production and preview sites with every push. You can also trigger these builds by content changes in Kontent using webhooks.
Sites powered by static site generators bring undoubtedly a lot of advantages for programmers, editors, and users. One small drawback is the necessity to rebuild your project with every content change in Kontent.
Automated site rebuilding is an essential feature of every static site. You can implement it by leveraging Kontent's webhooks and Netlify's build triggers.
In this tutorial, you'll learn how to use Netlify to run a static site that's built and published with every content update. You'll also find out how to use the Delivery Preview API to build a preview site with content you haven't published yet. Building and deploying are done automatically using Kontent's webhooks and Netlify's build hooks.
You'll have two separate sites on Netlify, each of them based on your production master or main branch code:
Build hooks are URLs you can use to trigger new Netlify builds and deploys.
Webhooks are programmatic notifications that let your application – in this case, Netlify – know when something changes in your Kontent project.
In this section, you'll learn how to build your production site with published content using your production master or main branch.
For this scenario, you need to have prepared one statically generated site. The site must be already deployed to Netlify and configured for continuous deployment. The site needs to fetch content from the Delivery API. Git providers supported by Netlify are Bitbucket, GitHub, and GitLab. Learn more about linking your repository in Netlify.
Now, your production site will be automatically built and deployed with the latest content on Netlify after every publish and unpublish in Kontent.
In this scenario, you'll learn how to build a preview site using your production branch with content you haven't published yet. You can protect this site with a password and use it to preview changes before you release them publically.
To build a preview site, you need to use a second statically generated site that's already deployed to Netlify and configured for continuous deployment. This site needs to use the same repository as the production site but, unlike the production site, it needs to use the Delivery Preview API to fetch the content.
Distinguishing between the Delivery API and the Delivery Preview API depends on the used static site generator and the overall logic of your code. The most reasonable approach is to distinguish them using environment variables. The example below uses Gatsby.js static site generator with @kentico/gatsby-source-kontent plugin. The gatsby-config.js uses the Delivery Preview API if its key is provided in the respective variable.
{
resolve: '@kentico/gatsby-source-kontent',
options: {
projectId: process.env.KONTENT_PROJECT_ID,
usePreviewUrl: process.env.KONTENT_PREVIEW_ENABLED && process.env.KONTENT_PREVIEW_ENABLED.toLowerCase() === 'true',
authorizationKey: process.env.KONTENT_PREVIEW_ENABLED && process.env.KONTENT_PREVIEW_ENABLED.toLowerCase() === 'true'
? process.env.KONTENT_PREVIEW_KEY
: undefined,
languageCodenames: ['en-US'],
},
}
This allows you to have one master or main branch you can use to build production and preview sites using different Delivery API endpoints according to the provided environment variables.
KONTENT_PREVIEW_ENABLED
and KONTENT_PREVIEW_KEY
. Now, your preview site will be automatically built and deployed with the latest content on Netlify every time you add, edit, or delete something in Kontent.
Unless you want to show your content drafts and development tests to the whole world, it's essential to restrict the access to your preview site only to authorized people. To do that, use Netlify's password protection to hide your site behind a password that you control.
You've learned how to create a static site on Netlify that'll get updated with every content publish and unpublish you do in Kontent. You also know how to create a password-protected preview site on Netlify for content and development testing.
This tutorial demonstrated the capabilities in combination with Gatsby.js. A similar approach can be used for the majority of other static site generators, such as Next.js.
You can empower your developers and editors to preview published or unpublished content with non-production branches with Netlify's deploy previews.
Originally published at docs.kontent.ai.