Links and data fetching for dynamic routes

This commit is contained in:
Henrik Larsson
2023-08-13 22:16:15 +02:00
parent 3c19fb7a79
commit ce9c62084e
18 changed files with 46 additions and 55 deletions

View File

@@ -6,6 +6,8 @@ import CategoryPage from './pages/category-page';
import ProductPage from './pages/product-page';
import SinglePage from './pages/single-page';
export const runtime = 'edge';
export const revalidate = 43200; // 12 hours in seconds
export async function generateMetadata({
@@ -44,11 +46,25 @@ export default async function Page({ params }: PageParams) {
const { query = '', queryParams, docType } = getQueryFromSlug(slug, locale);
let pageData;
if (docType === 'page') {
pageData = await clientFetch(query, queryParams);
} else if (docType === 'product') {
pageData = await clientFetch(query, queryParams);
} else if (docType === 'category') {
pageData = await clientFetch(query, queryParams);
} else {
return;
}
if (!pageData) return notFound();
return (
<>
{docType === 'page' && <SinglePage query={query} queryParams={queryParams} />}
{docType === 'product' && <ProductPage query={query} queryParams={queryParams} />}
{docType === 'category' && <CategoryPage query={query} queryParams={queryParams} />}
{docType === 'page' && <SinglePage data={pageData} />}
{docType === 'product' && <ProductPage data={pageData} />}
{docType === 'category' && <CategoryPage data={pageData} />}
</>
);
}

View File

@@ -1,21 +1,13 @@
import Search from '@/components/search/search';
import SearchResult from '@/components/search/search-result';
import Text from '@/components/ui/text/text';
import { clientFetch } from '@/lib/sanity/sanity.client';
import { notFound } from 'next/navigation';
interface CategoryPageParams {
query: string;
queryParams: {
slug: string;
locale: string;
};
data: object | any;
}
export default async function CategoryPage({ query, queryParams }: CategoryPageParams) {
const category = await clientFetch(query, queryParams);
if (!category) return notFound();
export default async function CategoryPage({ data }: CategoryPageParams) {
const category = data;
const { title } = category;

View File

@@ -1,19 +1,10 @@
import ProductView from '@/components/product/product-view';
import { clientFetch } from '@/lib/sanity/sanity.client';
import { notFound } from 'next/navigation';
interface ProductPageParams {
query: string;
queryParams: {
slug: string;
locale: string;
};
data: object | any;
}
export default async function ProductPage({ query, queryParams }: ProductPageParams) {
const product = await clientFetch(query, queryParams);
if (!product) return notFound();
export default async function ProductPage({ data }: ProductPageParams) {
const product = data;
const productJsonLd = {
'@context': 'https://schema.org',

View File

@@ -1,23 +1,13 @@
import DynamicContentManager from '@/components/layout/dynamic-content-manager/dynamic-content-manager';
import { clientFetch } from '@/lib/sanity/sanity.client';
import { notFound } from 'next/navigation';
interface SinglePageParams {
query: string;
queryParams: {
slug: string;
locale: string;
};
data: object | any;
}
export default async function SinglePage({ query = '', queryParams }: SinglePageParams) {
const page = await clientFetch(query, queryParams);
if (!page) return notFound();
export default async function SinglePage({ data }: SinglePageParams) {
return (
<div>
<DynamicContentManager content={page?.content} />;
</div>
<>
<DynamicContentManager content={data?.content} />;
</>
);
}

View File

@@ -3,6 +3,7 @@ import { homePageQuery } from 'lib/sanity/queries';
import { clientFetch } from 'lib/sanity/sanity.client';
import { Metadata } from 'next';
import { notFound } from 'next/navigation';
export const runtime = 'edge';
export async function generateMetadata({