{"id":4144,"date":"2023-11-04T23:14:05","date_gmt":"2023-11-04T23:14:05","guid":{"rendered":"http:\/\/localhost:10003\/how-to-build-a-blog-with-nuxt-js-and-contentful\/"},"modified":"2023-11-05T05:47:59","modified_gmt":"2023-11-05T05:47:59","slug":"how-to-build-a-blog-with-nuxt-js-and-contentful","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-build-a-blog-with-nuxt-js-and-contentful\/","title":{"rendered":"How to Build a Blog with Nuxt.js and Contentful"},"content":{"rendered":"
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.<\/p>\n
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!<\/p>\n
To follow along with this tutorial, you will need the following:<\/p>\n
To start, let’s create a new Nuxt.js project. Open your terminal and navigate to your desired project directory. Run the following command:<\/p>\n
npx create-nuxt-app blog\n<\/code><\/pre>\nThis will prompt you to answer a few questions to generate the project. Choose the default options for now.<\/p>\n
Once the project is generated, navigate into the project directory:<\/p>\n
cd blog\n<\/code><\/pre>\nNow, start the development server:<\/p>\n
npm run dev\n<\/code><\/pre>\nYou 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.<\/p>\n
Step 2: Set Up Contentful<\/h2>\n
Next, we need to set up a Contentful account and create a new space for our blog.<\/p>\n
\n- Go to Contentful<\/a> and sign up for a new account. Once signed in, you will be taken to the Contentful dashboard.<\/p>\n<\/li>\n
- \n
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.<\/p>\n<\/li>\n
- \n
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”.<\/p>\n<\/li>\n
- \n
Create a content type called “Blog Post” with the following fields:<\/p>\n
\n- Title (Text field)<\/li>\n
- Slug (Text field)<\/li>\n
- Date (Date and time field)<\/li>\n
- Body (Rich text field)<\/li>\n<\/ul>\n
Ensure that “Slug” is set as the “Field ID” for this content type.<\/p>\n<\/li>\n
- \n
Click on “Publish” to publish the content model.<\/p>\n<\/li>\n
- \n
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.<\/p>\n<\/li>\n
- \n
Once you have created some sample blog posts, click on “Publish” to publish the posts.<\/p>\n<\/li>\n
- \n
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.<\/p>\n<\/li>\n<\/ol>\n
Step 3: Set Up Contentful Integration<\/h2>\n
Now that we have our Nuxt.js project and Contentful space set up, let’s integrate them together.<\/p>\n
\n- In your project directory, open the
nuxt.config.js<\/code> file. We need to install and configure the @nuxt\/content<\/code> module to fetch data from Contentful.<\/p>\n<\/li>\n- \n
Install the required dependencies by running the following command in your terminal:<\/p>\n<\/li>\n<\/ol>\n
npm install @nuxt\/contentful --save\n<\/code><\/pre>\n\n- In the
nuxt.config.js<\/code> file, add the following code inside the modules<\/code> array:<\/li>\n<\/ol>\n['@nuxt\/content']\n<\/code><\/pre>\n\n- Add the following code block at the end of the
nuxt.config.js<\/code> file to configure the @nuxt\/content<\/code> module:<\/li>\n<\/ol>\ncontent: {\n markdown: {\n prism: {\n theme: 'prismjs\/themes\/prism-tomorrow.css'\n }\n },\n fullTextSearchFields: ['title', 'slug'],\n api: {\n base: 'https:\/\/cdn.contentful.com',\n space: '<YOUR_SPACE_ID>',\n accessToken: '<YOUR_CONTENT_DELIVERY_API_ACCESS_TOKEN>'\n }\n}\n<\/code><\/pre>\nReplace <YOUR_SPACE_ID><\/code> and <YOUR_CONTENT_DELIVERY_API_ACCESS_TOKEN><\/code> with the values you obtained from the Contentful dashboard.<\/p>\n\n- Restart the development server by stopping the running process and running
npm run dev<\/code> again.<\/li>\n<\/ol>\nStep 4: Fetch Blog Posts<\/h2>\n
Now that our Contentful integration is set up, let’s fetch the blog posts from Contentful and display them on the blog homepage.<\/p>\n
\n- In your project directory, open the
pages\/index.vue<\/code> file.<\/p>\n<\/li>\n- \n
Replace the existing content inside the <template><\/code> tag with the following code:<\/p>\n<\/li>\n<\/ol>\n<template>\n <div>\n <h1>Welcome to My Blog!<\/h1>\n <div v-for=\"post in blogPosts\" :key=\"post.sys.id\">\n <h2>{{ post.fields.title }}<\/h2>\n {{ formatDate(post.fields.date) }}\n <nuxt-link :to=\"`\/blog\/${post.fields.slug}`\">Read More<\/nuxt-link>\n <\/div>\n <\/div>\n<\/template>\n<\/code><\/pre>\n\n- In the
<script><\/code> tag, add the following code:<\/li>\n<\/ol>\nexport default {\n async asyncData({ $content }) {\n const blogPosts = await $content('blogPost').fetch()\n\n return { blogPosts }\n },\n\n methods: {\n formatDate(date) {\n const options = { year: 'numeric', month: 'long', day: 'numeric' }\n return new Date(date).toLocaleDateString(undefined, options)\n }\n }\n}\n<\/code><\/pre>\n\n- Save the changes and refresh the browser. You should now see the list of blog posts fetched from Contentful on the homepage.<\/li>\n<\/ol>\n
Step 5: Generate Dynamic Blog Post Pages<\/h2>\n
Next, let’s generate dynamic individual blog post pages based on the slugs of the blog posts.<\/p>\n
\n- In your project directory, create a new directory called
blog<\/code> inside the pages<\/code> directory.<\/p>\n<\/li>\n- \n
Inside the blog<\/code> directory, create a new file called _slug.vue<\/code>.<\/p>\n<\/li>\n- \n
Replace the existing content inside the _slug.vue<\/code> file with the following code:<\/p>\n<\/li>\n<\/ol>\n<template>\n <div>\n <h1>{{ blogPost.fields.title }}<\/h1>\n <p>Published on {{ formatDate(blogPost.fields.date) }}<\/p>\n <div v-html=\"blogPost.fields.body\"><\/div>\n <\/div>\n<\/template>\n\n<script>\nexport default {\n async asyncData({ $content, params }) {\n const blogPost = await $content('blogPost').where({ fields: { slug: params.slug } }).fetch()\n\n return { blogPost: blogPost[0] }\n },\n\n methods: {\n formatDate(date) {\n const options = { year: 'numeric', month: 'long', day: 'numeric' }\n return new Date(date).toLocaleDateString(undefined, options)\n }\n }\n}\n<\/script>\n<\/code><\/pre>\n\n- Save the changes and navigate to any blog post page (e.g.,
http:\/\/localhost:3000\/blog\/<YOUR_BLOG_POST_SLUG><\/code>). You should see the individual blog post page with the content fetched from Contentful.<\/li>\n<\/ol>\nCongratulations! 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.<\/p>\n
Conclusion<\/h2>\n
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.<\/p>\n
Feel free to explore more features of Nuxt.js and Contentful to enhance your blog.<\/p>\n","protected":false},"excerpt":{"rendered":"
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 Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[872,599,49,1458,870,1457],"yoast_head":"\nHow to Build a Blog with Nuxt.js and Contentful - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n