Next.js Cannot preview non master locale content

Hi there,

I have setup previews for a site built in NextJS, however previews are only working for the master locale.

When previewing a non master locale, I get the error No documents were returned. This error is returned from client.resolvePreviewURL. Even though ref and documentId have the expected values.

Preview file:

const preview = async (req, res) => {
  const { token: ref, documentId } = req.query;

  const redirectUrl = await client.resolvePreviewURL({
    linkResolver,
    defaultURL: '/',
    previewToken: ref,
    documentID: documentId,
  });

  if (!redirectUrl) {
    return res.status(401).json({ message: 'Invalid token' });
  }

  res.setPreviewData({ ref });

  res.write(
    `<!DOCTYPE html><html><head><meta http-equiv="Refresh" content="0; url=${redirectUrl}" />
    <script>window.location.href = '${redirectUrl}'</script>
    </head>`
  );

  res.end();
};

Link resolver:

export const linkResolver = (document) => {
  const lang = getLocaleByLanguage(document.lang);

  switch (document.type) {
    case 'blog_post':
      return `/${lang}/blog/${document.data.category.uid}/${document.uid}`;
    case 'blog_category':
      return `/${lang}/blog/${document.uid}`;
    case 'page_blog':
      return `/${lang}/blog`;
    default:
      return '/';
  }
};

package.json

"@prismicio/client": "^6.0.0",
"next": "12.0.7",

Any help or advice would be appreciated :slight_smile:

Thanks,
Sarah

Hi @sarah1,

Welcome to the Prismic community,

I am not sure what is causing the previews not to work, but I will try to troubleshoot this issue.

First of all, can you tell us if the preview works when previewing unpublished documents (Draft), and does the toolbar open when you try to preview locally?

Also, it would be helpful to add a code snippet of the way you initialize the Prismic client and any errors you get in your browser console and a screenshot of Prismic and Next.js cookies (you can get that in the browser console) such as:

Looking forward to your reply,
Fares

Thanks for responding Fares, this issue was fixed by upgrading @prismicio/client to version 6.1.0 :slight_smile:

1 Like