{"id":4202,"date":"2023-11-04T23:14:08","date_gmt":"2023-11-04T23:14:08","guid":{"rendered":"http:\/\/localhost:10003\/how-to-build-a-blog-with-next-js-and-strapi\/"},"modified":"2023-11-05T05:47:56","modified_gmt":"2023-11-05T05:47:56","slug":"how-to-build-a-blog-with-next-js-and-strapi","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-build-a-blog-with-next-js-and-strapi\/","title":{"rendered":"How to Build a Blog with Next.js and Strapi"},"content":{"rendered":"
In this tutorial, we will learn how to build a blog using Next.js, a popular React framework for server-side rendering, and Strapi, an open-source headless CMS. By the end of this tutorial, you will have a fully functional blog with Next.js as the frontend and Strapi as the backend API.<\/p>\n
Before we begin, make sure you have the following prerequisites installed on your system:<\/p>\n
First, let’s set up the backend using Strapi. Strapi provides a simple and user-friendly way to create APIs for your projects.<\/p>\n
To create a new Strapi project, open a terminal and run the following command:<\/p>\n
npx create-strapi-app my-blog --quickstart\n<\/code><\/pre>\nThis will create a new Strapi project in a directory called my-blog<\/code> using the quickstart template.<\/p>\nStep 2: Configure the database<\/h3>\n
By default, Strapi uses SQLite as the database for development. If you want to use a different database, you can configure it in the .env<\/code> file located in the root directory of your project.<\/p>\nFor example, if you want to use MongoDB, open the .env<\/code> file and update the DATABASE_URL<\/code> variable:<\/p>\nDATABASE_URL=mongodb:\/\/localhost:27017\/my-blog\n<\/code><\/pre>\nStep 3: Start the Strapi server<\/h3>\n
To start the Strapi server, navigate to the project directory and run the following command:<\/p>\n
cd my-blog\nnpm run develop\n<\/code><\/pre>\nThis will start the Strapi server on http:\/\/localhost:1337`. You can access the Strapi admin panel by visiting<\/code>http:\/\/localhost:1337\/admin`.<\/p>\nStep 4: Create the content types<\/h3>\n
In the Strapi admin panel, go to the Content-Types Builder<\/strong> section and create two new content types: Article<\/code> and Category<\/code>.<\/p>\nFor the Article<\/code> content type, add the following fields:<\/p>\n\n- Title (Text)<\/li>\n
- Content (Rich Text)<\/li>\n
- Category (Relation with the
Category<\/code> content type)<\/li>\n<\/ul>\nFor the Category<\/code> content type, add a single field:<\/p>\n\n- Name (Text)<\/li>\n<\/ul>\n
Save the changes and go back to the Plugins<\/strong> section. Install and enable the GraphQL<\/strong> plugin.<\/p>\nStep 5: Generate the GraphQL schema<\/h3>\n
To generate the GraphQL schema for your Strapi project, run the following command in the project directory:<\/p>\n
npm run build\n<\/code><\/pre>\nThis will generate the schema at .\/.tmp\/graphql\/schema.graphql<\/code>.<\/p>\nWith the Strapi backend set up, we can now move on to building the frontend using Next.js.<\/p>\n
Setting up the Frontend with Next.js<\/h2>\n
Next.js is a powerful React framework that enables server-side rendering and other advanced features to build fast and scalable applications.<\/p>\n
Step 1: Create a new Next.js project<\/h3>\n
To create a new Next.js project, open a terminal and run the following command:<\/p>\n
npx create-next-app my-blog\n<\/code><\/pre>\nThis will create a new Next.js project in a directory called my-blog<\/code>.<\/p>\nStep 2: Install dependencies<\/h3>\n
Navigate to the project directory and install the required dependencies:<\/p>\n
cd my-blog\nnpm install axios graphql-request\n<\/code><\/pre>\nWe will use the axios<\/code> library to make HTTP requests to the Strapi API and the graphql-request<\/code> library to query our GraphQL API.<\/p>\nStep 3: Configure environment variables<\/h3>\n
Create a new .env.local<\/code> file in the root directory of your project and add the following variables:<\/p>\nNEXT_PUBLIC_STRAPI_API_URL=http:\/\/localhost:1337\/graphql\n<\/code><\/pre>\nThis file will contain environment-specific variables, such as the URL to your Strapi API.<\/p>\n
Step 4: Create API functions<\/h3>\n
In the lib<\/code> directory of your project, create a new file called api.js<\/code> and add the following code:<\/p>\nimport { GraphQLClient } from 'graphql-request';\n\nexport async function request(query, variables = {}) {\n const endpoint = process.env.NEXT_PUBLIC_STRAPI_API_URL;\n const client = new GraphQLClient(endpoint);\n\n return client.request(query, variables);\n}\n<\/code><\/pre>\nThis code sets up a GraphQL client using the graphql-request<\/code> library and exports a function to make requests to the Strapi API.<\/p>\nStep 5: Fetch articles from the API<\/h3>\n
In the pages<\/code> directory of your project, create a new file called index.js<\/code> and add the following code:<\/p>\nimport { request } from '..\/lib\/api';\n\nexport default function Home({ articles }) {\n return (\n <div>\n <h1>My Blog<\/h1>\n <ul>\n {articles.map((article) => (\n <li key={article.id}>\n <h2>{article.title}<\/h2>\n {article.content}\n <\/li>\n ))}\n <\/ul>\n <\/div>\n );\n}\n\nexport async function getStaticProps() {\n const query = `\n query Articles {\n articles {\n id\n title\n content\n }\n }\n `;\n\n const { articles } = await request(query);\n\n return {\n props: {\n articles,\n },\n };\n}\n<\/code><\/pre>\nThis code fetches the articles from the Strapi API using the request<\/code> function we defined earlier and displays them on the page.<\/p>\nStep 6: Start the Next.js development server<\/h3>\n
To start the Next.js development server, run the following command in the project directory:<\/p>\n
npm run dev\n<\/code><\/pre>\nThis will start the Next.js server on `http:\/\/localhost:3000`. If you visit this URL in your browser, you should see the list of articles fetched from the Strapi API.<\/p>\n
Adding Dynamic Routes<\/h2>\n
Next.js allows you to create dynamic routes by using square brackets []<\/code> in the filename. Let’s add dynamic routes to display individual articles.<\/p>\nStep 1: Create a new file for the dynamic route<\/h3>\n
In the pages<\/code> directory of your project, create a new directory called articles<\/code>. Inside the articles<\/code> directory, create a file called [slug].js<\/code> and add the following code:<\/p>\nimport { request } from '..\/..\/lib\/api';\n\nexport default function Article({ article }) {\n return (\n <div>\n <h1>{article.title}<\/h1>\n {article.content}\n <\/div>\n );\n}\n\nexport async function getStaticPaths() {\n const query = `\n query Articles {\n articles {\n slug\n }\n }\n `;\n\n const { articles } = await request(query);\n\n const paths = articles.map((article) => ({\n params: { slug: article.slug },\n }));\n\n return {\n paths,\n fallback: false,\n };\n}\n\nexport async function getStaticProps({ params }) {\n const query = `\n query Article($slug: String!) {\n article(where: { slug: $slug }) {\n title\n content\n }\n }\n `;\n\n const { article } = await request(query, { slug: params.slug });\n\n return {\n props: {\n article,\n },\n };\n}\n<\/code><\/pre>\nThis code fetches the individual article based on the slug<\/code> parameter from the Strapi API and displays it on the page.<\/p>\nStep 2: Link to the dynamic route<\/h3>\n
In the index.js<\/code> file, replace the li<\/code> element with the following code:<\/p>\nimport Link from 'next\/link';\n\n\/\/ ...\n\n<ul>\n {articles.map((article) => (\n <li key={article.id}>\n <Link href={`\/articles\/${article.slug}`}>\n <a>\n <h2>{article.title}<\/h2>\n <\/a>\n <\/Link>\n {article.content}\n <\/li>\n ))}\n<\/ul>\n<\/code><\/pre>\nThis code uses Next.js’s Link<\/code> component to create a link to the dynamic route for each article.<\/p>\nRestart the Next.js server and visit `http:\/\/localhost:3000` to see the updated list of articles. Clicking on an article should take you to the individual article page.<\/p>\n
Conclusion<\/h2>\n
Congratulations! You have successfully built a blog with Next.js as the frontend and Strapi as the backend API. You have learned how to set up the backend with Strapi, create content types, and generate a GraphQL schema. You have also learned how to set up the frontend with Next.js, fetch data from the Strapi API, and create dynamic routes.<\/p>\n
This is just the beginning of what you can accomplish with Next.js and Strapi. Feel free to explore the Next.js and Strapi documentation to discover more features and possibilities for your projects. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"
In this tutorial, we will learn how to build a blog using Next.js, a popular React framework for server-side rendering, and Strapi, an open-source headless CMS. By the end of this tutorial, you will have a fully functional blog with Next.js as the frontend and Strapi as the backend API. 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":[1671,1672,444,1668,1669,1670,1667],"yoast_head":"\nHow to Build a Blog with Next.js and Strapi - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n