Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 3x 3x 2x 1x 1x 1x 3x 3x 3x 1x 4x 4x 4x 4x 4x 4x 4x 1x 3x 4x 4x 3x 1x 2x 3x | import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'
function getPreviewPath(contentType: string | undefined, slug: string | null, locale: string | null, status: string | null): string {
const basePath = (() => {
if (!contentType) return '/';
if (contentType === 'post' || contentType.includes('posts')) {
return slug ? '/blog/' + slug : '/blog';
}
Eif (contentType === 'page' || contentType.includes('pages')) {
return slug ? '/' + slug : '/';
}
return '/' + contentType;
})();
const localePath = locale && locale !== 'en' ? '/' + locale + basePath : basePath;
const statusParam = status ? '?status=' + status : '';
return localePath + statusParam;
}
export const GET = async (request: Request) => {
// Parse query string parameters
const { searchParams } = new URL(request.url);
const secret = searchParams.get('secret');
const slug = searchParams.get('slug');
const locale = searchParams.get('locale');
const uid = searchParams.get('uid');
const status = searchParams.get('status');
// Check the secret and next parameters
if (secret !== process.env.PREVIEW_SECRET) {
return new Response('Invalid token', { status: 401 });
}
const contentType = uid?.split(".").pop();
const finalPath = getPreviewPath(contentType, slug, locale, status);
// Enable Draft Mode by setting the cookie
const draft = await draftMode();
if (status === 'draft') {
draft.enable();
} else {
draft.disable();
}
// Redirect to the path from the fetched post
redirect(finalPath);
}; |