Added preview functionality for pages

This commit is contained in:
Henrik Larsson 2023-08-15 09:10:12 +02:00
parent e567914192
commit 15568378f5
7 changed files with 62 additions and 16 deletions

View File

@ -1,13 +1,14 @@
import CategoryPage from '@/components/pages/category-page'; import CategoryPage from '@/components/pages/category-page';
import ProductPage from '@/components/pages/product-page'; import ProductPage from '@/components/pages/product-page';
import SinglePage from '@/components/pages/single-page'; import SinglePage from '@/components/pages/single-page';
import SinglePagePreview from '@/components/pages/single-page-preview';
import PreviewProvider from '@/components/preview-provider';
import getQueryFromSlug from '@/helpers/get-query-from-slug'; import getQueryFromSlug from '@/helpers/get-query-from-slug';
import { getCachedClient } from 'lib/sanity/sanity.client'; import { getCachedClient } from 'lib/sanity/sanity.client';
import type { Metadata } from 'next'; import type { Metadata } from 'next';
import { draftMode } from 'next/headers';
import { notFound } from 'next/navigation'; import { notFound } from 'next/navigation';
export const revalidate = 43200; // 12 hours in seconds
export async function generateMetadata({ export async function generateMetadata({
params params
}: { }: {
@ -40,6 +41,8 @@ interface PageParams {
} }
export default async function Page({ params }: PageParams) { export default async function Page({ params }: PageParams) {
const preview = draftMode().isEnabled ? { token: process.env.SANITY_API_READ_TOKEN } : undefined;
const { slug, locale } = params; const { slug, locale } = params;
const { query = '', queryParams, docType } = getQueryFromSlug(slug, locale); const { query = '', queryParams, docType } = getQueryFromSlug(slug, locale);
@ -58,6 +61,14 @@ export default async function Page({ params }: PageParams) {
if (!pageData) return notFound(); if (!pageData) return notFound();
if (preview && preview.token) {
return (
<PreviewProvider token={preview.token}>
{docType === 'page' && <SinglePagePreview initialData={pageData} params={queryParams} />}
</PreviewProvider>
);
}
return ( return (
<> <>
{docType === 'page' && <SinglePage data={pageData} />} {docType === 'page' && <SinglePage data={pageData} />}

View File

@ -3,7 +3,7 @@ import { draftMode } from 'next/headers'
export async function GET(request: Request) { export async function GET(request: Request) {
const { searchParams } = new URL(request.url) const { searchParams } = new URL(request.url)
const secret = searchParams.get('secret') const secret = searchParams.get('secret')
// const slug = searchParams.get('slug') const slug = searchParams.get('slug')
const type = searchParams.get('type') const type = searchParams.get('type')
const locale = searchParams.get('locale') const locale = searchParams.get('locale')
@ -15,7 +15,6 @@ export async function GET(request: Request) {
draftMode().enable() draftMode().enable()
if (type === 'home') { if (type === 'home') {
return new Response(null, { return new Response(null, {
status: 307, status: 307,
@ -24,4 +23,13 @@ export async function GET(request: Request) {
}, },
}) })
} }
if (type === 'page') {
return new Response(null, {
status: 307,
headers: {
Location: `/${locale}${slug}`,
},
})
}
} }

View File

@ -18,7 +18,7 @@ export default function HomePagePreview({ initialData, params }: HomePagePreview
return ( return (
<> <>
<HomePage data={data} />;{/* @ts-ignore */} <HomePage data={data} />;{/* @ts-ignore */}
<PreviewBanner title={data?.title} /> <PreviewBanner title={data?.title} type={data?._type} />
</> </>
); );
} }

View File

@ -0,0 +1,26 @@
'use client';
import PreviewBanner from '@/components/ui/preview-banner';
import { pageQuery } from '@/lib/sanity/queries';
import { useLiveQuery } from '@sanity/preview-kit';
import SinglePage from './single-page';
interface SinglePagePreviewParams {
initialData: [];
params: {
locale: string;
slug: string;
};
}
export default function SinglePagePreview({ initialData, params }: SinglePagePreviewParams) {
const [data] = useLiveQuery(initialData, pageQuery, params);
return (
<>
<SinglePage data={data && data} />
{/* @ts-ignore */}
<PreviewBanner title={data?.title} type={data?._type} />
</>
);
}

View File

@ -4,16 +4,20 @@ import { useTranslations } from 'next-intl';
import Link from 'next/link'; import Link from 'next/link';
interface PreviewBannerProps { interface PreviewBannerProps {
title?: string; title: string;
type?: string;
} }
const PreviewBanner = ({ title }: PreviewBannerProps) => { const PreviewBanner = ({ title, type }: PreviewBannerProps) => {
const t = useTranslations('ui.previewBanner'); const t = useTranslations('ui.previewBanner');
return ( return (
<div className="fixed bottom-0 right-0 z-50 flex w-full items-center justify-between border-t border-high-contrast bg-app p-6"> <div className="fixed bottom-0 right-0 z-50 flex w-full items-center justify-between border-t border-ui-border bg-app p-4">
{title && ( {title && (
<p className="text-lg"> <p className="text-lg">
{t('titlePart')} <span className="font-bold italic">{title}</span> {t('titlePart')}{' '}
<span className="font-bold italic">
{title} {type && `(${type})`}
</span>
</p> </p>
)} )}
<Link <Link

View File

@ -41,8 +41,6 @@ export const SANITY_API_VERSION = '2022-10-25'
// Set this to enable helper links in document status banners and shortcut links on products and collections. // Set this to enable helper links in document status banners and shortcut links on products and collections.
export const STORM_STORE_ID = '' export const STORM_STORE_ID = ''
export const SANITY_STUDIO_API_READ_TOKEN = process.env.SANITY_STUDIO_API_READ_TOKEN;
// Project preview URLs // Project preview URLs
export const localStorefrontUrl = 'http://localhost:3000'; export const localStorefrontUrl = 'http://localhost:3000';
export const localStorefrontPreviewUrl = 'http://localhost:3000/api/preview'; export const localStorefrontPreviewUrl = 'http://localhost:3000/api/preview';

View File

@ -1,9 +1,8 @@
import {ListItemBuilder} from 'sanity/desk' import { DocumentsIcon, EyeOpenIcon, MasterDetailIcon } from '@sanity/icons'
import defineStructure from '../utils/define-structure' import { SanityDocument } from 'sanity'
import {DocumentsIcon} from '@sanity/icons'
import Iframe from 'sanity-plugin-iframe-pane' import Iframe from 'sanity-plugin-iframe-pane'
import {SanityDocument} from 'sanity' import { ListItemBuilder } from 'sanity/desk'
import {EyeOpenIcon, MasterDetailIcon} from '@sanity/icons' import defineStructure from '../utils/define-structure'
import getPreviewUrl from '../utils/get-preview-url' import getPreviewUrl from '../utils/get-preview-url'
export default defineStructure<ListItemBuilder>((S) => export default defineStructure<ListItemBuilder>((S) =>