rsnext/examples/cms-strapi
Freddy Montes 15cc88909c
chore(examples): Add dotCMS example (#38214)
<!--
Thanks for opening a PR! Your contribution is much appreciated.
In order to make sure your PR is handled as smoothly as possible we
request that you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

This adds a new example under cms-dotcms/. Is a general-purpose example
that should allow developers to undestand how to use next.js with dotCMS
apis.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [x] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)

Co-authored-by: Daniel Esteves <estevesd8@gmail.com>
Co-authored-by: Will Ezell <will@dotcms.com>
Co-authored-by: Arcadio Quintero A <oidacra@gmail.com>
Co-authored-by: Rafael <rjvelazco21@gmail.com>
2022-10-01 15:26:13 +02:00
..
components docs: update CMS examples to use the new tw grid gaps (#34917) 2022-03-01 07:47:28 -06:00
lib bugfix: remark/webpack conflict (#29180) 2021-09-17 15:52:55 +00:00
pages [Docs] Update examples to favour functional _document (#39871) 2022-08-23 18:52:31 +00:00
public/favicon ButterCMS Example (#13908) 2020-06-10 17:13:10 +00:00
styles CMS Strapi Example (#12701) 2020-06-01 18:49:43 -05:00
.env.local.example Add new import flow to CMS examples (#14053) 2020-06-11 23:44:23 +00:00
.gitignore Update default gitignore templates (#39051) 2022-07-26 20:08:40 -05:00
jsconfig.json CMS Strapi Example (#12701) 2020-06-01 18:49:43 -05:00
next.config.js [Docs] Add config types to all examples (#40083) 2022-08-31 16:41:22 -05:00
package.json Update all CMS examples dependencies. (#33580) 2022-02-04 00:07:35 +00:00
postcss.config.js Update dependencies of all CMS examples. (#27001) 2021-07-09 14:43:56 +02:00
README.md chore(examples): Add dotCMS example (#38214) 2022-10-01 15:26:13 +02:00
tailwind.config.js [Docs] Add config types to all examples (#40083) 2022-08-31 16:41:22 -05:00

A statically generated blog example using Next.js and Strapi

This example showcases Next.js's Static Generation feature using Strapi as the data source.

Demo

https://next-blog-strapi.vercel.app/

Deploy your own

Once you have access to the environment variables you'll need, deploy the example using Vercel:

Deploy with Vercel

How to use

Execute create-next-app with npm, Yarn, or pnpm to bootstrap the example:

npx create-next-app --example cms-strapi cms-strapi-app
yarn create next-app --example cms-strapi cms-strapi-app
pnpm create next-app --example cms-strapi cms-strapi-app

Configuration

Step 1. Set up Strapi locally

Use the provided Strapi template Next example to run a pre-configured Strapi project locally. See the Strapi template docs for more information

npx create-strapi-app@3 my-project --template next-example --quickstart
# or: yarn create strapi-app@3 my-project --template next-example --quickstart
npm run develop # or: yarn develop

This will open http://localhost:1337/ and prompt you to create an admin user.

After you sign in there should already be data for Authors and Posts. If you want to add more entries, just do the following:

Select Author and click Add New Author.

  • Use dummy data for the name.
  • For the image, you can download one from Unsplash.

Next, select Posts and click Add New Post.

  • Use dummy data for the text.
  • You can write markdown for the content field.
  • For the images, you can download ones from Unsplash.
  • Pick the Author you created earlier.
  • Set the status field to be published.

Step 2. Set up environment variables

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).

Step 3. Run Next.js in development mode

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!

The best place to debug is inside the fetchAPI function in lib/api.js. If you need help, you can post on GitHub discussions.

Step 4. Try preview mode

If you go to the /posts/draft page on localhost, you won't see this post because its 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
  • <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 add more preview pages, create a post and set the status as draft.

Step 5. Deploy Strapi

To deploy to production, you must first deploy your Strapi app. The Strapi app for our demo at https://next-blog-strapi.vercel.app/ is deployed to Heroku (heres the documentation) and uses Cloudinary for image hosting (see this file).

Step 6. Deploy on Vercel

You can deploy this app to the cloud with Vercel (Documentation).

Deploy Your Local Project

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.

Deploy from Our Template

Alternatively, you can deploy using our template by clicking on the Deploy button below.

Deploy with Vercel