This example showcases Next.js's Static Generation feature using Cosmic as the data source. Now with TypeScript!
Includes Cosmic merge request preview giving you the ability to do bulk updates between Buckets, see bulk update previews, get approvals from other team members, and more.
https://cosmic-next-blog.vercel.app/
Once you have access to the environment variables you'll need, deploy the example using Vercel:
Cosmic Merge Requests is a feature in the Cosmic headless CMS that enables you to do bulk edits, preview, and approval workflows for content updates. Learn more about merge requests in the Cosmic blog announcement.
Uses SWR for instant previews on content updates in preview mode.
Clone the repo to install and bootstrap the example:
git clone https://github.com/cosmicjs/next-merge
First, create an account on Cosmic.
After creating an account, install the Next.js Merge Blog app from the Cosmic App Marketplace.
Go to the Settings menu at the sidebar and click Basic Settings.
Next, 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
:
COSMIC_BUCKET_SLUG
should be the Bucket slug key under Basic Settings.COSMIC_READ_KEY
should be the Read Key under API Access.COSMIC_PREVIEW_SECRET
can be any random string (but avoid spaces) - this is used for Preview Mode.
Your .env.local
file should look like this:
COSMIC_BUCKET_SLUG=...
COSMIC_READ_KEY=...
COSMIC_PREVIEW_SECRET=...
npm install
npm run dev
# or
yarn install
yarn dev
Your blog should be up and running on http://localhost:3000! If it doesn't work, post on GitHub discussions.
To add the ability to preview content from a merge request, follow these steps:
- To clone a Bucket into your Project, go to Your Bucket > Basic Settings, scroll down, and click "Clone Bucket".
- After cloning your Bucket, create a new merge request in your Bucket project clicking the "Add Merge Request" button on the Dasboard
- Select the Source Bucket (Staging) and your Target Bucket (Production). Click "Review Changes".
- There shouldn't be any changes to preview yet. Now add your merge Preview Link exactly the following and click "Save":
http://localhost:3000?merge_id=[merge_id]
The [merge_id]
shortcode will be converted into the merge request id which will be used to get the Objects in the merge request.
- Add / Edit content in your Staging Bucket. Any edits to Objects, Object Types, and Media will be added to the merge request (deleting content does not affect it).
- After adding and editing content in staging, go back to your merge request and notice the Changes to Objects, Object Types, and / or Media.
- Click the "Preview" button to see content from the merge request now visible in your locally running app.
- Make more changes in your Staging Bucket and see real time updates in the app thanks to SWR :)
To exit preview mode, you can click on You are previewing merge content. Click here to remove at the top.
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 this template by clicking on the Deploy button below.