-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Improve sanity.io example #18227
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
Improve sanity.io example #18227
Changes from all commits
df7bb56
30aec5a
ac23f45
5bc47db
3a15e3f
1f4f3fe
41ed492
c03ad14
21c99b4
e9a2c37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| NEXT_PUBLIC_SANITY_PROJECT_ID= | ||
| NEXT_PUBLIC_SANITY_DATASET= | ||
| SANITY_API_TOKEN= | ||
| SANITY_PREVIEW_SECRET= | ||
| SANITY_PREVIEW_SECRET= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,20 @@ | ||
| import cn from 'classnames' | ||
| import Link from 'next/link' | ||
| import { imageBuilder } from '../lib/sanity' | ||
| import { urlForImage } from '../lib/sanity' | ||
|
|
||
| export default function CoverImage({ title, url, slug }) { | ||
| const image = ( | ||
| export default function CoverImage({ title, slug, image: source }) { | ||
| const image = source ? ( | ||
| <img | ||
| width={2000} | ||
| height={1000} | ||
| alt={`Cover Image for ${title}`} | ||
| className={cn('shadow-small', { | ||
| 'hover:shadow-medium transition-shadow duration-200': slug, | ||
| })} | ||
| src={imageBuilder.image(url).height(1000).width(2000).url()} | ||
| src={urlForImage(source).height(1000).width(2000).url()} | ||
| /> | ||
| ) : ( | ||
| <div style={{ paddingTop: '50%', backgroundColor: '#ddd' }} /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't need to block this PR, but we should use next/image here so we don't need to do |
||
| ) | ||
|
|
||
| return ( | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export const sanityConfig = { | ||
| // Find your project ID and dataset in `sanity.json` in your studio project | ||
| dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production', | ||
| projectId: process.env.NEXT_PUBLIC_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 | ||
| // data always (potentially slightly slower and a bit more expensive). | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| const postFields = ` | ||
| _id, | ||
| name, | ||
| title, | ||
| date, | ||
| excerpt, | ||
| coverImage, | ||
| "slug": slug.current, | ||
| "author": author->{name, picture}, | ||
| ` | ||
|
|
||
| export const indexQuery = ` | ||
| *[_type == "post"] | order(date desc, _updatedAt desc) { | ||
| ${postFields} | ||
| }` | ||
|
|
||
| export const postQuery = ` | ||
| { | ||
| "post": *[_type == "post" && slug.current == $slug] | order(_updatedAt desc) | [0] { | ||
| content, | ||
| ${postFields} | ||
| }, | ||
| "morePosts": *[_type == "post" && slug.current != $slug] | order(date desc, _updatedAt desc) | [0...2] { | ||
| content, | ||
| ${postFields} | ||
| } | ||
| }` | ||
|
|
||
| export const postSlugsQuery = ` | ||
| *[_type == "post" && defined(slug.current)][].slug.current | ||
| ` | ||
|
|
||
| export const postBySlugQuery = ` | ||
| *[_type == "post" && slug.current == $slug][0] { | ||
| ${postFields} | ||
| } | ||
| ` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,14 @@ | ||
| import sanityClient from '@sanity/client' | ||
| import sanityImage from '@sanity/image-url' | ||
| import { | ||
| createImageUrlBuilder, | ||
| createPreviewSubscriptionHook, | ||
| } from 'next-sanity' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! 🚀 |
||
| import { sanityConfig } from './config' | ||
|
|
||
| const options = { | ||
| // Find your project ID and dataset in `sanity.json` in your studio project | ||
| dataset: 'production', | ||
| projectId: process.env.NEXT_PUBLIC_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 | ||
| // data always (potentially slightly slower and a bit more expensive). | ||
| } | ||
| export const imageBuilder = createImageUrlBuilder(sanityConfig) | ||
|
|
||
| const client = sanityClient(options) | ||
| export const urlForImage = (source) => | ||
| imageBuilder.image(source).auto('format').fit('max') | ||
|
|
||
| export const imageBuilder = sanityImage(client) | ||
|
|
||
| export const previewClient = sanityClient({ | ||
| ...options, | ||
| useCdn: false, | ||
| token: process.env.SANITY_API_TOKEN, | ||
| }) | ||
|
|
||
| export default client | ||
| export const usePreviewSubscription = createPreviewSubscriptionHook( | ||
| sanityConfig | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** | ||
| * Server-side Sanity utilities. By having these in a separate file from the | ||
| * utilities we use on the client side, we are able to tree-shake (remove) | ||
| * code that is not used on the client side. | ||
| */ | ||
| import { createClient } from 'next-sanity' | ||
| import { sanityConfig } from './config' | ||
|
|
||
| export const sanityClient = createClient(sanityConfig) | ||
|
|
||
| export const previewClient = createClient({ | ||
| ...sanityConfig, | ||
| useCdn: false, | ||
| token: process.env.SANITY_API_TOKEN, | ||
| }) | ||
|
|
||
| export const getClient = (preview) => (preview ? previewClient : sanityClient) | ||
|
|
||
| export function overlayDrafts(docs) { | ||
| const documents = docs || [] | ||
| const overlayed = documents.reduce((map, doc) => { | ||
| if (!doc._id) { | ||
| throw new Error('Ensure that `_id` is included in query projection') | ||
| } | ||
|
|
||
| const isDraft = doc._id.startsWith('drafts.') | ||
| const id = isDraft ? doc._id.slice(7) : doc._id | ||
| return isDraft || !map.has(id) ? map.set(id, doc) : map | ||
| }, new Map()) | ||
|
|
||
| return Array.from(overlayed.values()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,10 +8,9 @@ | |
| }, | ||
| "dependencies": { | ||
| "@sanity/block-content-to-react": "2.0.7", | ||
| "@sanity/client": "1.149.2", | ||
| "@sanity/image-url": "0.140.17", | ||
| "classnames": "2.2.6", | ||
| "date-fns": "2.10.0", | ||
| "next-sanity": "0.1.5", | ||
| "next": "latest", | ||
| "react": "^16.13.0", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow up could also update these libraries 😄 |
||
| "react-dom": "^16.13.0" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| import Head from 'next/head' | ||
| import Container from '../components/container' | ||
| import MoreStories from '../components/more-stories' | ||
| import HeroPost from '../components/hero-post' | ||
| import Intro from '../components/intro' | ||
| import Layout from '../components/layout' | ||
| import { getAllPostsForHome } from '../lib/api' | ||
| import Head from 'next/head' | ||
| import { CMS_NAME } from '../lib/constants' | ||
| import { indexQuery } from '../lib/queries' | ||
| import { getClient, overlayDrafts } from '../lib/sanity.server' | ||
|
|
||
| export default function Index({ allPosts, preview }) { | ||
| const heroPost = allPosts[0] | ||
|
|
@@ -36,7 +37,7 @@ export default function Index({ allPosts, preview }) { | |
| } | ||
|
|
||
| export async function getStaticProps({ preview = false }) { | ||
| const allPosts = await getAllPostsForHome(preview) | ||
| const allPosts = overlayDrafts(await getClient(preview).fetch(indexQuery)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's just the naming of this ( |
||
| return { | ||
| props: { allPosts, preview }, | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.