In this tutorial, we will learn how to build a blog using Nuxt.js and Contentful. Nuxt.js is a powerful framework for building Vue.js applications, and Contentful is a flexible and scalable content management system (CMS) that allows you to manage and deliver content across multiple platforms.
By the end of this tutorial, you will have a fully functional blog with a dynamic list of blog posts and individual blog post pages. Let’s get started!
Prerequisites
To follow along with this tutorial, you will need the following:
- Basic knowledge of JavaScript and Vue.js
- Node.js installed on your local machine
- Basic knowledge of terminal commands
Step 1: Set Up a Nuxt.js Project
To start, let’s create a new Nuxt.js project. Open your terminal and navigate to your desired project directory. Run the following command:
npx create-nuxt-app blog
This will prompt you to answer a few questions to generate the project. Choose the default options for now.
Once the project is generated, navigate into the project directory:
cd blog
Now, start the development server:
npm run dev
You should see the Nuxt.js logo and a message saying “Nuxt.js is listening on http://localhost:3000”. Open your browser and navigate to `http://localhost:3000` to see your Nuxt.js app in action.
Step 2: Set Up Contentful
Next, we need to set up a Contentful account and create a new space for our blog.
- Go to Contentful and sign up for a new account. Once signed in, you will be taken to the Contentful dashboard.
-
Click on “Create Empty Space”, give your space a name (e.g., “My Blog”), and select a region. Click on “Create Space” to create the space.
-
Once the space is created, you will be taken to the space dashboard. From the sidebar, click on “Content model” and then click on “Add content type”.
-
Create a content type called “Blog Post” with the following fields:
- Title (Text field)
- Slug (Text field)
- Date (Date and time field)
- Body (Rich text field)
Ensure that “Slug” is set as the “Field ID” for this content type.
-
Click on “Publish” to publish the content model.
-
Now that our content model is set up, let’s create some sample blog posts. From the sidebar, click on “Content” and then “Add blog post”. Enter a title, slug, date, and body content for each blog post. Repeat this step to create multiple blog posts.
-
Once you have created some sample blog posts, click on “Publish” to publish the posts.
-
Finally, we need to obtain the API keys required to access our Contentful data. From the sidebar, click on “Settings” and then “API keys”. Copy the “Space ID” and “Content Delivery API – access token” values.
Step 3: Set Up Contentful Integration
Now that we have our Nuxt.js project and Contentful space set up, let’s integrate them together.
- In your project directory, open the
nuxt.config.js
file. We need to install and configure the@nuxt/content
module to fetch data from Contentful. -
Install the required dependencies by running the following command in your terminal:
npm install @nuxt/contentful --save
- In the
nuxt.config.js
file, add the following code inside themodules
array:
['@nuxt/content']
- Add the following code block at the end of the
nuxt.config.js
file to configure the@nuxt/content
module:
content: {
markdown: {
prism: {
theme: 'prismjs/themes/prism-tomorrow.css'
}
},
fullTextSearchFields: ['title', 'slug'],
api: {
base: 'https://cdn.contentful.com',
space: '<YOUR_SPACE_ID>',
accessToken: '<YOUR_CONTENT_DELIVERY_API_ACCESS_TOKEN>'
}
}
Replace <YOUR_SPACE_ID>
and <YOUR_CONTENT_DELIVERY_API_ACCESS_TOKEN>
with the values you obtained from the Contentful dashboard.
- Restart the development server by stopping the running process and running
npm run dev
again.
Step 4: Fetch Blog Posts
Now that our Contentful integration is set up, let’s fetch the blog posts from Contentful and display them on the blog homepage.
- In your project directory, open the
pages/index.vue
file. -
Replace the existing content inside the
<template>
tag with the following code:
<template>
<div>
<h1>Welcome to My Blog!</h1>
<div v-for="post in blogPosts" :key="post.sys.id">
<h2>{{ post.fields.title }}</h2>
{{ formatDate(post.fields.date) }}
<nuxt-link :to="`/blog/${post.fields.slug}`">Read More</nuxt-link>
</div>
</div>
</template>
- In the
<script>
tag, add the following code:
export default {
async asyncData({ $content }) {
const blogPosts = await $content('blogPost').fetch()
return { blogPosts }
},
methods: {
formatDate(date) {
const options = { year: 'numeric', month: 'long', day: 'numeric' }
return new Date(date).toLocaleDateString(undefined, options)
}
}
}
- Save the changes and refresh the browser. You should now see the list of blog posts fetched from Contentful on the homepage.
Step 5: Generate Dynamic Blog Post Pages
Next, let’s generate dynamic individual blog post pages based on the slugs of the blog posts.
- In your project directory, create a new directory called
blog
inside thepages
directory. -
Inside the
blog
directory, create a new file called_slug.vue
. -
Replace the existing content inside the
_slug.vue
file with the following code:
<template>
<div>
<h1>{{ blogPost.fields.title }}</h1>
<p>Published on {{ formatDate(blogPost.fields.date) }}</p>
<div v-html="blogPost.fields.body"></div>
</div>
</template>
<script>
export default {
async asyncData({ $content, params }) {
const blogPost = await $content('blogPost').where({ fields: { slug: params.slug } }).fetch()
return { blogPost: blogPost[0] }
},
methods: {
formatDate(date) {
const options = { year: 'numeric', month: 'long', day: 'numeric' }
return new Date(date).toLocaleDateString(undefined, options)
}
}
}
</script>
- Save the changes and navigate to any blog post page (e.g.,
http://localhost:3000/blog/<YOUR_BLOG_POST_SLUG>
). You should see the individual blog post page with the content fetched from Contentful.
Congratulations! You have successfully built a blog using Nuxt.js and Contentful. You can now create and manage blog posts in Contentful and they will be dynamically rendered on your blog.
Conclusion
In this tutorial, we learned how to set up a Nuxt.js project, integrate it with Contentful, fetch and display blog posts from Contentful, and generate dynamic individual blog post pages. Nuxt.js and Contentful make it easy to manage and deliver content for your blog, allowing you to focus on creating engaging content for your readers.
Feel free to explore more features of Nuxt.js and Contentful to enhance your blog.