In the first part of this series, we learned How to set up a blog using Netlify CMS. In this part, we'll see how to integrate the content with the NuxtJS frontend.
Setting up Vuex store to collect data
In your store
directory create a file named index.js
and add the following content to it.
export const state = () => ({
blogPosts: []
});
export const mutations = {
setBlogPosts(state, list) {
state.blogPosts = list;
}
};
export const actions = {
async nuxtServerInit({ commit }) {
let files = await require.context(
"~/assets/content/blog/",
false,
/\.json$/
);
let blogPosts = files.keys().map(key => {
let res = files(key);
res.slug = key.slice(2, -5);
return res;
});
await commit("setBlogPosts", blogPosts);
}
};
Now we can use the content in our Vue files. To learn more about Vuex store in NuxtJS see this.
To use
nuxtServerInit
action your mode should be universal innuxt.config.js
Getting data from Vuex store
In pages/
directory in index.vue
file add the following content -
<template>
<div>
<h1>Blog Posts</h1>
<div class="blogs">
<ul class="blog" v-for="blog in blogPosts" :key="blog.slug">
<li class="blog__title">
<nuxt-link :to="`/blog/${blog.slug}`">
<h2>{{ blog.title }}</h2>
</nuxt-link>
</li>
</ul>
</div>
</div>
</template>
<script>
export default {
computed: {
// Fetching all posts data
blogPosts() {
return this.$store.state.blogPosts;
},
},
};
</script>
Now if you go to your http://localhsot:3000/
you'll see all the blog posts listed there.
Getting a single blog post
Use the following code to get the single blog post
export default {
// Fetching Single BlogPost
async asyncData({ params, payload }) {
if (payload) return { blogPost: payload };
else
return {
blogPost: await require(`~/assets/content/blog/${params.blog}.json`)
};
}
};
Now the blogPost
variable is available in the template.
<div class="blog">
<img v-bind:src="blogPost.thumbnail" class="blog__featured-img" />
<h1 class="blog__title">{{blogPost.title}}</h1>
<div class="blog__date">{{blogPost.date}}</div>
<div class="blog__body" v-html="$md.render(blogPost.body)"></div>
</div>
Since the body contains the markdown, we need to install a package to display the markdown content.
npm install @nuxtjs/markdownit
In your nuxt.config.js
add the following line
...
modules: ["@nuxtjs/markdownit"],
...
Generating pages
To render the site as static, you need to add a generate property in your nuxt.config.js
generate: {
routes: function() {
const fs = require("fs");
const path = require("path");
return fs.readdirSync("./assets/content/blog").map(file => {
return {
route: `/blog/${path.parse(file).name}`, // Return the slug
payload: require(`./assets/content/blog/${file}`)
};
});
}
},
I have also created a repository to get you started with the NuxtJS blog.
frikishaan / nuxt-netlify-cms-starter-template
This is a starter files for creating Nuxt.js based blogs using Netlify CMS.
Reference - Netlify CMS Docs
Top comments (6)
You need to check out nuxt/content. Works perfectly with this kind of setup and you can write the posts in markdown :)
Yeah definitely having come across this I would say the same. I've been playing with nuxt/content module for site idea it's great to be able to write content in markdown. I'm very much a beginner and need to learn more about it but I'm liking it so far just I've a lot of folders in my structure need to try and figure out best why to do it but its been fun. And setting it up with netlifycms was straightforward too.
Thanks for sharing this.
Hello, thank you so much for the syntethic and precise post! I was wondering if you have time to share how to handle categories and category pages.
Thanks, Martino for reading the post!
In order to handle categories, you have to create a new collection and create a new field with relation type widget (see links) in the blog collection.
I hope this will help.
Great, thanks