mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Test
This commit is contained in:
@@ -1,17 +1,14 @@
|
||||
import CategoryPage from '@/components/pages/category-page';
|
||||
import ProductPage from '@/components/pages/product-page';
|
||||
import SearchPage from '@/components/pages/search-page';
|
||||
import SearchPagePreview from '@/components/pages/search-page-preview';
|
||||
import SinglePage from '@/components/pages/single-page';
|
||||
import SinglePagePreview from '@/components/pages/single-page-preview';
|
||||
import PreviewProvider from '@/components/preview-provider';
|
||||
// import PreviewProvider from '@/components/preview-provider';
|
||||
import getQueryFromSlug from '@/helpers/get-query-from-slug';
|
||||
import { getCachedClient } from 'lib/sanity/sanity.client';
|
||||
import { getCategory, getPage, getProduct, getSearch } from '@/lib/sanity/sanity.fetch';
|
||||
import type { Metadata } from 'next';
|
||||
import { draftMode } from 'next/headers';
|
||||
import { notFound } from 'next/navigation';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
export const runtime = 'edge';
|
||||
|
||||
export async function generateMetadata({
|
||||
params
|
||||
@@ -20,18 +17,23 @@ export async function generateMetadata({
|
||||
}): Promise<Metadata> {
|
||||
const { slug, locale } = params;
|
||||
|
||||
const { query = '', queryParams } = getQueryFromSlug(slug, locale);
|
||||
const { queryParams, docType } = getQueryFromSlug(slug, locale);
|
||||
|
||||
const page = await getCachedClient()(query, queryParams);
|
||||
let page;
|
||||
|
||||
docType === 'page' && (page = await getPage(queryParams.slug, queryParams.locale));
|
||||
docType === 'product' && (page = await getProduct(queryParams.slug, queryParams.locale));
|
||||
docType === 'category' && (page = await getCategory(queryParams.slug, queryParams.locale));
|
||||
docType === 'search' && (page = await getSearch(queryParams.slug, queryParams.locale));
|
||||
|
||||
if (!page) return notFound();
|
||||
|
||||
return {
|
||||
title: `${page.seo?.title || page.title}`,
|
||||
description: page.seo?.description || page.bodySummary,
|
||||
description: page.seo?.description,
|
||||
openGraph: {
|
||||
publishedTime: page.createdAt,
|
||||
modifiedTime: page.updatedAt,
|
||||
// publishedTime: page.createdAt,
|
||||
// modifiedTime: page.updatedAt,
|
||||
type: 'article'
|
||||
}
|
||||
};
|
||||
@@ -39,49 +41,38 @@ export async function generateMetadata({
|
||||
|
||||
interface PageParams {
|
||||
params: {
|
||||
locale: string;
|
||||
slug: string[];
|
||||
locale: string;
|
||||
};
|
||||
}
|
||||
|
||||
export default async function Page({ params }: PageParams) {
|
||||
const preview = draftMode().isEnabled ? { token: process.env.SANITY_API_READ_TOKEN } : undefined;
|
||||
|
||||
const { slug, locale } = params;
|
||||
|
||||
const { query = '', queryParams, docType } = getQueryFromSlug(slug, locale);
|
||||
const { queryParams, docType } = getQueryFromSlug(slug, locale);
|
||||
|
||||
let pageData;
|
||||
let data;
|
||||
|
||||
if (docType === 'page') {
|
||||
pageData = await getCachedClient()(query, queryParams);
|
||||
data = await getPage(queryParams.slug, queryParams.locale);
|
||||
} else if (docType === 'product') {
|
||||
pageData = await getCachedClient()(query, queryParams);
|
||||
data = await getProduct(queryParams.slug, queryParams.locale);
|
||||
} else if (docType === 'category') {
|
||||
pageData = await getCachedClient()(query, queryParams);
|
||||
data = await getCategory(queryParams.slug, queryParams.locale);
|
||||
} else if (docType === 'search') {
|
||||
pageData = await getCachedClient()(query, queryParams);
|
||||
} else {
|
||||
return;
|
||||
data = await getSearch(queryParams.slug, queryParams.locale);
|
||||
}
|
||||
|
||||
if (!pageData) return notFound();
|
||||
|
||||
if (preview && preview.token) {
|
||||
return (
|
||||
<PreviewProvider token={preview.token}>
|
||||
{docType === 'page' && <SinglePagePreview initialData={pageData} params={queryParams} />}
|
||||
{docType === 'search' && <SearchPagePreview initialData={pageData} params={queryParams} />}
|
||||
</PreviewProvider>
|
||||
);
|
||||
if (!data) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{docType === 'page' && <SinglePage data={pageData} />}
|
||||
{docType === 'product' && <ProductPage data={pageData} />}
|
||||
{docType === 'category' && <CategoryPage data={pageData} />}
|
||||
{docType === 'search' && <SearchPage data={pageData} />}
|
||||
{docType === 'page' && <SinglePage data={data} />}
|
||||
{docType === 'product' && <ProductPage data={data} />}
|
||||
{docType === 'category' && <CategoryPage data={data} />}
|
||||
{docType === 'search' && <SearchPage data={data} />}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
@@ -1,8 +1,11 @@
|
||||
import PreviewProvider from '@/components/preview/preview-provider';
|
||||
import { token } from '@/lib/sanity/sanity.fetch';
|
||||
import { Analytics } from '@vercel/analytics/react';
|
||||
import Footer from 'components/layout/footer/footer';
|
||||
import Header from 'components/layout/header/header';
|
||||
import { NextIntlClientProvider } from 'next-intl';
|
||||
import { Inter } from 'next/font/google';
|
||||
import { draftMode } from 'next/headers';
|
||||
import { notFound } from 'next/navigation';
|
||||
import { ReactNode, Suspense } from 'react';
|
||||
import { supportedLanguages } from '../../../i18n-config';
|
||||
@@ -54,7 +57,9 @@ export default async function LocaleLayout({ children, params: { locale } }: Loc
|
||||
notFound();
|
||||
}
|
||||
|
||||
return (
|
||||
const isDraftMode = draftMode().isEnabled;
|
||||
|
||||
const layout = (
|
||||
<html lang={locale} className={inter.variable}>
|
||||
<body className="flex min-h-screen flex-col">
|
||||
<NextIntlClientProvider locale={locale} messages={messages}>
|
||||
@@ -70,4 +75,10 @@ export default async function LocaleLayout({ children, params: { locale } }: Loc
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
if (isDraftMode) {
|
||||
return <PreviewProvider token={token!}>{layout}</PreviewProvider>;
|
||||
}
|
||||
|
||||
return layout;
|
||||
}
|
||||
|
@@ -8,7 +8,6 @@ import { draftMode } from 'next/headers';
|
||||
import { notFound } from 'next/navigation';
|
||||
|
||||
export const runtime = 'edge';
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function generateMetadata({
|
||||
params
|
||||
@@ -31,16 +30,12 @@ interface HomePageParams {
|
||||
}
|
||||
|
||||
export default async function IndexPage({ params }: HomePageParams) {
|
||||
// const preview = draftMode().isEnabled ? { token: process.env.SANITY_API_READ_TOKEN } : undefined;
|
||||
|
||||
const data = await getHomePage(params.locale);
|
||||
|
||||
if (!data && !draftMode().isEnabled) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
console.log('Preview:', draftMode().isEnabled);
|
||||
|
||||
return (
|
||||
<LiveQuery
|
||||
enabled={draftMode().isEnabled}
|
||||
|
Reference in New Issue
Block a user