Sometime ago, I was thinking about my internet habit and, more specifically, what I really like when I'm reading stuff. Here’s what I usually do: I run a query, and then I just let myself be guided by the most interesting links. I always find myself reading blog posts about someone’s experience that is entirely unrelated to the query I initially typed.
Blogging is an excellent way to let share experiences, beliefs, or testimonials. And Strapi is useful at helping you create your blog! So, I am pretty sure that you now understand what this post is about. Let’s learn how to create a blog with your favorite tech, Strapi.
If you are familiar with our blog, you must have seen that we've released a series of tutorials on how to make blogs using Strapi with a lot of frontend frameworks:
The goal here in this article is to be able to create a blog website using Strapi as the backend, Nuxt for the frontend, and Apollo for requesting the Strapi API with GraphQL.
Click here to access the source code on GitHub.
To follow this tutorial, you'll need to have latest version of Strapi and Nuxt installed on your computer, but don't worry, we are going to install these together!
You'll also need to install Node.js v14 and that's all.
Since the beta.9, we have an awesome package, create strapi-app
, that allows you to create a Strapi project in seconds without needing to install Strapi globally so let's try it out.
mkdir blog-strapi
cd blog-strapi
yarn create strapi-app backend --quickstart --no-run
This single command line will create all you need for your backend. Make sure to add the --no-run
flag as it will prevent your app from automatically starting the server because
SPOILER ALERT: we need to install some awesome Strapi plugins first.
Now that you know that we need to install some plugins to enhance your app, let's install one of our most popular—the GraphQL plugin:
cd backend
yarn strapi install graphql
yarn develop
Open your Strapi dev server at http://localhost:1337.
Once the installation is complete, you can finally start your Strapi dev server and create your first admin user. That's the one that has all the rights in your application, so please make sure to enter a proper password; (password123) is really not safe.
Nice! Now that Strapi is ready, you are going to create your Nuxt application.
Well, the easiest part has been completed, let's get our hands dirty developing our blog!
./blog-strapi
: yarn create nuxt-app frontend
Note: The terminal will prompt for some details about your project. As they are not really relevant to our blog, you can ignore them. I strongly advise you to read the documentation, though. So go ahead, enjoy yourself, and press enter all the way!
Again, once the installation is over, you can start your front-end app to make sure everything is ok.
cd frontend
yarn dev
As you might want people to read your blog or to make it "cute & pretty", we will use a popular CSS framework for styling: UIkit and Apollo GraphQL to query Strapi with GraphQL.
Make sure you are in the frontend
folder before running the following commands.
// Ctrl + C to close Nuxt.js process
yarn add @nuxtjs/apollo
Apollo Client is a fully-featured caching GraphQL client with integrations for Vue, React, and more. It allows you to easily build UI components that fetch data via GraphQL.
@nuxtjs/apollo
to the modules section with Apollo configuration in ./frontend/nuxt.config.js
1 // nuxt.config.js
2
3 export default {
4 modules: [
5 '@nuxtjs/apollo',
6 ],
7
8 apollo: {
9 clientConfigs: {
10 default: {
11 httpEndpoint: process.env.BACKEND_URL || "http://localhost:1337/graphql",
12 }
13 }
14 },
15 }
We'll also need to use an env variable for our Strapi base url, add a new env
section at the end of nuxt.config.js
file:
1 // nuxt.config.js
2
3 export default {
4 env: {
5 strapiBaseUri: process.env.API_URL || "http://localhost:1337"
6 },
7 }
Great! Apollo is ready now. 🚀
yarn add uikit
Now you need to initialize UIkit's JS in your Nuxt application. You are going to do this by creating a new plugin.
./frontend/plugins/uikit.js
file and copy/paste the following code:1 import Vue from 'vue'
2
3 import UIkit from 'uikit/dist/js/uikit-core'
4 import Icons from 'uikit/dist/js/uikit-icons'
5
6 UIkit.use(Icons)
7 UIkit.container = '#__nuxt'
8
9 Vue.prototype.$uikit = UIkit
10
11Add the following sections to your `nuxt.config.js` file:
12
13 // nuxt.config.js
14
15 export default {
16 css: [
17 'uikit/dist/css/uikit.min.css',
18 '@assets/css/main.css'
19 ],
20
21 plugins: [
22 { src: '~/plugins/uikit.js', ssr: false }
23 ]
24 }
25
26As you can see, you are including both UIkit and `main.css` files! We just need to create the `./frontend/assets/css/main.css` file.
27
28 a {
29 text-decoration: none;
30 }
31
32 h1 {
33 font-family: Staatliches;
34 font-size: 120px;
35 }
36
37 #category {
38 font-family: Staatliches;
39 font-weight: 500;
40 }
41
42 #title {
43 letter-spacing: .4px;
44 font-size: 22px;
45 font-size: 1.375rem;
46 line-height: 1.13636;
47 }
48
49 #banner {
50 margin: 20px;
51 height: 800px;
52 }
53
54 #editor {
55 font-size: 16px;
56 font-size: 1rem;
57 line-height: 1.75;
58 }
59
60 .uk-navbar-container {
61 background: #fff !important;
62 font-family: Staatliches;
63 }
64
65 img:hover {
66 opacity: 1;
67 transition: opacity 0.25s cubic-bezier(0.39, 0.575, 0.565, 1);
68 }
69
70**Note:** You don't need to understand what's in this file. It's just some styling ;)
71
72Let's add a beautiful font [Staatliches](https://fonts.google.com/specimen/Staatliches) to the project! Add the following code to your `link` section in your `nuxt.config.js`
73
74 // nuxt.config.js
75
76 export default {
77 link: [
78 { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Staatliches' }
79 ],
80 }
Perfect! Run yarn dev
to restart your server and be prepared to get impressed by the front page of your application!
Awesome! It's time to structure our code a little bit.
Finally, we are now going to create the data structure of our articles by creating an Article content type.
Now you'll be asked to create all the fields for your content-type
Press Save! Here you go, your “Article” content type has been created.
You may want to create your first article, but we have one thing to do before that: Grant access to the article content type.
Awesome! You should be ready to create your first article right now and fetch it on the GraphQL Playground.
Now, create your first article ! Here's an example:
Great! Now you may want to reach the moment when you can actually fetch your articles through the API! Go to http://localhost:1337/api/articles Isn't that cool!
You can also play with the GraphQL Playground.
You may want to assign a category to your articles (news, trends, opinion). You are going to do this by creating another content type in Strapi.
Create a “Category” content type with the following field
Press save!
Create a new field in the Article content type which is a Relation Category has many Articles
like below:
Again, open Settings then Roles and click on the “Public” role, then check the category find
and findone
routes and save.
Now you'll be able to select a category for your article in the right sidebox.
Now that we are good with Strapi, let's work on the frontend part!
You can change the default layout of the Nuxt.js application by creating your own layouts/default.vue
file.
1 <template>
2 <div>
3 <nav class="uk-navbar-container" uk-navbar>
4 <div class="uk-navbar-left">
5 <ul class="uk-navbar-nav">
6 <li>
7 <a href="#modal-full" uk-toggle
8 ><span uk-icon="icon: table"></span
9 ></a>
10 </li>
11 <li>
12 <a href="/">Strapi Blog </a>
13 </li>
14 </ul>
15 </div>
16 <div class="uk-navbar-right">
17 <ul class="uk-navbar-nav">
18 <li v-for="category in categories.data" :key="category.id">
19 <NuxtLink
20 :to="{ name: 'categories-id', params: { id: category.id } }"
21 >{{ category.attributes.name }}
22 </NuxtLink>
23 </li>
24 </ul>
25 </div>
26 </nav>
27 <div id="modal-full" class="uk-modal-full" uk-modal>
28 <div class="uk-modal-dialog">
29 <button
30 class="uk-modal-close-full uk-close-large"
31 type="button"
32 uk-close
33 ></button>
34 <div
35 class="uk-grid-collapse uk-child-width-1-2@s uk-flex-middle"
36 uk-grid
37 >
38 <div
39 class="uk-background-cover"
40 style="
41 background-image: url('https://images.unsplash.com/photo-1493612276216-ee3925520721?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=3308&q=80 3308w');
42 "
43 uk-height-viewport
44 ></div>
45 <div class="uk-padding-large">
46 <h1 style="font-family: Staatliches">Strapi blog</h1>
47 <div class="uk-width-1-2@s">
48 <ul class="uk-nav-primary uk-nav-parent-icon" uk-nav>
49 <li v-for="category in categories.data" :key="category.id">
50 <NuxtLink
51 class="uk-modal-close"
52 :to="{ name: 'categories-id', params: { id: category.id } }"
53 >{{ category.attributes.name }}
54 </NuxtLink>
55 </li>
56 </ul>
57 </div>
58 <p class="uk-text-light">Built with strapi</p>
59 </div>
60 </div>
61 </div>
62 </div>
63 <Nuxt />
64 </div>
65 </template>
66
67 <script>
68 export default {
69 data() {
70 return {
71 categories: {
72 data: [],
73 },
74 };
75 },
76 };
77 </script>
As you can see, categories
list is empty. In fact, you want to be able to list every category in your navbar. To do this, we need to fetch them with Apollo, let's write the query!
apollo/queries/category
folder and a categories.gql
file inside with the following code:1 query {
2 categories {
3 data {
4 id
5 attributes {
6 name
7 }
8 }
9 }
10 }
script
tag in your default.vue
file by the following code:1 <script>
2 import categoriesQuery from "~/apollo/queries/category/categories";
3
4 export default {
5 data() {
6 return {
7 categories: {
8 data: [],
9 },
10 };
11 },
12 apollo: {
13 categories: {
14 prefetch: true,
15 query: categoriesQuery,
16 },
17 },
18 };
19 </script>
Note: The current code is not suited to display a lot of categories as you may encounter a UI issue.
Since this blog post is supposed to be short, I will let you improve the code to maybe add a lazy load or something. For now, the links are not working, you'll work on it later on the tutorial :)
This component will display all your articles on different pages, so listing them through a component is not a bad idea.
components/Articles.vue
file containing the following:1 <template>
2 <div>
3 <div class="uk-child-width-1-2" uk-grid>
4 <div>
5 <router-link
6 v-for="article in leftArticles"
7 :to="{ name: 'articles-id', params: { id: article.id } }"
8 class="uk-link-reset"
9 :key="article.id"
10 >
11 <div class="uk-card uk-card-muted">
12 <div v-if="article.attributes.image.data" class="uk-card-media-top">
13 <img
14 :src="api_url + article.attributes.image.data.attributes.url"
15 alt=""
16 height="100"
17 />
18 </div>
19 <div class="uk-card-body">
20 <p
21 id="category"
22 v-if="article.attributes.category.data"
23 class="uk-text-uppercase"
24 >
25 {{ article.attributes.category.data.attributes.name }}
26 </p>
27 <p id="title" class="uk-text-large">
28 {{ article.attributes.title }}
29 </p>
30 </div>
31 </div>
32 </router-link>
33 </div>
34 <div>
35 <div class="uk-child-width-1-2@m uk-grid-match" uk-grid>
36 <router-link
37 v-for="article in rightArticles"
38 :to="{ name: 'articles-id', params: { id: article.id } }"
39 class="uk-link-reset"
40 :key="article.id"
41 >
42 <div class="uk-card uk-card-muted">
43 <div
44 v-if="article.attributes.image.data"
45 class="uk-card-media-top"
46 >
47 <img
48 :src="api_url + article.attributes.image.data.attributes.url"
49 alt=""
50 height="100"
51 />
52 </div>
53 <div class="uk-card-body">
54 <p
55 id="category"
56 v-if="article.attributes.category.data"
57 class="uk-text-uppercase"
58 >
59 {{ article.attributes.category.data.attributes.name }}
60 </p>
61 <p id="title" class="uk-text-large">
62 {{ article.attributes.title }}
63 </p>
64 </div>
65 </div>
66 </router-link>
67 </div>
68 </div>
69 </div>
70 </div>
71 </template>
72
73 <script>
74 export default {
75 data() {
76 return {
77 api_url: process.env.strapiBaseUri,
78 };
79 },
80 props: {
81 articles: Object,
82 },
83 computed: {
84 leftArticlesCount() {
85 return Math.ceil(this.articles.data.length / 5);
86 },
87 leftArticles() {
88 return this.articles.data.slice(0, this.leftArticlesCount);
89 },
90 rightArticles() {
91 return this.articles.data.slice(
92 this.leftArticlesCount,
93 this.articles.length
94 );
95 },
96 },
97 };
98 </script>
As you can see, you are fetching articles thanks to a GraphQl query, let's write it!
apollo/queries/article/articles.gql
file containing the following:1 query {
2 articles {
3 data {
4 id
5 attributes {
6 title
7 content
8 image {
9 data {
10 attributes {
11 url
12 }
13 }
14 }
15 category {
16 data {
17 attributes {
18 name
19 }
20 }
21 }
22 }
23 }
24 }
25 }
Awesome! Now, you can create your main page.
You want to list every article on your index page, let's use our new component! Update the code in your pages/index.vue
file with:
1 <template>
2 <div>
3 <div class="uk-section">
4 <div class="uk-container uk-container-large">
5 <h1>Strapi blog</h1>
6 <Articles :articles="articles"></Articles>
7 </div>
8 </div>
9 </div>
10 </template>
11
12 <script>
13 import articlesQuery from "~/apollo/queries/article/articles";
14 import Articles from "~/components/Articles";
15 export default {
16 data() {
17 return {
18 articles: {
19 data: [],
20 },
21 };
22 },
23 components: {
24 Articles,
25 },
26 apollo: {
27 articles: {
28 prefetch: true,
29 query: articlesQuery,
30 },
31 },
32 };
33 </script>
Great! You have now reached the moment when you can actually fetch your articles through the GraphQL API!
You can see that if you click on the article, there is nothing. Let's create the article page together!
pages/articles
folder and a new _id.vue
file inside containing the following:1 <template>
2 <div>
3 <div
4 v-if="article.data.attributes.image.data"
5 id="banner"
6 class="uk-height-small uk-flex uk-flex-center uk-flex-middle uk-background-cover uk-light uk-padding"
7 :data-src="api_url + article.data.attributes.image.data.attributes.url"
8 uk-img
9 >
10 <h1>{{ article.data.attributes.title }}</h1>
11 </div>
12 <div class="uk-section">
13 <div class="uk-container uk-container-small">
14 <div v-if="article.data.attributes.content" id="editor">
15 {{ article.data.attributes.content }}
16 </div>
17 <p v-if="article.data.publishedAt">
18 {{ article.data.attributes.publishedAt }}
19 </p>
20 </div>
21 </div>
22 </div>
23 </template>
24
25 <script>
26 import articleQuery from "~/apollo/queries/article/article";
27
28 export default {
29 data() {
30 return {
31 article: {
32 data: [],
33 },
34 api_url: process.env.strapiBaseUri,
35 };
36 },
37 apollo: {
38 article: {
39 prefetch: true,
40 query: articleQuery,
41 variables() {
42 return { id: parseInt(this.$route.params.id) };
43 },
44 },
45 },
46 };
47 </script>
Here you are fetching just one article, let's write the query behind it! Create a apollo/queries/article/article.gql
containing the following:
1 query Articles($id: ID!) {
2 article(id: $id) {
3 data {
4 id
5 attributes {
6 title
7 content
8 image {
9 data {
10 attributes {
11 url
12 }
13 }
14 }
15 publishedAt
16 }
17 }
18 }
19 }
Alright, you may want to display your content as Markdown
markdownit
with yarn add @nuxtjs/markdownit
date-fns
with yarn add @nuxtjs/date-fns
nuxt.config.js
file and add the markdownit object configuration just under // nuxt.config.js
.1 export default {
2 // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
3 buildModules: [
4 '@nuxtjs/date-fns',
5 ],
6
7 // Modules: https://go.nuxtjs.dev/config-modules
8 modules: [
9 '@nuxtjs/apollo',
10 '@nuxtjs/markdownit'
11 ],
12
13 // [optional] markdownit options
14 // See https://github.com/markdown-it/markdown-it
15 markdownit: {
16 preset: 'default',
17 linkify: true,
18 breaks: true,
19 injected: true
20 }
21 }
_id.vue
file by replacing the line responsible for displaying the content. // pages/articles/_id.vue
1<div v-if="article.content" id="editor" v-html="$md.render(article.content)"></div>
Let's create a page for each category now! Create a pages/categories
folder and a _id.vue
file inside containing the following:
1 <template>
2 <div>
3 <client-only>
4 <div class="uk-section">
5 <div class="uk-container uk-container-large">
6 <h1>{{ category.data.attributes.name }}</h1>
7 <Articles :articles="category.data.attributes.articles"></Articles>
8 </div>
9 </div>
10 </client-only>
11 </div>
12 </template>
13
14 <script>
15 import articlesQuery from "~/apollo/queries/article/articles-categories";
16 import Articles from "~/components/Articles";
17 export default {
18 data() {
19 return {
20 category: {
21 data: [],
22 },
23 };
24 },
25 components: {
26 Articles,
27 },
28 apollo: {
29 category: {
30 prefetch: true,
31 query: articlesQuery,
32 variables() {
33 return { id: parseInt(this.$route.params.id) };
34 },
35 },
36 },
37 };
38 </script>
And don't forget the query! Create a apollo/queries/article/articles-categories.gql
containing the following:
1 query Category($id: ID!){
2 category(id: $id) {
3 data {
4 attributes {
5 name
6 articles {
7 id
8 data {
9 attributes {
10 title
11 content
12 image {
13 data {
14 attributes {
15 url
16 }
17 }
18 }
19 category {
20 data {
21 attributes {
22 name
23 }
24 }
25 }
26 }
27 }
28 }
29 }
30 }
31 }
32 }
Awesome! You can now navigate through categories :)
Huge congrats, you successfully achieved this tutorial. I hope you enjoyed it!
Click here to access the source code on GitHub.
Still hungry?
Feel free to add additional features, adapt this project to your own needs, and give your feedback in the comments section.
If you want to deploy your application, check the documentation.
A Front-end developer, Acquia Certified and React.js expert.