mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Added preview functionality for home page
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import CategoryPage from '@/components/pages/category-page';
|
||||
import ProductPage from '@/components/pages/product-page';
|
||||
import SinglePage from '@/components/pages/single-page';
|
||||
import getQueryFromSlug from '@/helpers/get-query-from-slug';
|
||||
import { clientFetch } from 'lib/sanity/sanity.client';
|
||||
import { getCachedClient } from 'lib/sanity/sanity.client';
|
||||
import type { Metadata } from 'next';
|
||||
import { notFound } from 'next/navigation';
|
||||
import CategoryPage from './pages/category-page';
|
||||
import ProductPage from './pages/product-page';
|
||||
import SinglePage from './pages/single-page';
|
||||
|
||||
export const revalidate = 43200; // 12 hours in seconds
|
||||
|
||||
@@ -17,7 +17,7 @@ export async function generateMetadata({
|
||||
|
||||
const { query = '', queryParams } = getQueryFromSlug(slug, locale);
|
||||
|
||||
const page = await clientFetch(query, queryParams);
|
||||
const page = await getCachedClient()(query, queryParams);
|
||||
|
||||
if (!page) return notFound();
|
||||
|
||||
@@ -47,11 +47,11 @@ export default async function Page({ params }: PageParams) {
|
||||
let pageData;
|
||||
|
||||
if (docType === 'page') {
|
||||
pageData = await clientFetch(query, queryParams);
|
||||
pageData = await getCachedClient()(query, queryParams);
|
||||
} else if (docType === 'product') {
|
||||
pageData = await clientFetch(query, queryParams);
|
||||
pageData = await getCachedClient()(query, queryParams);
|
||||
} else if (docType === 'category') {
|
||||
pageData = await clientFetch(query, queryParams);
|
||||
pageData = await getCachedClient()(query, queryParams);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
@@ -1,26 +0,0 @@
|
||||
import Search from '@/components/search/search';
|
||||
import SearchResult from '@/components/search/search-result';
|
||||
import Text from '@/components/ui/text/text';
|
||||
|
||||
interface CategoryPageParams {
|
||||
data: object | any;
|
||||
}
|
||||
|
||||
export default function CategoryPage({ data }: CategoryPageParams) {
|
||||
const category = data;
|
||||
|
||||
const { title } = category;
|
||||
|
||||
return (
|
||||
<div className="my-8 flex w-full flex-col px-4 lg:my-12 lg:px-8 2xl:px-16">
|
||||
{title && (
|
||||
<Text className="mb-8 lg:mb-12" variant="pageHeading">
|
||||
{title}
|
||||
</Text>
|
||||
)}
|
||||
<Search isCategory placeholder={title.toLowerCase()}>
|
||||
<SearchResult />
|
||||
</Search>
|
||||
</div>
|
||||
);
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
import ProductView from '@/components/product/product-view';
|
||||
interface ProductPageParams {
|
||||
data: object | any;
|
||||
}
|
||||
|
||||
export default function ProductPage({ data }: ProductPageParams) {
|
||||
const product = data;
|
||||
|
||||
const productJsonLd = {
|
||||
'@context': 'https://schema.org',
|
||||
'@type': 'Product',
|
||||
name: product.name,
|
||||
description: product.description,
|
||||
image: product.images[0].asset.url
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<script
|
||||
type="application/ld+json"
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: JSON.stringify(productJsonLd)
|
||||
}}
|
||||
/>
|
||||
<ProductView product={product} relatedProducts={[]} />;
|
||||
</>
|
||||
);
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
import DynamicContentManager from '@/components/layout/dynamic-content-manager/dynamic-content-manager';
|
||||
|
||||
interface SinglePageParams {
|
||||
data: object | any;
|
||||
}
|
||||
|
||||
export default function SinglePage({ data }: SinglePageParams) {
|
||||
return (
|
||||
<>
|
||||
<DynamicContentManager content={data?.content} />;
|
||||
</>
|
||||
);
|
||||
}
|
@@ -1,17 +1,18 @@
|
||||
import DynamicContentManager from 'components/layout/dynamic-content-manager/dynamic-content-manager';
|
||||
import HomePage from '@/components/pages/home-page';
|
||||
import HomePagePreview from '@/components/pages/home-page-preview';
|
||||
import PreviewProvider from '@/components/preview-provider';
|
||||
import { homePageQuery } from 'lib/sanity/queries';
|
||||
import { clientFetch } from 'lib/sanity/sanity.client';
|
||||
import { getCachedClient } from 'lib/sanity/sanity.client';
|
||||
import { Metadata } from 'next';
|
||||
import { draftMode } from 'next/headers';
|
||||
import { notFound } from 'next/navigation';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
export async function generateMetadata({
|
||||
params
|
||||
}: {
|
||||
params: { slug: string; locale: string };
|
||||
}): Promise<Metadata> {
|
||||
const homePage = await clientFetch(homePageQuery, params);
|
||||
const homePage = await getCachedClient()(homePageQuery, params);
|
||||
|
||||
if (!homePage) return notFound();
|
||||
|
||||
@@ -20,19 +21,26 @@ export async function generateMetadata({
|
||||
description: homePage.seo.description || homePage.description
|
||||
};
|
||||
}
|
||||
|
||||
interface HomePageParams {
|
||||
params: {
|
||||
locale: string;
|
||||
};
|
||||
}
|
||||
|
||||
export default async function HomePage({ params }: HomePageParams) {
|
||||
const data = await clientFetch(homePageQuery, params);
|
||||
export default async function IndexPage({ params }: HomePageParams) {
|
||||
const preview = draftMode().isEnabled ? { token: process.env.SANITY_API_READ_TOKEN } : undefined;
|
||||
|
||||
return (
|
||||
<>
|
||||
<DynamicContentManager content={data?.content} />
|
||||
</>
|
||||
);
|
||||
const data = await getCachedClient(preview)(homePageQuery, params);
|
||||
|
||||
if (!data) return notFound();
|
||||
|
||||
if (preview && preview.token) {
|
||||
return (
|
||||
<PreviewProvider token={preview.token}>
|
||||
<HomePagePreview initialData={data} params={params} />
|
||||
</PreviewProvider>
|
||||
);
|
||||
}
|
||||
|
||||
return <HomePage data={data} />;
|
||||
}
|
||||
|
Reference in New Issue
Block a user