Discussion on setting up previews with Next.js and Sanity, including debugging steps and clarification on querying for draft data.
32 replies
Last updated: Feb 10, 2021
R
If anyone can point me to the current right way to set up previews with Next.js, I'd be very grateful! I think I muddled a few methods and tutorials, looking to try a fresh go at it.
Jan 25, 2021, 2:44 PM
O
I did follow step 5 here https://github.com/vercel/next.js/tree/canary/examples/cms-sanity and it worked perfectly.
Jan 25, 2021, 2:50 PM
R
I'll follow this a little closer and give it a shot, thanks!
Jan 27, 2021, 5:19 PM
R
Just want to revisit this: I don't know what I'm missing, but I can't seem to get draft data to preview from Sanity in my NextJS app, and I can't think of any further steps to debug it.
I see Get Preview options, I'm able to set the appropriate cookies on the NextJS api and use a different client with a token configured for previews... but no preview data. Is there a way to stream what's being queried on the Sanity side? Some other configuration I might be missing?
I see Get Preview options, I'm able to set the appropriate cookies on the NextJS api and use a different client with a token configured for previews... but no preview data. Is there a way to stream what's being queried on the Sanity side? Some other configuration I might be missing?
Feb 10, 2021, 8:39 PM
R
do you have any source code to look at?
Feb 10, 2021, 8:44 PM
R
I can provide snippets here in a moment!
Feb 10, 2021, 8:45 PM
R
My understanding of the overall paradigm in NextJS is this:
1. Point Sanity's preview mechanism to an API endpoint that requires a secret and the slug of the page to preview.
2. The API
1. Point Sanity's preview mechanism to an API endpoint that requires a secret and the slug of the page to preview.
2. The API
res.setPreviewData({})as appropriate3. in
getStaticProps, I should use a different client with a token set to request preview data4. I should receive the preview data.
Feb 10, 2021, 8:52 PM
R
do you have an api route for preview as well?
Feb 10, 2021, 8:59 PM
R
(Thanks for taking the time!)
Feb 10, 2021, 9:02 PM
H
ok, is preview ever true in getStaticProps?
Feb 10, 2021, 9:03 PM
R
It is, once you go through the API route (cookies cleared after session ends)
Feb 10, 2021, 9:04 PM
R
I have seen
previewcome back true, as well as the right client returned with a token assigned
Feb 10, 2021, 9:05 PM
R
in your api route you seems to missing
Just a notice
response.writeHead(307, {Location: `/${page.slug.current}`}); response.end();
Feb 10, 2021, 9:05 PM
H
Do you get any data in response in your api/preview route?
Feb 10, 2021, 9:06 PM
R
res.redirect(post.slug)should be a stand in for that, according to the official NextJS docs on Preview redirects
Feb 10, 2021, 9:06 PM
R
I do, it's the expected published data however
Feb 10, 2021, 9:08 PM
R
Even when preview is set. It seems that despite having a token set, my previewClient doesn't get preview data
Feb 10, 2021, 9:09 PM
R
Is the post you are viewing published?
Feb 10, 2021, 9:11 PM
R
It is published, but as unsaved changes. Does preview work only for unpublished documents?
Feb 10, 2021, 9:12 PM
R
So you are saying your console.log(data) is empty?
Feb 10, 2021, 9:12 PM
R
another thing, in your api route for preview when you're calling
getPageDatayou're not pass on preview property, So it should always be false on that function
Feb 10, 2021, 9:12 PM
R
But in the API it's just checking that a page with that slug exists. in
getStaticProps, where we render the page, I'm setting
previewto get the appropriate data
Feb 10, 2021, 9:14 PM
R
console.log(data) isn't empty, it returns the published data instead of unpublished data I'm trying to preview.
Feb 10, 2021, 9:14 PM
R
Is this in dev?
Feb 10, 2021, 9:15 PM
R
user L
it is!Feb 10, 2021, 9:15 PM
R
The reason I was asking is you haven’t setup the subscription for getting live updates like next-sanity provides https://github.com/sanity-io/next-sanity#usage
Feb 10, 2021, 9:16 PM
R
I opted to set things up per the CMS example posted above. Is Next-Sanity the preferred way of doing it atm?
Feb 10, 2021, 9:17 PM
R
Don’t know if it’s preferred, but it’s the way i did it.
Feb 10, 2021, 9:19 PM
H
have you tried remove the
[0]from your query btw?
Feb 10, 2021, 9:19 PM
R
Yeah because the way sanity works is it creates a new document
drafts.id
Feb 10, 2021, 9:21 PM
R
... that's absolutely it
Feb 10, 2021, 9:24 PM
R
Thank you both, I need to specifically query for the
draftprefixed ID...
Feb 10, 2021, 9:26 PM
R
I never put together you'd have to query it separately, which makes perfect sense.
Feb 10, 2021, 9:34 PM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.