A minimal starter for building websites with Next.js and Contentful CMS.
⚡ View demo: nextjs-contentful-starter.netlify.app
Before you begin, please make sure you have the following:
- Netlify account
- Contentful account
- GitHub, GitLab or Bitbucket account
- Node v18+ or later
- (optional) nvm for Node version management.
Fork and clone your repository, then run npm install in its root directory.
After signing into Contentful, create a new space.
If you don't already have a management token (or personal access token), generate one. To do so, go into your new empty space, then:
- Click Settings
- Choose API Keys
- Select the Content management tokens tab
- Click the button to generate a new token
From the same place you generated the management token, you can now generate API access keys.
- Select the content delivery / preview tokens tab
- Choose Add API key
In your project, duplicate .env.example to .env.
Fill in the values in the file based on the keys you've created.
Note: the Contentful space ID can be viewed and copied via Settings->General Settings in Contentful.
Import the provided content models & content into Contentful by running the import.js script:
npm run import
If the import fails to run, make sure that you've run npm install and that all keys in your .env file are set correctly.
Run the Next.js development server:
npm run dev
Visit localhost:3000 and you should see the example content you imported into your new Contentful space.
To deploy your site to Netlify:
- Push your code to a Git repository (GitHub, GitLab, or Bitbucket)
- Log in to app.netlify.com
- Click "Import from Git" or "Add new site"
- Select your repository
- Configure your build settings:
- Build command:
npm run build - Publish directory:
.next
- Build command:
- Add your environment variables in the Netlify dashboard (Settings > Environment variables):
CONTENTFUL_SPACE_IDCONTENTFUL_DELIVERY_TOKENCONTENTFUL_PREVIEW_TOKEN
- Deploy your site
Here are a few suggestions on what to do next:
- Explore the Contentful documentation to learn more about content modeling
- Check out the Next.js documentation to understand the framework better
- Learn about Netlify deployment options
If you get stuck along the way, get help in our support forums.
