Preview not redirecting to correct page

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 '/'
}

Hello Volkat,

Welcome to the Prismic community.

Ensure that in your repo Settings > Previews that the link resolver route is set up correctly to redirect to the correct page. In the case of Next.js, add the following route for the Link Resolver field: /api/preview.

Have you added linkResolver in the prismic-configuration.js file?

Looking forward to hearing from you back.

Thanks,

Priyanka

Hi Priyanka,

I actually got it to work. It must have been an error from my side!

Thanks.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Hi Vokalt,

I'm glad that it's working for you. Don't hesitate to reach out to us if you have any doubt.

Thanks,

Priyanka