{"ast":null,"code":"import { getDocumentById } from 'lib/api';\nexport default async function preview(req, res) {\n  // Check the secret and next parameters\n  // This secret should only be known to this API route and the CMS\n  if (req.query.secret !== process.env.SANITY_PREVIEW_SECRET) return res.status(401).json({\n    message: 'Invalid token'\n  });\n  if (!req.query.id) return res.status(401).json({\n    message: 'ID must be be set'\n  }); // Fetch the headless CMS to check if the provided `id` exists\n\n  const page = await getDocumentById(req.query.id, true); // If the slug doesn't exist prevent preview mode from being enabled\n\n  if (!page) {\n    return res.status(401).json({\n      message: 'Invalid slug'\n    });\n  } // Enable Preview Mode by setting the cookies\n\n\n  res.setPreviewData({}); // Redirect to the path from the fetched page\n  // We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities\n\n  res.writeHead(307, {\n    Location: `/${page._type}/${page.slug}`\n  });\n  res.end();\n}","map":null,"metadata":{},"sourceType":"module"}