Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
183 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.