Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 457

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T11:04:08+00:00 2024-11-25T11:04:08+00:00

How to setup a blog with headless CMS (Strapi) and Nextjs

  • 62k

Introduction

If we want to setup a blog, there are many options, for enterprise level, you can use WordPress, which helps you to deliver a blog pretty fast, or you can build it using something like Hexo, which has pretty much everything built-in, you only need to tune config and write content in markdown and host that on Heroku or Vercel, Netllify. Or, you can start everything from frontend to backend and host it on your own server, and this is what this blog will talk about.

The previous blog of my current company was hosted on WordPress, which costs and under a different domain from what we want. Our expected domain is xxx.page, and it was on xxx.app domain, which is on 2 different servers, in order to migrate it, we will need to install PHP language and bunch of WordPress plugins, some of them could cause severe security issues.

To avoid such question and extend flexibility of our company blog for better SEO purpose, we decide to build it using Nextjs and Strapi, a headless content management system (CMS) and host that on the xxx.page server.

To do that, you will have to refer to docs first:

  • Nextjs
  • Strapi

from basic introduction we can understand what we can do with this suite:

  • Nextjs provides intelligent default setup for SSR, SSG purpose, along with default optimization function, the default Rust-based compiler provides 5x times faster than babel

  • Strapi provides a built-in UI dashboard that allows user to do everything you want with content, and since it is a Node project, you can add more configuration to it, like image compression function, database connection, email channel, etc.

the result blog for Desty.app

Image description

Strapi as backend

strapi

In this case, Strapi is the backend where content is stored. We decide to install docker image of Strapi directly on the server, and if more config needed to be added in code, we can do it in server directly.

Doing that can actually save lot of trouble, there is no need for writing code in Strapi to connect to database, since we store files in server directly, like WordPress, and else functions can all be done directly on its dashboard.

You can choose other headless CMS, well, Jamstack's headless cms section is a good place to offer those options

after hosting your Strapi project and make couple content types and publish, for example, articles or posts, or else, you can access to the json data by checking the url xxx.xxx/[content type name], for example, mine is xxx.xxx/articles and xxx.xxx/categories.

in this case the client side can fetch data directly, or if you want your data private, you can setup authentication in Strapi's code and change the role of public access to only authenticated users, and you can define authenticated users in UI dashboard directly.

Nextjs as frontend

nextjs

Nextjs is the frontend, and we use static generator to do so, we will use Jenkins to do a daily trigger everyday 4:00pm to build the entire blog so everything is included in the build, and thus providing better user interaction since it is fast.

First, lets take a look of hosted Strapi:

ui entry

after clicking the open administrator and signup and login, you will see:

ui dashboard

The blank Strapi project should not have those collection types on the left side, and you can feel free to build whatever content type you want in Content-Types Builder, feel free to explore Strapi doc here

Here I will demonstrate how to do the client side:

  1. start a Nextjs project by using
npx create-next-app@latest # or yarn create next-app 
Enter fullscreen mode Exit fullscreen mode

  1. define the basic layout

well, this is not strictly required in the first place, you can extract layout logic out later, along with some css files

  1. define routes

the basic blog route will be

  • /blog –> root
  • /blog/category/[name] –> category name, displays articles under that category
  • /blog/[slug] –> article page with unique slug
  1. data fetching

you can define the data fetching in lib folder, naming a file like api.js

import axios from "axios";  async function getStrapiAuthToken() {   const { data } = await axios.post(     process.env.NEXT_PUBLIC_STRAPI_API_URL + "/auth/local",     {       identifier: process.env.STRAPI_IDENTIFIER,       password: process.env.STRAPI_PASSWORD,     }   );   return data.jwt; }  export function getStrapiURL(path = "") {   const p = `${process.env.NEXT_PUBLIC_STRAPI_API_URL}${path}`;   return p; }  // Helper to make GET requests to Strapi export async function fetchAPI(path) {   const requestUrl = getStrapiURL(path);    const { data } = await axios.get(requestUrl);    return data.reverse(); }  
Enter fullscreen mode Exit fullscreen mode

then you can fetch articles using this method along with Nextjs's provided data fetching methods such as getStaticProps and getStaticPaths or getServerSideProps, for details, check this page

one example of fetching articles would be

// for root path '/', getStaticPaths cannot be used, // only use it in places like /blog/[slug] export async function getStaticPaths() {   const articles = await fetchAPI("/articles");    return {     paths: articles.map((article) => ({       params: {         slug: article.slug,       },     })),     fallback: "blocking",   }; }  // this is required export async function getStaticProps({ params }) {   const articles = await fetchAPI(`/articles?slug=${params.slug}`);   const categories = await fetchAPI("/categories");    return {     props: { article: articles[0], categories },     revalidate: 10,   }; } 
Enter fullscreen mode Exit fullscreen mode

the default gives only limited number of entries that can be fetched, you can add /articles?_limit=-1 to replace /articles to access unlimited number of entries.

Right now we have a blog that requires building every time, but if we update like 50 blogs per day, or like Twitter it may have up to 500M new tweets per day, you will have to define skeleton and let articles or tweets to be statically generated now time, if they are not included in the build. this can be explained later as I am also working on it.

SEO and Optimization

till here we have not done well on SEO and optimization, for there are lots of things to optimize.

I would recommend using Google's PageSpeed Insights, or easier, using Google's lighthouse tool which you just install as google plugin and can run it by single click

There are couple things we need to do better here

  • optimize data fetching
  • use next-gen image format (AVIF or WebP)
  • details such as adding lang to html, having alt in <img /> tag, etc.

you can check how lighthouse or PageSpeed index does the calculation, and then you can optimize based on them

blognextjsstrapiwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.