-
I am attempting to set up a preview mode with Sanity on Next v13 According to the beta docs, there is a previewData function intended for server components: https://beta.nextjs.org/docs/api-reference/preview-data However, when I try to use this in any server component (either
How are we supposed to use this function? Edit -- As expected, this also causes an error when used in generateStaticParams -- here's the test file for reference: import { getClient } from "../../lib/sanity/index.server"
import { getLandingPageBySlug } from "../../helpers/sanity";
import { previewData } from 'next/headers'
const LandingPage = ({ params: {landingPageSlug} }) => {
const preview = previewData();
const isPreview = !!preview && preview.key === process.env.PREVIEW_SECRET
console.log(preview)
const landingPageData = getLandingPageBySlug(landingPageSlug)
return <div>Page!</div>
}
export default LandingPage
export const generateStaticParams = async () => {
const landingPages = await getClient().fetch(/* groq */ `*[_type == 'landingPage']{ pageSlug }`)
return landingPages.map(landingPage => ({ landingPageSlug: landingPage.pageSlug.current}))
} |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 7 replies
-
For me, I see it working fine until I add the What is the correct way to use it in conjunction w/ |
Beta Was this translation helpful? Give feedback.
-
It seems like there's an issue when trying to use next/headers with generateStaticParams (see this comment). I'm also running into this implementing next-auth in the app directory - as soon as next/headers is used, generateStaticParams throws the above error when implemented anywhere in the folder structure. Hope this is addressed by the Next team soon. @leerob wondering if this has been flagged yet or if there's a solution. |
Beta Was this translation helpful? Give feedback.
-
I guess that the 'previewData()' is the same as cookies() and headers() inside next/headers. According to the beta docs here, if you use one of those on page or layout will force dynamic rendering. I'm not using I played around a bit also with the Route Segment Config options, setting the dynamic option to 'error' (according to the beta docs is the same as getStaticProps in pages) I receive an error at build time:
I don't know if it's a bug or it's how should work also after the beta, but it looks like that as soon as you use the preview mode your pages are forced to be dynamic. If I'm not wrong this was not the case with the pages directory. Anyway...as @rolldavid said, let's see if the Next team will give us an update, maybe there is a proper way to do it. |
Beta Was this translation helpful? Give feedback.
-
Dump question. But do I have to set previewData like in nextjs 12? If I do it like in the docs preview data is all the time false. And what's your preview url? which parameters/tokens are set? |
Beta Was this translation helpful? Give feedback.
-
Somehow it works with both File in question: https://github.com/estubmo/nextjs-blog-cms-sanity-v3/blob/main/app/posts/%5Bslug%5D/page.tsx I have another repo where I |
Beta Was this translation helpful? Give feedback.
-
I am also getting weird behaviour with this. I've followed the same setup per the documentation. The basic implementation is: // ./src/pages/api/preview.ts
import {NextApiRequest, NextApiResponse} from 'next';
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
res.setPreviewData({});
res.writeHead(301, {Location: req.query?.return || '/'});
res.end();
};
export default handler; When I visit this URL (e.g. on local it's Weirdly, if I close my dev client, delete the Currently running 13.1.0. |
Beta Was this translation helpful? Give feedback.
-
Preview mode is not ready in the We will release more information as things are ready to use 🙏 |
Beta Was this translation helpful? Give feedback.
-
I guess most of you following this know, but just in case (since I was looking at this thread on weekly basis in case Github would fail to send me an update on it as I was waiting for this feature 😉 ) . Draft mode which replaces preview mode for the app directory has just been released in 13.4: https://nextjs.org/docs/app/building-your-application/configuring/draft-mode. Beware of this little issue: #49237 |
Beta Was this translation helpful? Give feedback.
I guess most of you following this know, but just in case (since I was looking at this thread on weekly basis in case Github would fail to send me an update on it as I was waiting for this feature 😉 ) .
Draft mode which replaces preview mode for the app directory has just been released in 13.4: https://nextjs.org/docs/app/building-your-application/configuring/draft-mode.
Beware of this little issue: #49237