Static blog with Preview Mode, built with Next.js and Contentful.
This example showcases Next.js's Static Generation feature using Contentful as the data source.
Using the Deploy Button below, you'll deploy the Next.js project as well as connect it to your Contentful space using the Vercel Contentful Integration.
Execute
npx create-next-app --example cms-contentful cms-contentful-app
yarn create next-app --example cms-contentful cms-contentful-app
pnpm create next-app --example cms-contentful cms-contentful-app
First, create an account on Contentful.
After creating an account, create a new empty space from the dashboard and assign to it any name of your liking.
The content model defines the data structures of your application/websites. The structures are flexible and you can tailor them to your needs.
For this example you need to create a content model that defines an author and a post content type. You can create these two by running a script or by doing it manually to familiarize yourself with the Contentful user interface.
Run a script to create the content modelThis project includes a setup script which you can use to set up the content model expected by the source code.
In your Contentful dashboard go to Settings > General Settings and copy the Space ID.
Next, go to Settings > CMA tokens and create a new token by clicking Create personal access token. This token has the same access rights as the logged in user. Do not share it publicly, you will only use it to set up your space and can delete it afterwards.
With the space ID and management access token at hand run the following command:
npx cross-env CONTENTFUL_SPACE_ID=YOUR_SPACE_ID CONTENTFUL_MANAGEMENT_TOKEN=XXX npm run setup
This command will create the needed content structures and set up your Contentful space ready to use. The output should look as follows:
Create the content model manually Create an
> cms-contentful@1.0.0 setup /Users/stefan.judis/Projects/next.js/examples/cms-contentful> node ./contentful/setup.js $CONTENTFUL_SPACE_ID $CONTENTFUL_MANAGEMENT_TOKEN┌──────────────────────────────────────────────────┐│ The following entities are going to be imported: │├─────────────────────────────────┬────────────────┤│ Content Types │ 2 │├─────────────────────────────────┼────────────────┤│ Editor Interfaces │ 2 │├─────────────────────────────────┼────────────────┤│ Locales │ 1 │├─────────────────────────────────┼────────────────┤│ Webhooks │ 0 │├─────────────────────────────────┼────────────────┤│ Entries │ 0 │├─────────────────────────────────┼────────────────┤│ Assets │ 0 │└─────────────────────────────────┴────────────────┘✔ Validating content-file✔ Initialize client (1s)✔ Checking if destination space already has any content and retrieving it (2s)✔ Apply transformations to source data (1s)✔ Push content to destination space✔ Connecting to space (1s).........
Author
From your contentful space, go to Content model and add a new content type:
Author
author
Once the content model is saved, add these fields (you don't have to modify the settings unless specified):
name
name
picture
picture
Save the content type and continue.
Create apost
From your contentful space, go to Content model and add another content type:
Post
post
Next, add these fields (you don't have to modify the settings unless specified):
title
content
excerpt
coverImage
date
slug
title
author
Save the content type and continue.
After setting up the content model (either manually or by running
npm run setup
yarn setup
Content model overview
Go to the Content section in your space, then click on Add entry and select the Author content type:
Next, create another entry with the content type Post:
Important: For each entry and asset, you need to click on Publish. If not, the entry will be in draft state.
From your contentful space, go to Settings > API keys. There will be an example Content delivery / preview token - you can use these API keys. (You may also create a new key.)
Next, copy the
.env.local.example
.env.local
cp .env.local.example .env.local
Then set each variable on
.env.local
CONTENTFUL_SPACE_ID
CONTENTFUL_ACCESS_TOKEN
CONTENTFUL_PREVIEW_ACCESS_TOKEN
CONTENTFUL_PREVIEW_SECRET
CONTENTFUL_REVALIDATE_SECRET
Your
.env.local
CONTENTFUL_SPACE_ID=...CONTENTFUL_ACCESS_TOKEN=...CONTENTFUL_PREVIEW_ACCESS_TOKEN=...CONTENTFUL_PREVIEW_SECRET=...CONTENTFUL_REVALIDATE_SECRET=...
npm installnpm run dev# oryarn installyarn dev
Your blog should be up and running on http://localhost:3000! If it doesn't work, post on GitHub discussions.
In your Contentful space, go to Settings > Content preview and add a new content preview for development.
The Name field may be anything, like
Development
http://localhost:3000/api/draft?secret=<CONTENTFUL_PREVIEW_SECRET>&slug={entry.fields.slug}
Replace
<CONTENTFUL_PREVIEW_SECRET>
.env.local
Once saved, go to one of the posts you've created and:
[Draft]
You will now be able to see the updated title. To manually exit Draft Mode, you can navigate to
/api/disable-draft
You can deploy this app to the cloud with Vercel (Documentation).
Deploy Your Local ProjectTo deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and import to Vercel.
Important: When you import your project on Vercel, make sure to click on Environment Variables and set them to match your
.env.local
Alternatively, you can deploy using our template by clicking on the Deploy button below.
This will deploy the Next.js project as well as connect it to your Contentful space using the Vercel Contentful Integration. If you are using Draft Mode, make sure to add
CONTENTFUL_PREVIEW_SECRET
In your Contentful space, go to Settings > Webhooks and add a new webhook:
Give the webhook a name
Activate: Check the activate checkbox to ensure the webhook is marked as active
Specify the POST URL: Using the URL from your Vercel deployment in step 8, add the path
/api/revalidate
https://<YOUR_VERCEL_DEPLOYMENT_URL>/api/revalidate
Replace
<YOUR_VERCEL_DEPLOYMENT_URL>
Specify Triggers: You can choose to trigger for all events or specific events only, such as the Publishing and Unpublishing of Entries and Assets, as shown below.
Specify Secret Header: Add a secret header named
x-vercel-reval-key
CONTENTFUL_REVALIDATE_SECRET
Set Content type: Set content type to
application/json
Edit post: Now, try editing the title of one of your blog posts in Contentful and click Publish. You should see the changed reflected in the website you just deployed, all without triggering a build! Behind the scenes a call was made to the revalidate api that triggers a revalidation of both the landing page and the specific post that was changed.
Verify: You can verify if your request was made successfully by checking the webhook request log on Contentful and checking for a successful 200 status code, or by having your functions tab open on Vercel when committing the change (log drains may also be used). If you are experiencing issues with the api call, ensure you have correctly entered in the value for environment variable
CONTENTFUL_REVALIDATE_SECRET
Static blog with Preview Mode, built with Next.js and Contentful.
This example showcases Next.js's Static Generation feature using Contentful as the data source.
Using the Deploy Button below, you'll deploy the Next.js project as well as connect it to your Contentful space using the Vercel Contentful Integration.
Execute
npx create-next-app --example cms-contentful cms-contentful-app
yarn create next-app --example cms-contentful cms-contentful-app
pnpm create next-app --example cms-contentful cms-contentful-app
First, create an account on Contentful.
After creating an account, create a new empty space from the dashboard and assign to it any name of your liking.
The content model defines the data structures of your application/websites. The structures are flexible and you can tailor them to your needs.
For this example you need to create a content model that defines an author and a post content type. You can create these two by running a script or by doing it manually to familiarize yourself with the Contentful user interface.
Run a script to create the content modelThis project includes a setup script which you can use to set up the content model expected by the source code.
In your Contentful dashboard go to Settings > General Settings and copy the Space ID.
Next, go to Settings > CMA tokens and create a new token by clicking Create personal access token. This token has the same access rights as the logged in user. Do not share it publicly, you will only use it to set up your space and can delete it afterwards.
With the space ID and management access token at hand run the following command:
npx cross-env CONTENTFUL_SPACE_ID=YOUR_SPACE_ID CONTENTFUL_MANAGEMENT_TOKEN=XXX npm run setup
This command will create the needed content structures and set up your Contentful space ready to use. The output should look as follows:
Create the content model manually Create an
> cms-contentful@1.0.0 setup /Users/stefan.judis/Projects/next.js/examples/cms-contentful> node ./contentful/setup.js $CONTENTFUL_SPACE_ID $CONTENTFUL_MANAGEMENT_TOKEN┌──────────────────────────────────────────────────┐│ The following entities are going to be imported: │├─────────────────────────────────┬────────────────┤│ Content Types │ 2 │├─────────────────────────────────┼────────────────┤│ Editor Interfaces │ 2 │├─────────────────────────────────┼────────────────┤│ Locales │ 1 │├─────────────────────────────────┼────────────────┤│ Webhooks │ 0 │├─────────────────────────────────┼────────────────┤│ Entries │ 0 │├─────────────────────────────────┼────────────────┤│ Assets │ 0 │└─────────────────────────────────┴────────────────┘✔ Validating content-file✔ Initialize client (1s)✔ Checking if destination space already has any content and retrieving it (2s)✔ Apply transformations to source data (1s)✔ Push content to destination space✔ Connecting to space (1s).........
Author
From your contentful space, go to Content model and add a new content type:
Author
author
Once the content model is saved, add these fields (you don't have to modify the settings unless specified):
name
name
picture
picture
Save the content type and continue.
Create apost
From your contentful space, go to Content model and add another content type:
Post
post
Next, add these fields (you don't have to modify the settings unless specified):
title
content
excerpt
coverImage
date
slug
title
author
Save the content type and continue.
After setting up the content model (either manually or by running
npm run setup
yarn setup
Content model overview
Go to the Content section in your space, then click on Add entry and select the Author content type:
Next, create another entry with the content type Post:
Important: For each entry and asset, you need to click on Publish. If not, the entry will be in draft state.
From your contentful space, go to Settings > API keys. There will be an example Content delivery / preview token - you can use these API keys. (You may also create a new key.)
Next, copy the
.env.local.example
.env.local
cp .env.local.example .env.local
Then set each variable on
.env.local
CONTENTFUL_SPACE_ID
CONTENTFUL_ACCESS_TOKEN
CONTENTFUL_PREVIEW_ACCESS_TOKEN
CONTENTFUL_PREVIEW_SECRET
CONTENTFUL_REVALIDATE_SECRET
Your
.env.local
CONTENTFUL_SPACE_ID=...CONTENTFUL_ACCESS_TOKEN=...CONTENTFUL_PREVIEW_ACCESS_TOKEN=...CONTENTFUL_PREVIEW_SECRET=...CONTENTFUL_REVALIDATE_SECRET=...
npm installnpm run dev# oryarn installyarn dev
Your blog should be up and running on http://localhost:3000! If it doesn't work, post on GitHub discussions.
In your Contentful space, go to Settings > Content preview and add a new content preview for development.
The Name field may be anything, like
Development
http://localhost:3000/api/draft?secret=<CONTENTFUL_PREVIEW_SECRET>&slug={entry.fields.slug}
Replace
<CONTENTFUL_PREVIEW_SECRET>
.env.local
Once saved, go to one of the posts you've created and:
[Draft]
You will now be able to see the updated title. To manually exit Draft Mode, you can navigate to
/api/disable-draft
You can deploy this app to the cloud with Vercel (Documentation).
Deploy Your Local ProjectTo deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and import to Vercel.
Important: When you import your project on Vercel, make sure to click on Environment Variables and set them to match your
.env.local
Alternatively, you can deploy using our template by clicking on the Deploy button below.
This will deploy the Next.js project as well as connect it to your Contentful space using the Vercel Contentful Integration. If you are using Draft Mode, make sure to add
CONTENTFUL_PREVIEW_SECRET
In your Contentful space, go to Settings > Webhooks and add a new webhook:
Give the webhook a name
Activate: Check the activate checkbox to ensure the webhook is marked as active
Specify the POST URL: Using the URL from your Vercel deployment in step 8, add the path
/api/revalidate
https://<YOUR_VERCEL_DEPLOYMENT_URL>/api/revalidate
Replace
<YOUR_VERCEL_DEPLOYMENT_URL>
Specify Triggers: You can choose to trigger for all events or specific events only, such as the Publishing and Unpublishing of Entries and Assets, as shown below.
Specify Secret Header: Add a secret header named
x-vercel-reval-key
CONTENTFUL_REVALIDATE_SECRET
Set Content type: Set content type to
application/json
Edit post: Now, try editing the title of one of your blog posts in Contentful and click Publish. You should see the changed reflected in the website you just deployed, all without triggering a build! Behind the scenes a call was made to the revalidate api that triggers a revalidation of both the landing page and the specific post that was changed.
Verify: You can verify if your request was made successfully by checking the webhook request log on Contentful and checking for a successful 200 status code, or by having your functions tab open on Vercel when committing the change (log drains may also be used). If you are experiencing issues with the api call, ensure you have correctly entered in the value for environment variable
CONTENTFUL_REVALIDATE_SECRET