Hello, I'm trying to set up the preview functionality on some of my websites. I've gotten the preview toolbar to show up and the content seems to be correct when viewing the specific changes that I've made.
However, there is a problem that I can't really figure out. When I press the preview button in prismic I am redirected to the root index.js file of the project and not the actual say news article that I wanted to preview. Now I have to manually navigate to that page to preview the changes which is not optimal.
Below is the code for the Preview which is provided in all the examples provided by Next.js and Prismic. My understanding is that what I need to do is replace the second argument of the resolve function to the path of the actual page I want to preview so that the redirect is redirected to the correct page. However there is no data that I can use to do that inside the preview function.
Does anybody have a solution for this?
import { linkResolver } from 'prismic-configuration'
import { Client } from 'utils/prismicHelpers'
const Preview = async (req, res) => {
const { token: ref, documentId } = req.query
const redirectUrl = await Client(req)
.getPreviewResolver(ref, documentId)
.resolve(linkResolver, '/')
if (!redirectUrl) {
return res.status(401).json({ message: 'Invalid token' })
}
res.setPreviewData({ ref })
res.writeHead(302, { Location: `${redirectUrl}` })
res.end()
}
export default Preview
export const linkResolver = (doc) => {
if (doc?.type === 'fund') {
return `/fond/${doc?.uid}`
}
if (doc?.type === 'contact') {
return '/kontakt'
}
if (doc?.type === 'news_overview') {
return '/nyheter'
}
if (doc?.type === 'page') {
return `/${doc?.uid}`
}
if (doc?.type === 'information') {
return `/information/${doc?.uid}`
}
return '/'
}