mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
## Description This PR ensures that the default prettier config is used for examples and templates. This config is compatible with `prettier@3` as well (upgrading prettier is bigger change that can be a future PR). ## Changes - Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will be needed upgrading to prettier@3) - Added `examples/.prettierrc.json` with default config (this will change every example) - Added `packages/create-next-app/templates/.prettierrc.json` with default config (this will change every template) ## Related - Fixes #54402 - Closes #54409
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import { getPreviewPost } from "../../lib/api";
|
|
|
|
export default async function preview(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
) {
|
|
const { secret, id, slug } = req.query;
|
|
|
|
// Check the secret and next parameters
|
|
// This secret should only be known by this API route
|
|
if (
|
|
!process.env.WORDPRESS_PREVIEW_SECRET ||
|
|
secret !== process.env.WORDPRESS_PREVIEW_SECRET ||
|
|
(!id && !slug)
|
|
) {
|
|
return res.status(401).json({ message: "Invalid token" });
|
|
}
|
|
|
|
// Fetch WordPress to check if the provided `id` or `slug` exists
|
|
const post = await getPreviewPost(id || slug, id ? "DATABASE_ID" : "SLUG");
|
|
|
|
// If the post doesn't exist prevent preview mode from being enabled
|
|
if (!post) {
|
|
return res.status(401).json({ message: "Post not found" });
|
|
}
|
|
|
|
// Enable Preview Mode by setting the cookies
|
|
res.setPreviewData({
|
|
post: {
|
|
id: post.databaseId,
|
|
slug: post.slug,
|
|
status: post.status,
|
|
},
|
|
});
|
|
|
|
// Redirect to the path from the fetched post
|
|
// We don't redirect to `req.query.slug` as that might lead to open redirect vulnerabilities
|
|
res.writeHead(307, { Location: `/posts/${post.slug || post.databaseId}` });
|
|
res.end();
|
|
}
|