strapi-frontend-nextjs
This example showcases Next.js’s Static Generation feature using Strapi as the data source.
https://next-blog-strapi.now.sh/
Once you have access to the environment variables you’ll need, deploy the example using Vercel:
Execute create-next-app
with npm or Yarn to bootstrap the example:
npx create-next-app --example cms-strapi cms-strapi-app
# or
yarn create next-app --example cms-strapi cms-strapi-app
Follow the instructions on this page to create a Strapi project locally.
npx create-strapi-app my-project --quickstart
npm run develop # or: yarn develop
This will open http://localhost:1337/ and prompt you to create an admin user.
Inside the Strapi directory, stop the server, install GraphQL, and restart the server:
# If using Yarn: yarn strapi install graphql
npm run strapi install graphql
npm run develop # or: yarn develop
Author
collectionFrom Content-Types Builder, create a new collection type.
Author
.Next, add these fields (you don’t have to modify the settings):
name
(Short text)picture
(Single media)Then click Save.
Post
collectionFrom Content-Types Builder, create a new collection type.
Post
.Next, add these fields (you don’t have to modify the settings unless specified):
title
(Short text)content
(Multiple-paragraph Text)excerpt
(Long text)coverImage
(Single media)date
(type should be date)slug
(attached field should be title)author
(Post has one Author)status
(the values should be published and draft)From Roles & Permissions, edit the Public role.
Then select: count
, find
, and findone
permissions for both Author and Post. Click Save.
Select Author and click Add New Author.
Next, select Posts and click Add New Post.
While the Strapi server is running, open a new terminal and cd
into the Next.js app directory you created earlier.
cd cms-strapi-app
Copy the .env.local.example
file in this directory to .env.local
(which will be ignored by Git):
cp .env.local.example .env.local
Then set each variable on .env.local
:
STRAPI_PREVIEW_SECRET
can be any random string (but avoid spaces), like MY_SECRET
- this is used for Preview Mode.NEXT_PUBLIC_STRAPI_API_URL
should be set as http://localhost:1337
(no trailing slash).Make sure that the local Strapi server is still running at http://localhost:1337. Inside the Next.js app directory, run:
npm install
npm run dev
# or
yarn install
yarn dev
Your blog should be up and running on http://localhost:3000! You should see the two posts you’ve created. If it doesn’t work, make sure that:
status
of each post to be published
in Step 6.The best place to debug is inside the fetchAPI
function in lib/api.js
. If you still need help, you can post on GitHub discussions.
To try preview mode, create another post like before, but:
Draft Post Test
draft
.Now, if you go to the post page on localhost, you won’t see this post because it’s not published. However, if you use the Preview Mode, you’ll be able to see the change (Documentation).
To enable the Preview Mode, go to this URL:
http://localhost:3000/api/preview?secret=<secret>&slug=draft-post-test
<secret>
should be the string you entered for STRAPI_PREVIEW_SECRET
.<slug>
should be the post’s slug
attribute.You should now be able to see the draft post. To exit the preview mode, you can click Click here to exit preview mode at the top.
To deploy to production, you must first deploy your Strapi app. The Strapi app for our demo at https://next-blog-strapi.now.sh/ is deployed to Heroku (here’s the documentation) and uses Cloudinary for image hosting (see this file).
You can deploy this app to the cloud with Vercel (Documentation).
To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and import to Vercel.
Important: When you import your project on Vercel, make sure to click on Environment Variables and set them to match your .env.local
file.
Alternatively, you can deploy using our template by clicking on the Deploy button below.