feat: payload cms
This commit is contained in:
24
lib/mdx.ts
24
lib/mdx.ts
@@ -155,12 +155,23 @@ export async function getAllProducts(locale: string): Promise<ProductMdx[]> {
|
||||
// Fetch ALL products in a single query to avoid N+1 getPayload() calls
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
|
||||
const selectFields = {
|
||||
title: true,
|
||||
slug: true,
|
||||
sku: true,
|
||||
description: true,
|
||||
categories: true,
|
||||
images: true,
|
||||
locale: true,
|
||||
};
|
||||
|
||||
// Get products for this locale
|
||||
const result = await payload.find({
|
||||
collection: 'products',
|
||||
where: { locale: { equals: locale } },
|
||||
depth: 1,
|
||||
pagination: false,
|
||||
select: selectFields,
|
||||
});
|
||||
|
||||
let products: ProductMdx[] = result.docs
|
||||
@@ -174,15 +185,15 @@ export async function getAllProducts(locale: string): Promise<ProductMdx[]> {
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
sku: doc.sku,
|
||||
description: doc.description,
|
||||
sku: doc.sku || '',
|
||||
description: doc.description || '',
|
||||
categories: Array.isArray(doc.categories) ? doc.categories.map((c: any) => c.category) : [],
|
||||
images: ((doc.images as any[]) || [])
|
||||
.map((img) => (typeof img === 'string' ? img : img.url))
|
||||
.filter(Boolean),
|
||||
locale: doc.locale,
|
||||
},
|
||||
content: doc.content,
|
||||
content: null, // Avoided loading into memory!
|
||||
}));
|
||||
|
||||
// Also include English fallbacks for slugs not in this locale
|
||||
@@ -193,6 +204,7 @@ export async function getAllProducts(locale: string): Promise<ProductMdx[]> {
|
||||
where: { locale: { equals: 'en' } },
|
||||
depth: 1,
|
||||
pagination: false,
|
||||
select: selectFields,
|
||||
});
|
||||
|
||||
const fallbacks = enResult.docs
|
||||
@@ -207,8 +219,8 @@ export async function getAllProducts(locale: string): Promise<ProductMdx[]> {
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
sku: doc.sku,
|
||||
description: doc.description,
|
||||
sku: doc.sku || '',
|
||||
description: doc.description || '',
|
||||
categories: Array.isArray(doc.categories)
|
||||
? doc.categories.map((c: any) => c.category)
|
||||
: [],
|
||||
@@ -218,7 +230,7 @@ export async function getAllProducts(locale: string): Promise<ProductMdx[]> {
|
||||
locale: doc.locale,
|
||||
isFallback: true,
|
||||
},
|
||||
content: doc.content,
|
||||
content: null, // Avoided loading into memory!
|
||||
}));
|
||||
|
||||
products = [...products, ...fallbacks];
|
||||
|
||||
Reference in New Issue
Block a user