This repository has been archived by the owner on Apr 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
Error using Preview mode: ReferenceError: locale is not defined #74
Comments
m8ker
changed the title
Issues using Preview mode on this starter project.
Error using Preview mode: ReferenceError: locale is not defined
Feb 3, 2022
You need to update http://localhost:3000/api/preview?&secret=abced&slug=secret&locale=en import { getPageData } from "utils/api"
import { parseCookies } from "utils/parse-cookies"
const preview = async (req, res) => {
// Check the secret and next parameters
// This secret should only be known to this API route and the CMS
if (req.query.secret !== (process.env.PREVIEW_SECRET || "secret-token")) {
return res.status(401).json({ message: "Invalid token" })
}
const cookies = parseCookies(req)
const slugArray = req.query.slug.split("/")
const localeID = req.query.locale || "en"
// Fetch the headless CMS to check if the provided `slug` exists
const pageData = await getPageData({
locale: localeID,
slug: slugArray.join("/"),
preview: true,
})
// If the slug doesn't exist prevent preview mode from being enabled
if (!pageData) {
return res.status(401).json({ message: "Invalid slug" })
}
// Enable Preview Mode by setting the cookies
res.setPreviewData({})
// Redirect to the path from the fetched post
// We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities
// Prefix with locale so previews are available in all languages
res.writeHead(307, {
Location: `/${pageData.attributes.locale}/${pageData.attributes.slug}`,
})
res.end()
}
export default preview
// You can view Preview pages with URLs like this:
// http://localhost:3000/api/preview?secret=<preview-secret>&slug=<slug>
// where <preview-secret> is the secret token defined in your .env config
// and where <slug> is the slug you entered in Strapi for your page
// The slug must match the current locale |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I set this starter project up locally with no customization, and I'm having issues getting preview mode to work. I get this error:
Has anyone else encountered this? Is the localization plugin compatible with preview mode?
I tried to add a parameter to my preview url but it had no effect:
Windows 11
Node.js v14.18.3
The text was updated successfully, but these errors were encountered: