Added preview functionality for home page

This commit is contained in:
Henrik Larsson
2023-08-14 23:22:54 +02:00
parent 31b5f2a8b9
commit bfdfeeaf97
25 changed files with 263 additions and 327 deletions

View File

@@ -1,99 +0,0 @@
// route handler enabling draft mode
import { categoryQuery, homePageQuery, pageQuery, productQuery } from 'lib/sanity/queries';
import { client } from 'lib/sanity/sanity.client';
import { draftMode } from 'next/headers';
const draftSecret = process.env.NEXT_PUBLIC_SANITY_DRAFT_TOKEN
export async function GET(request: Request) {
// Enable Draft Mode by setting the cookie
draftMode().enable();
// 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 type = searchParams.get('type');
// Make sure there's a valid draft token.
if (secret !== draftSecret) {
return new Response('Invalid token', { status: 401 });
}
// Make sure there's a slug provided.
if (!slug) {
return new Response('No slug provided', { status: 401 });
}
// Make sure there's a locale provided.
if (!locale) {
return new Response('No locale provided', { status: 401 });
}
// Make sure there's a type provided.
if (!type) {
return new Response('No type provided', { status: 401 });
}
// Types available for preview - Check if the post with the given `slug` exists
const home = await client.fetch(homePageQuery, {
slug: slug,
locale: locale,
})
const page = await client.fetch(pageQuery, {
slug: slug,
locale: locale,
})
const product = await client.fetch(productQuery, {
slug: slug,
locale: locale,
})
const category = await client.fetch(categoryQuery, {
slug: slug,
locale: locale,
})
draftMode().enable();
// Redirect to the path from the fetched post
// We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities
if (home && type === 'home') {
return new Response(null, {
status: 307,
headers: {
Location: `/${home.locale}/${home.slug}`,
},
})
}
if (page && type === 'page') {
return new Response(null, {
status: 307,
headers: {
Location: `/${page.locale}/${page.slug}`,
},
})
}
if (product && type === 'product') {
return new Response(null, {
status: 307,
headers: {
Location: `/${product.locale}/product/${product.slug}`,
},
})
}
if (category && type === 'category') {
return new Response(null, {
status: 307,
headers: {
Location: `/${category.locale}/category/${category.slug}`,
},
})
}
}

View File

@@ -1,12 +0,0 @@
import { draftMode } from 'next/headers';
export async function GET() {
draftMode().disable();
return new Response(null, {
status: 307,
headers: {
Location: `/`,
},
})
}

View File

@@ -0,0 +1,12 @@
import { draftMode } from "next/headers";
export async function GET() {
draftMode().disable();
return new Response(null, {
status: 307,
headers: {
Location: `/`,
},
})
}

27
app/api/preview/route.ts Normal file
View File

@@ -0,0 +1,27 @@
import { draftMode } from 'next/headers'
export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const secret = searchParams.get('secret')
// const slug = searchParams.get('slug')
const type = searchParams.get('type')
const locale = searchParams.get('locale')
// Check the secret and next parameters
// This secret should only be known to this route handler and the CMS
if (secret !== process.env.SANITY_API_READ_TOKEN) {
return new Response('Invalid token', { status: 401 })
}
draftMode().enable()
if (type === 'home') {
return new Response(null, {
status: 307,
headers: {
Location: `/${locale}`,
},
})
}
}