Trouble with `next-sanity` preview hook not displaying draft data in blog post.
9 replies
Last updated: Apr 8, 2021
R
Maybe I'm missing something in my configuration of the
I'm trying to test it by editing the title of a blog post without publishing, but am still just getting the published version.
next-sanitypreview hook, or am misunderstanding how it works, but once set up using
usePreviewSubscriptionit should automatically display data in drafts right? Pulling instructions from https://github.com/sanity-io/next-sanity#example-minimal-blog-post-template
I'm trying to test it by editing the title of a blog post without publishing, but am still just getting the published version.
Apr 5, 2021, 4:55 PM
R
my client config right now:
const config = { projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || '', dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production', apiVersion: '2021-03-25', token: process.env.NEXT_PUBLIC_SANITY_TOKEN, useCdn: false, };
Apr 5, 2021, 4:56 PM
R
and in my `/blog/[slug].js`:
export default function BlogPost({ post, ...props }) { const router = useRouter(); if (!router.isFallback && !post?.slug) { return <FourOhFourPage />; } const { data: newPost } = usePreviewSubscription(postQuery, { params: { slug: <http://props.post?.slug|props.post?.slug> }, initialData: post, enabled: true, }); return <Post post={newPost} {...props} />; }
Apr 5, 2021, 4:57 PM
R
My groq query gets
[0]for the given post slug, but when I remove that in Vision, I can see both the published version, and the
id: 'draft.id-here'version with my updated title (just added
Testto the end of the title lol). How do I get the preview client to use that draft version?
*[_type == 'post' && slug.current == $slug][0] { ..., 'slug': slug.current, // other projects here, but not important for this i think }
Apr 5, 2021, 5:11 PM
R
Hmm, looking into it more, it seems to do something when I remove some of the other projections that were references (i.e.
authors[]->{ name }), but I'm now just getting
nullreturned from my
newPost
Apr 5, 2021, 7:07 PM
R
In your code you've got props.post?.slug should that not be post?.slug?
Apr 5, 2021, 7:57 PM
R
Ahh good catch! Seems like it was that and maybe some whitespace that was leading to a syntax error
Apr 5, 2021, 8:03 PM
R
Specifically, when expanding my image asset reference, it didn't like
asset ->, but
asset->(no space) fixed it. š¤¦āāļø
Apr 5, 2021, 8:04 PM
R
Yes I saw another post saying the new API version doesn't allow for bad syntax any longer.
Apr 5, 2021, 9:23 PM
W
Specifically, when expanding my image asset reference, it didnāt likeĀExactly the same! š Thanks, would never have figured it out by myself! šasset ->, butĀasset->Ā (no space) fixed it.Ā
Apr 8, 2021, 8:35 PM
Sanityā build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.