Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMS example for Sanity #10907

Merged
merged 30 commits into from Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e7b41fd
Add start of Sanity CMS example
ijjk Mar 9, 2020
6bcf3d9
Merge remote-tracking branch 'upstream/canary' into example/sanity-cms
ijjk Mar 9, 2020
3b8f0ee
Update cover-image
ijjk Mar 9, 2020
c3ea04e
Clean up example
ijjk Mar 9, 2020
edc38e4
Merge remote-tracking branch 'upstream/canary' into example/sanity-cms
ijjk Mar 9, 2020
a7dfdd5
Apply suggestions from code review
ijjk Mar 10, 2020
95684be
Make changes from review
ijjk Mar 10, 2020
0270e56
Remove extra style
ijjk Mar 10, 2020
0ed7587
Apply suggestions from code review
ijjk Mar 12, 2020
5cef061
Merge remote-tracking branch 'upstream/canary' into example/sanity-cms
ijjk Mar 12, 2020
5ca2042
Make tweaks for preview mode and normalize env naming
ijjk Mar 12, 2020
c7bc255
Merge branch 'example/sanity-cms' of github.com:ijjk/next.js into exa…
ijjk Mar 12, 2020
783108e
Update viewing preview step
ijjk Mar 12, 2020
3ce1e90
Delete author.jpg
chibicode Mar 13, 2020
d8ecc0d
Delete image.jpg
chibicode Mar 13, 2020
dfd7ef6
Merge branch 'canary' into example/sanity-cms
chibicode Mar 13, 2020
e32eaac
Merge branch 'canary' into example/sanity-cms
chibicode Mar 16, 2020
b98f7e8
Update README
chibicode Mar 16, 2020
570ab8c
Fix environment variable names
chibicode Mar 16, 2020
80c487b
Project ID is needed by lambda
chibicode Mar 16, 2020
a6504ba
Improved steps
Mar 18, 2020
03c7a5c
Fixed issue with the project URL example
Mar 18, 2020
6c432ba
Clarify line
Mar 18, 2020
d44cf57
Get the preview post correctly
chibicode Mar 18, 2020
6968ea3
preview = false
chibicode Mar 18, 2020
6a7869c
Get unique posts
chibicode Mar 18, 2020
9f9968e
Show preview mode on index page
chibicode Mar 18, 2020
3068ab3
typo fix and use next latest
Mar 18, 2020
1ec4f95
Merge branch 'canary' into example/sanity-cms
Mar 18, 2020
0e028b3
Added related examples section
Mar 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/cms-sanity/.env.example
@@ -1 +1,2 @@
NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET=
NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET=
SANITY_PROJECT_ID=
52 changes: 26 additions & 26 deletions examples/cms-sanity/README.md
Expand Up @@ -13,9 +13,9 @@ This example showcases Next.js's [Static Generation](/docs/basic-features/pages.
Execute [`create-next-app`](https://github.com/zeit/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:

```bash
npm init next-app --example cms-Sanity cms-Sanity-app
npm init next-app --example cms-sanity cms-sanity-app
# or
yarn create next-app --example cms-Sanity cms-Sanity-app
yarn create next-app --example cms-sanity cms-sanity-app
```

### Download manually
Expand Down Expand Up @@ -43,15 +43,35 @@ This will be where we manage our data.

When going through the init phase make sure to select **Yes** to the **Use the default dataset configuration** step and select **Clean project with no predefined schemas** for the **Select project template** step.

After initializing get the `projectId` and `dataset` values from the `sanity.json` file that was generated and add them to `lib/sanity.js` in the example.
After initializing, get the `projectId` value from the `sanity.json` file that was generated for the next step.

### Step 3. Copy the schema file
### Step 3. Set up environment variables

First, copy the `.env.example` file in this directory to `.env` (which will be ignored by Git):

```bash
cp .env.example .env
```

Then set each variable on `.env`:

- `NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET` can be any random string (but avoid spaces), like `MY_SECRET` - this is used for [the Preview Mode](/docs/advanced-features/preview-mode.md).
- `SANITY_PROJECT_ID` the `projectId` from the previous step

Your `.env` file should look like this:

```bash
NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET=...
SANITY_PROJECT_ID=...
```

### Step 4. Copy the schema file

After initializing your Sanity studio project there should be a `schemas` folder.

To get up and running we want to copy the schema file from [`./schemas/schema.js`](./schemas/schema.js) to the studio project to use for creating/editing content.
ijjk marked this conversation as resolved.
Show resolved Hide resolved

### Step 4. Populate Content
### Step 5. Populate Content

To add some content go to your Sanity studio project where we added the schema file and run `sanity start`.
ijjk marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -71,26 +91,6 @@ Next, select **Post** and create a new record.

**Important:** For each post record, you need to click **Publish** after saving. If not, the post will be in the draft state.
ijjk marked this conversation as resolved.
Show resolved Hide resolved

### Step 5. Set up environment variables

First, create a random preview secret.

Next, copy the `.env.example` file in this directory to `.env` (which will be ignored by Git):

```bash
cp .env.example .env
```

Then set each variable on `.env`:

- `NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET` can be any random string (but avoid spaces), like `MY_SECRET` - this is used for [the Preview Mode](/docs/advanced-features/preview-mode.md).

Your `.env` file should look like this:

```bash
NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET=...
```

### Step 6. Run Next.js in development mode

```bash
Expand All @@ -110,7 +110,7 @@ Your blog should be up and running on [http://localhost:3000](http://localhost:3
On Sanity, go to one of the posts you've created and:

- **Update the title**. For example, you can add `[Draft]` in front of the title.
- Click **Save**, but **DO NOT** click **Publish**. By doing this, the post will be in the draft state.
- As you edit the document it will be saved as a draft, but **DO NOT** click **Publish**. By doing this, the post will be in the draft state.

Now, if you go to the post page on localhost, you won't see the updated title. However, if you use the **Preview Mode**, you'll be able to see the change ([Documentation](/docs/advanced-features/preview-mode.md)).

Expand Down
10 changes: 9 additions & 1 deletion examples/cms-sanity/components/cover-image.js
@@ -1,14 +1,22 @@
import cn from 'classnames'
import Link from 'next/link'
import { imageBuilder } from '../lib/api'

export default function CoverImage({ title, url, slug }) {
const image = (
<img
src={url}
width={2000}
height={1000}
alt={`Cover Image for ${title}`}
className={cn('shadow-small', {
'hover:shadow-medium transition-shadow duration-200': slug,
})}
ijjk marked this conversation as resolved.
Show resolved Hide resolved
style={{ maxWidth: '100%', maxHeight: '100%' }}
ijjk marked this conversation as resolved.
Show resolved Hide resolved
src={imageBuilder
.image(url)
.height(1000)
.width(2000)
.url()}
/>
)

Expand Down
3 changes: 3 additions & 0 deletions examples/cms-sanity/lib/api.js
@@ -1,4 +1,5 @@
import client, { previewClient } from './sanity'
import sanityImage from '@sanity/image-url'

const postFields = `
name,
Expand All @@ -12,6 +13,8 @@ const postFields = `

const getClient = preview => (preview ? previewClient : client)

export const imageBuilder = sanityImage(client)

export async function getPreviewPostBySlug(slug) {
const data = await getClient(true).fetch(
`*[_type == "post" && slug.current == $slug] | order(date desc){
Expand Down
2 changes: 1 addition & 1 deletion examples/cms-sanity/lib/sanity.js
Expand Up @@ -2,8 +2,8 @@ import sanityClient from '@sanity/client'

const options = {
// Find your project ID and dataset in `sanity.json` in your studio project
projectId: 'lsd8pmjw',
dataset: 'production',
projectId: process.env.SANITY_PROJECT_ID,
useCdn: process.env.NODE_ENV === 'production',
// useCdn == true gives fast, cheap responses using a globally distributed cache.
// Set this to false if your application require the freshest possible
Expand Down
4 changes: 3 additions & 1 deletion examples/cms-sanity/next.config.js
@@ -1,5 +1,7 @@
require('dotenv').config()

module.exports = {
env: {},
env: {
SANITY_PROJECT_ID: process.env.SANITY_PROJECT_ID,
},
}
7 changes: 6 additions & 1 deletion examples/cms-sanity/now.json
@@ -1,5 +1,10 @@
{
"env": {
"NEXT_EXAMPLE_CMS_DATOCMS_PREVIEW_SECRET": "@next_example_cms_datocms_preview_secret"
"NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET": "@next_example_cms_sanity_preview_secret"
},
"build": {
"env": {
"SANITY_PROJECT_ID": "@next_example_cms_sanity_project_id"
}
}
}
2 changes: 1 addition & 1 deletion examples/cms-sanity/pages/api/preview.js
Expand Up @@ -4,7 +4,7 @@ export default async (req, res) => {
// Check the secret and next parameters
// This secret should only be know to this API route and the CMS
if (
req.query.secret !== process.env.NEXT_EXAMPLE_CMS_DATOCMS_PREVIEW_SECRET ||
req.query.secret !== process.env.NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET ||
!req.query.slug
) {
return res.status(401).json({ message: 'Invalid token' })
Expand Down