Skip to content

Commit 0c7217b

Browse files
authored
docs(examples): use vercel integration in cms-sanity (vercel#39323)
## Feature - [x] Documentation added ## Documentation / Examples - [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples) This PR updates the cms-sanity example to: - Deploy to Vercel with the [Sanity Vercel Integration](https://www.sanity.io/docs/vercel-integration) by default. - You can still opt-out of using the Sanity Vercel Integration, clicking `You can also set up manually` gives you all the information you need. - The blog itself is updated so it's much more resilient to missing data, and is setup to set `revalidate: 60` until you have a On-demand Revalidation webhook setup. - Preview Mode is now enabled on the frontpage as well. - The Sanity client setup, and webhook validation, are updated to follow our current best practices.
1 parent 911ba23 commit 0c7217b

35 files changed

+663
-246
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
NEXT_PUBLIC_SANITY_PROJECT_ID=
22
NEXT_PUBLIC_SANITY_DATASET=
3-
SANITY_API_TOKEN=
4-
SANITY_PREVIEW_SECRET=
5-
SANITY_STUDIO_REVALIDATE_SECRET=
3+
SANITY_API_READ_TOKEN=

examples/cms-sanity/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# dependencies
44
/node_modules
5+
/studio/node_modules
56
/.pnp
67
.pnp.js
78

@@ -14,6 +15,7 @@
1415

1516
# production
1617
/build
18+
/studio/dist
1719

1820
# misc
1921
.DS_Store
@@ -34,3 +36,7 @@ yarn-error.log*
3436
# typescript
3537
*.tsbuildinfo
3638
next-env.d.ts
39+
40+
# Env files created by scripts for working locally
41+
.env
42+
studio/.env.development

examples/cms-sanity/README.md

Lines changed: 278 additions & 104 deletions
Large diffs are not rendered by default.

examples/cms-sanity/components/avatar.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import Image from 'next/image'
1+
import Image from 'next/future/image'
22
import { urlForImage } from '../lib/sanity'
33

44
export default function Avatar({ name, picture }) {
55
return (
66
<div className="flex items-center">
7-
<div className="w-12 h-12 relative mr-4">
7+
<div className="relative w-12 h-12 mr-4">
88
<Image
9-
src={urlForImage(picture).height(96).width(96).fit('crop').url()}
10-
layout="fill"
9+
src={
10+
picture?.asset?._ref
11+
? urlForImage(picture).height(96).width(96).fit('crop').url()
12+
: 'https://source.unsplash.com/96x96/?face'
13+
}
1114
className="rounded-full"
15+
height={96}
16+
width={96}
1217
alt={name}
1318
/>
1419
</div>

examples/cms-sanity/components/cover-image.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import cn from 'classnames'
2-
import Image from 'next/image'
2+
import Image from 'next/future/image'
33
import Link from 'next/link'
44
import { urlForImage } from '../lib/sanity'
55

6-
export default function CoverImage({ title, slug, image: source }) {
7-
const image = source ? (
6+
export default function CoverImage({ title, slug, image: source, priority }) {
7+
const image = source?.asset?._ref ? (
88
<div
99
className={cn('shadow-small', {
1010
'hover:shadow-medium transition-shadow duration-200': slug,
1111
})}
1212
>
1313
<Image
14+
className="w-full h-auto"
1415
layout="responsive"
1516
width={2000}
1617
height={1000}
1718
alt={`Cover Image for ${title}`}
1819
src={urlForImage(source).height(1000).width(2000).url()}
20+
sizes="100vw"
21+
priority={priority}
1922
/>
2023
</div>
2124
) : (
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { parseISO, format } from 'date-fns'
22

33
export default function Date({ dateString }) {
4+
if (!dateString) return null
5+
46
const date = parseISO(dateString)
57
return <time dateTime={dateString}>{format(date, 'LLLL d, yyyy')}</time>
68
}

examples/cms-sanity/components/hero-post.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ export default function HeroPost({
1414
return (
1515
<section>
1616
<div className="mb-8 md:mb-16">
17-
<CoverImage slug={slug} title={title} image={coverImage} />
17+
<CoverImage slug={slug} title={title} image={coverImage} priority />
1818
</div>
19-
<div className="md:grid md:grid-cols-2 md:gap-x-16 lg:gap-x-8 mb-20 md:mb-28">
19+
<div className="mb-20 md:grid md:grid-cols-2 md:gap-x-16 lg:gap-x-8 md:mb-28">
2020
<div>
21-
<h3 className="mb-4 text-4xl lg:text-6xl leading-tight">
21+
<h3 className="mb-4 text-4xl leading-tight lg:text-6xl">
2222
<Link href={`/posts/${slug}`}>
2323
<a className="hover:underline">{title}</a>
2424
</Link>
2525
</h3>
26-
<div className="mb-4 md:mb-0 text-lg">
26+
<div className="mb-4 text-lg md:mb-0">
2727
<Date dateString={date} />
2828
</div>
2929
</div>
3030
<div>
31-
<p className="text-lg leading-relaxed mb-4">{excerpt}</p>
32-
<Avatar name={author.name} picture={author.picture} />
31+
<p className="mb-4 text-lg leading-relaxed">{excerpt}</p>
32+
{author && <Avatar name={author.name} picture={author.picture} />}
3333
</div>
3434
</div>
3535
</section>

examples/cms-sanity/components/post-header.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ export default function PostHeader({ title, coverImage, date, author }) {
88
<>
99
<PostTitle>{title}</PostTitle>
1010
<div className="hidden md:block md:mb-12">
11-
<Avatar name={author.name} picture={author.picture} />
11+
{author && <Avatar name={author.name} picture={author.picture} />}
1212
</div>
1313
<div className="mb-8 md:mb-16 sm:mx-0">
14-
<CoverImage title={title} image={coverImage} />
14+
<CoverImage title={title} image={coverImage} priority />
1515
</div>
1616
<div className="max-w-2xl mx-auto">
17-
<div className="block md:hidden mb-6">
18-
<Avatar name={author.name} picture={author.picture} />
17+
<div className="block mb-6 md:hidden">
18+
{author && <Avatar name={author.name} picture={author.picture} />}
1919
</div>
2020
<div className="mb-6 text-lg">
2121
<Date dateString={date} />

examples/cms-sanity/components/post-preview.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ export default function PostPreview({
1616
<div className="mb-5">
1717
<CoverImage slug={slug} title={title} image={coverImage} />
1818
</div>
19-
<h3 className="text-3xl mb-3 leading-snug">
19+
<h3 className="mb-3 text-3xl leading-snug">
2020
<Link href={`/posts/${slug}`}>
2121
<a className="hover:underline">{title}</a>
2222
</Link>
2323
</h3>
24-
<div className="text-lg mb-4">
24+
<div className="mb-4 text-lg">
2525
<Date dateString={date} />
2626
</div>
27-
<p className="text-lg leading-relaxed mb-4">{excerpt}</p>
28-
<Avatar name={author.name} picture={author.picture} />
27+
<p className="mb-4 text-lg leading-relaxed">{excerpt}</p>
28+
{author && <Avatar name={author.name} picture={author.picture} />}
2929
</div>
3030
)
3131
}

examples/cms-sanity/lib/config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ export const sanityConfig = {
22
// Find your project ID and dataset in `sanity.json` in your studio project
33
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
44
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
5-
useCdn: process.env.NODE_ENV !== 'production',
5+
useCdn:
6+
typeof document !== 'undefined' && process.env.NODE_ENV === 'production',
67
// useCdn == true gives fast, cheap responses using a globally distributed cache.
78
// When in production the Sanity API is only queried on build-time, and on-demand when responding to webhooks.
89
// Thus the data need to be fresh and API response time is less important.
910
// When in development/working locally, it's more important to keep costs down as hot reloading can incurr a lot of API calls
1011
// And every page load calls getStaticProps.
1112
// To get the lowest latency, lowest cost, and latest data, use the Instant Preview mode
12-
apiVersion: '2021-03-25',
13+
apiVersion: '2022-03-13',
1314
// see https://www.sanity.io/docs/api-versioning for how versioning works
1415
}

0 commit comments

Comments
 (0)