Content appears in preview then disappears after page loads

23 replies
Last updated: Jan 16, 2022
A completely unrelated question to my last one, but can anyone understand why my content is appearing in the preview and then promptly disappearing when the page finishes loading? Video here: https://www.dropbox.com/s/js2vxz0fpfnixjz/Screen%20Recording%202022-01-13%20at%2010.05.11.mov?dl=0
AI Update

Based on the video you shared, this looks like a classic issue where content loads initially and then disappears after the page finishes loading. This was a common problem with older Next.js preview implementations from around 2022.

Important note: The preview approach you're likely using (usePreviewSubscription from older versions of next-sanity) has been replaced with modern alternatives. The current recommended approaches for previewing draft content in Next.js are:

  1. Visual Editing with Draft Mode - The modern approach that provides live-as-you-type updates with clickable overlays
  2. Live Content API - For real-time content delivery without complex preview setup

However, if you're still working with the older usePreviewSubscription pattern, the most common cause of content disappearing is:

Parameter name mismatches - Your GROQ query expects a variable name (like $slug) but your subscription hook is passing a different parameter name. When the subscription takes over from the initial getStaticProps data, it runs the query with incorrect parameters and returns null.

For example, if your query is:

*[_type == "page" && slug.current == $slug][0]

Your subscription params must use slug:

const { data: page } = usePreviewSubscription(pageQuery, {
  params: { slug: data?.page?.slug }, // Must match $slug in query
  initialData: data.page,
  enabled: preview,
});

Other common causes from that era:

  • Initial data structure mismatch - Passing the wrong shape to initialData
  • Missing or incorrect authentication token for fetching draft content
  • Browser blocking third-party cookies if Studio and frontend are on different domains

My recommendation: If you're starting fresh or can refactor, migrate to the modern Draft Mode with Visual Editing approach. It's more reliable, better maintained, and provides a superior editing experience with live updates as you type. The old usePreviewSubscription pattern had various quirks that have been solved in the current implementation.

Show original thread
23 replies
I assume it's when the preview kicks in, but I'm not sure why 😞
The site is using Next preview mode — I followed this guide but I'm happy to share my code
https://www.sanity.io/guides/nextjs-live-preview#e0a6b3b6629c
I can't say by the video but the fact it prints:
page: null
might have something to do with it? I've not tried a complex page in preview mode yet, so hopefully it's not riddled with issues!
Yeah it's like the page data is loaded, then unloaded when the preview appears? You can see the the log shows the data load, but then shows null when the loading is complete
I guess it's this that's unloading the data:
  const {
    data: { page },
  } = usePreviewSubscription(pageQuery, {
    params: { slug },
    initialData: data,
    enabled: preview && slug,
  });
I can see that
{ page }
does exist within
data
though...
Just posting here as I test things out... changing
enabled
above to
false
allows the page to load, but then of course the live preview doesn't work. So I've at least found the cause of the issue, but not yet sure what will fix it
Totally appreciate it's the weekend, but if anyone has any ideas here ... I'm a bit up shit creek with it 😐
It looks like you're using a subquery i.e
{
  "globalMetaData": *[_type == "config"][0],
  "menuItem": *[_type == "config"][0],
  /* etc */
}
If so, AFAIK it doesn't work with
createPreviewSubscriptionHook
. I ran into this same issue last year https://github.com/sanity-io/next-sanity/issues/4
I ended up fetching the global data separately & only fetch the page data in preview mode
There's a new beta available for next-sanity but I haven't checked if it solves this issue: https://sanity-io-land.slack.com/archives/CB4MVT2AE/p1639747394005000
Thanks
user G
— I've tried a couple of things from your suggestions... updated to the beta, and also took a look at how I'm using subqueries. I suspect my code may have changed since the last time I shared it so I can't actually see if I'm still using subqueries... as far as I can tell I'm not
Can you see any problems here? This is driving me nuts :lolsob: and the client is putting the screws in saying they need it by today hahahah

https://github.com/Modular-Everything/nrg/tree/main/web
I've just looked... I think in this line

 
 ​  ​const​ ​{​ ​data​: ​page​ ​}​ ​=​ ​usePreviewSubscription​(​pageQuery​,​ ​{
Should it just be

const page = usePreview...
Instead? Since
pageQuery
only return page data
I've just looked... I think in this line

 
 ​  ​const​ ​{​ ​data​: ​page​ ​}​ ​=​ ​usePreviewSubscription​(​pageQuery​,​ ​{
Should it just be

const page = usePreview...
Instead? Since
pageQuery
only return page data
And your initial data should just be
data.page
Thanks Derek... so I tried this, makes a lot of sense! Unfortunately I'm still getting the same result
I tried logging the output of
page
and get this:
The data loads, then unloads
Ah my bad, usePreview actually doesnt return the data directly 😅 I'll check again when I get on a laptop
We resolved this in DM, the culprit is a wrongly named query params 🎉
Derek has once again bailed me out of a tight spot. The problem with the above code (full credit for the fix goes to Derek) was I was using
slug
where I should have been using
page
:

  const slug = data?.page?.slug;
  const { data: page } = usePreviewSubscription(pageQuery, {
    params: { page: slug }, // previously this was just 'slug'
    initialData: data.page,
    enabled: preview && slug,
    useGroqBeta: true,
  });
and just like that it works
🙂 thank you
user G

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?