Hi. Full disclosure, I’m relatively new to both NextJS and Prismic, and a complete Netlify function newb. This may be super easy. I am trying to get the preview function in Prismic to work on my Netlify site. Part of the Prismic support forums indicated the reason is that the /api/preview.js route isn’t natively routed by Netlify, and is natively converted to serverless functions on platforms like Vercel. So, I think I need to convert the preview.js into some kind of Netlify function, but I’m not sure where to start, even after reading the functions documentation. The preview.js file looks like this:
import { Client, linkResolver } from "../../prismic-configuration"
export default async (req, res) => {
console.log("In Preview.js");
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.write(
`<!DOCTYPE html><html><head><meta http-equiv="Refresh" content="0; url=${redirectUrl}" />
<script>window.location.href = '${redirectUrl}'</script>
</head>`
);
res.end();
};
Any help would be super appreciated. This is my last hurdle with this project. Everything else has been breezy with Netlify.