Query an Entry from Strapi with Next.js 13 Server Components

InstructorDaniel Phiri

Share this video with your friends

Send Tweet

We'll learn how to connect a Next.js 13 application to Strapi's API by setting environment variables and defining an async function that uses fetch to get data from your Strapi backend.

You'll notice that on our home page, we are accessing sensitive environment variables within the getPosts function that will make the query for our posts that live in Stapi. This would normally be ill-advised as you don't want to leak sensitive information to the client side. However, since we are using Next.js 13 app directory components default to being Server Components which means we can safely access this sensitive information without worrying about it being exposed to the client.

Once we get data from Strapi we can map over it and display the titles as a list on our home page.

~ a year ago

Sorry, but this code is not working.
Console show's me an error: "The above error occurred in the <NotFoundErrorBoundary> component" When I wrapped fetching in [ try, catch ] construction - I do not get any data :( And, eahh, link [ http://localhost:1337/api/posts ] show my posts. Help to solwe the problem, please. [ Next.js version "13.3.1", "react": "18.2.0" ]

~ a year ago

Sorry, I'm fixed my problem. I build my file structure like in your repository, rebuild DB and everything starts working. Have a gook day 🤗