Issue with Next.js live preview mode causing content to disappear on page load
23 replies
Last updated: Jan 16, 2022
C
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
Jan 13, 2022, 10:07 AM
C
I assume it's when the preview kicks in, but I'm not sure why 😞
Jan 13, 2022, 10:09 AM
C
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
https://www.sanity.io/guides/nextjs-live-preview#e0a6b3b6629c
Jan 13, 2022, 10:10 AM
J
I can't say by the video but the fact it prints:
page: nullmight 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!
Jan 13, 2022, 10:32 AM
C
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
Jan 13, 2022, 10:53 AM
C
I guess it's this that's unloading the data:
const { data: { page }, } = usePreviewSubscription(pageQuery, { params: { slug }, initialData: data, enabled: preview && slug, });
Jan 13, 2022, 11:00 AM
C
I can see that
{ page }does exist within
datathough...
Jan 13, 2022, 11:02 AM
C
Just posting here as I test things out... changing
enabledabove to
falseallows 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
Jan 13, 2022, 11:10 AM
C
Totally appreciate it's the weekend, but if anyone has any ideas here ... I'm a bit up shit creek with it 😐
Jan 15, 2022, 10:32 AM
D
It looks like you're using a subquery i.e
If so, AFAIK it doesn't work with
{ "globalMetaData": *[_type == "config"][0], "menuItem": *[_type == "config"][0], /* etc */ }
createPreviewSubscriptionHook. I ran into this same issue last year https://github.com/sanity-io/next-sanity/issues/4
Jan 15, 2022, 2:47 PM
D
I ended up fetching the global data separately & only fetch the page data in preview mode
Jan 15, 2022, 2:50 PM
D
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
Jan 15, 2022, 2:52 PM
C
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 notJan 16, 2022, 9:36 AM
C
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
https://github.com/Modular-Everything/nrg/tree/main/web
Jan 16, 2022, 9:37 AM
C
My queries are all here: https://github.com/Modular-Everything/nrg/blob/main/web/lib/queries.js and the [page] I'm working on is here:
https://github.com/Modular-Everything/nrg/blob/main/web/pages/%5Bpage%5D.js
https://github.com/Modular-Everything/nrg/blob/main/web/pages/%5Bpage%5D.js
Jan 16, 2022, 9:38 AM
D
I've just looked... I think in this line
Should it just be
Instead? Since
const { data: page } = usePreviewSubscription(pageQuery, {
const page = usePreview...
pageQueryonly return page data
Jan 16, 2022, 9:56 AM
D
I've just looked... I think in this line
Should it just be
Instead? Since
const { data: page } = usePreviewSubscription(pageQuery, {
const page = usePreview...
pageQueryonly return page data
Jan 16, 2022, 9:56 AM
D
And your initial data should just be
data.page
Jan 16, 2022, 9:57 AM
C
Thanks Derek... so I tried this, makes a lot of sense! Unfortunately I'm still getting the same result
Jan 16, 2022, 10:03 AM
C
I tried logging the output of
pageand get this:
Jan 16, 2022, 10:03 AM
C
The data loads, then unloads
Jan 16, 2022, 10:03 AM
D
Ah my bad, usePreview actually doesnt return the data directly 😅 I'll check again when I get on a laptop
Jan 16, 2022, 10:08 AM
D
We resolved this in DM, the culprit is a wrongly named query params 🎉
Jan 16, 2022, 10:46 AM
C
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
and just like that it works
🙂 thank you
slugwhere 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, });
🙂 thank you
user G
Jan 16, 2022, 10:46 AM
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.