This example showcases Next.js's Static Generation feature using TakeShape as the data source.
Execute create-next-app
with npm or Yarn to bootstrap the example:
npx create-next-app --example cms-takeshape cms-takeshape-app
# or
yarn create next-app --example cms-takeshape cms-takeshape-app
Download the example:
curl https://codeload.github.com/vercel/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/cms-takeshape
cd cms-takeshape
First, create an account on TakeShape.
After creating an account, create a new project from the dashboard. You can select a Blank Project.
From the project settings page, create a new content type.
- The title should be
Author
.
Next, drag these widgets:
- Single Line widget: Set the title as Name.
- Asset widget: Set the title as Picture.
When you’re done, click "Create Content Type".
Click Add Content Type again.
- The title should be
Post
.
Next, add these fields (you don't have to modify the settings unless specified):
- Single Line widget: Set the title as Title.
- Markdown widget: Set the title as Content.
- Single Line widget: Set the title as Excerpt.
- Asset widget: Set the title as Cover Image.
- Date widget: Set the title as Date.
- Single Line widget: Set the title as Slug.
- Relationship widget: Set the title as Author, then set Relationship Type to
Single
and check theAuthor
checkbox under Allowed Content Types.
When you’re done, click "Create Content Type".
Select Author and create a new record.
- You just need 1 Author record.
- Use dummy data for the text.
- For the image, you can download one from Unsplash.
When you’re done, make sure to click Enabled under Workflow Status.
Next, select Post and create a new record.
- We recommend creating at least 2 Post records.
- 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.
When you’re done, make sure to click Enabled under Workflow Status.
From the dropdown next to the project name, click API Keys.
Create a new API Key with the Read permission.
Next, copy the .env.example
file in this directory to .env
(which will be ignored by Git):
cp .env.example .env
Then set each variable on .env
:
NEXT_EXAMPLE_CMS_TAKESHAPE_API_KEY
should be the API token you just copied.NEXT_EXAMPLE_CMS_TAKESHAPE_PROJECT_ID
should be the project ID, which is a substring in the project page URL:https://app.takeshape.io/projects/<project-id>/...
NEXT_EXAMPLE_CMS_TAKESHAPE_PREVIEW_SECRET
can be any random string (but avoid spaces), likeMY_SECRET
- this is used for the Preview Mode.
Your .env
file should look like this:
NEXT_EXAMPLE_CMS_TAKESHAPE_PROJECT_ID=...
NEXT_EXAMPLE_CMS_TAKESHAPE_API_KEY=...
NEXT_EXAMPLE_CMS_TAKESHAPE_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.
On TakeShape, create a new post like before. But DO NOT click Enabled under Workflow Status.
Now, if you go to http://localhost:3000/posts/<slug>
(replace <slug>
), you won’t see the post. 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=<slug>
<secret>
should be the string you entered forNEXT_EXAMPLE_CMS_TAKESHAPE_PREVIEW_SECRET
.<slug>
should be the post'sslug
attribute (you can check on TakeShape).
You should now be able to see this post. To exit the preview mode, you can click Click here to exit preview mode at the top.
You can deploy this app to the cloud with Vercel (Documentation).
To deploy on Vercel, you need to set the environment variables with Now Secrets using Vercel CLI (Documentation).
Install Vercel CLI, log in to your account from the CLI, and run the following commands to add the environment variables. Replace <NEXT_EXAMPLE_CMS_TAKESHAPE_API_KEY>
and <NEXT_EXAMPLE_CMS_TAKESHAPE_PREVIEW_SECRET>
with the corresponding strings in .env
.
now secrets add next_example_cms_takeshape_api_key <NEXT_EXAMPLE_CMS_TAKESHAPE_API_KEY>
now secrets add next_example_cms_takeshape_project_id <NEXT_EXAMPLE_CMS_TAKESHAPE_PROJECT_ID>
now secrets add next_example_cms_takeshape_preview_secret <NEXT_EXAMPLE_CMS_TAKESHAPE_PREVIEW_SECRET>
Then push the project to GitHub/GitLab/Bitbucket and import to Vercel to deploy.