feat: migrate Payload CMS to MDX and harden static infrastructure
This commit is contained in:
199
lib/blog.ts
199
lib/blog.ts
@@ -1,5 +1,6 @@
|
||||
import { getPayload } from 'payload';
|
||||
import configPromise from '@payload-config';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
import { config } from '@/lib/config';
|
||||
|
||||
export function extractExcerpt(content: string): string {
|
||||
@@ -57,185 +58,59 @@ export function isPostVisible(post: { frontmatter: { date: string; public?: bool
|
||||
|
||||
export async function getPostBySlug(slug: string, locale: string): Promise<PostData | null> {
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
const filePath = path.join(process.cwd(), 'content', 'posts', `${slug}.mdx`);
|
||||
const fileContent = await fs.readFile(filePath, 'utf-8');
|
||||
const { data, content } = matter(fileContent);
|
||||
|
||||
// First try: Find in the requested locale
|
||||
let { docs } = await payload.find({
|
||||
collection: 'posts',
|
||||
where: {
|
||||
slug: { equals: slug },
|
||||
...(!config.showDrafts ? { _status: { equals: 'published' } } : {}),
|
||||
},
|
||||
locale: locale as any,
|
||||
draft: config.showDrafts,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
// Fallback: If not found, try searching across all locales.
|
||||
// This happens when a user uses the static language switcher
|
||||
// e.g. switching from /en/blog/en-slug to /de/blog/en-slug.
|
||||
if (!docs || docs.length === 0) {
|
||||
const { docs: crossLocaleDocs } = await payload.find({
|
||||
collection: 'posts',
|
||||
where: {
|
||||
slug: { equals: slug },
|
||||
...(!config.showDrafts ? { _status: { equals: 'published' } } : {}),
|
||||
},
|
||||
locale: 'all',
|
||||
draft: config.showDrafts,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
if (crossLocaleDocs && crossLocaleDocs.length > 0) {
|
||||
// Fetch the found document again, but strictly in the requested locale
|
||||
// so we get the correctly translated fields (like the localized slug)
|
||||
const { docs: correctLocaleDocs } = await payload.find({
|
||||
collection: 'posts',
|
||||
where: {
|
||||
id: { equals: crossLocaleDocs[0].id },
|
||||
},
|
||||
locale: locale as any,
|
||||
draft: config.showDrafts,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
docs = correctLocaleDocs;
|
||||
let parsedContent = content;
|
||||
try {
|
||||
if (content.trim().startsWith('{')) {
|
||||
parsedContent = JSON.parse(content);
|
||||
}
|
||||
} catch (e) {
|
||||
// Not JSON
|
||||
}
|
||||
|
||||
if (!docs || docs.length === 0) return null;
|
||||
|
||||
const doc = docs[0];
|
||||
|
||||
return {
|
||||
slug: doc.slug,
|
||||
slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
date: doc.date,
|
||||
excerpt: doc.excerpt || '',
|
||||
category: doc.category || '',
|
||||
featuredImage:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.url || doc.featuredImage.sizes?.card?.url
|
||||
: null,
|
||||
focalX:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalX
|
||||
: 50,
|
||||
focalY:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalY
|
||||
: 50,
|
||||
public: doc._status === 'published',
|
||||
} as PostFrontmatter,
|
||||
content: doc.content as any, // Native Lexical Editor State
|
||||
title: data.title || '',
|
||||
date: data.date || '',
|
||||
excerpt: data.excerpt || '',
|
||||
category: data.category || '',
|
||||
featuredImage: data.featuredImage?.url || data.featuredImage || null,
|
||||
focalX: data.featuredImage?.focalX || data.focalX || 50,
|
||||
focalY: data.featuredImage?.focalY || data.focalY || 50,
|
||||
public: data.public ?? true,
|
||||
},
|
||||
content: parsedContent,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getPostBySlug failed for ${slug}:`, error);
|
||||
console.error(`getPostBySlug failed for ${slug}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getPostSlugs(slug: string, locale: string): Promise<Record<string, string>> {
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
|
||||
// First, find the post in the current locale to get its ID
|
||||
let { docs } = await payload.find({
|
||||
collection: 'posts',
|
||||
where: {
|
||||
slug: { equals: slug },
|
||||
...(!config.showDrafts ? { _status: { equals: 'published' } } : {}),
|
||||
},
|
||||
locale: locale as any,
|
||||
draft: config.showDrafts,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
if (!docs || docs.length === 0) {
|
||||
// Fallback: search across all locales
|
||||
const { docs: crossLocaleDocs } = await payload.find({
|
||||
collection: 'posts',
|
||||
where: {
|
||||
slug: { equals: slug },
|
||||
...(!config.showDrafts ? { _status: { equals: 'published' } } : {}),
|
||||
},
|
||||
locale: 'all',
|
||||
draft: config.showDrafts,
|
||||
limit: 1,
|
||||
});
|
||||
docs = crossLocaleDocs;
|
||||
}
|
||||
|
||||
if (!docs || docs.length === 0) return {};
|
||||
|
||||
const postId = docs[0].id;
|
||||
|
||||
// Fetch the post with locale 'all' to get all localized fields
|
||||
const { docs: allLocalesDocs } = await payload.find({
|
||||
collection: 'posts',
|
||||
where: {
|
||||
id: { equals: postId },
|
||||
},
|
||||
locale: 'all',
|
||||
draft: config.showDrafts,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
if (!allLocalesDocs || allLocalesDocs.length === 0) return {};
|
||||
return (allLocalesDocs[0].slug as unknown as Record<string, string>) || {};
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getPostSlugs failed for ${slug}:`, error);
|
||||
return {};
|
||||
}
|
||||
// Mock function, simply returns the slug for all locales
|
||||
return { de: slug, en: slug };
|
||||
}
|
||||
|
||||
export async function getAllPosts(locale: string): Promise<PostData[]> {
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
const { docs } = await payload.find({
|
||||
collection: 'posts',
|
||||
where: {
|
||||
...(!config.showDrafts ? { _status: { equals: 'published' } } : {}),
|
||||
},
|
||||
locale: locale as any,
|
||||
sort: '-date',
|
||||
draft: config.showDrafts,
|
||||
limit: 100,
|
||||
});
|
||||
const dir = path.join(process.cwd(), 'content', 'posts');
|
||||
const files = await fs.readdir(dir);
|
||||
const slugs = files.filter((f) => f.endsWith('.mdx')).map((f) => f.replace('.mdx', ''));
|
||||
const posts = await Promise.all(slugs.map((slug) => getPostBySlug(slug, locale)));
|
||||
|
||||
console.log(`[Payload] getAllPosts for ${locale}: Found ${docs.length} docs`);
|
||||
|
||||
const posts = docs.map((doc) => {
|
||||
return {
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
date: doc.date,
|
||||
excerpt: doc.excerpt || '',
|
||||
category: doc.category || '',
|
||||
featuredImage:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.url || doc.featuredImage.sizes?.card?.url
|
||||
: null,
|
||||
focalX:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalX
|
||||
: 50,
|
||||
focalY:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalY
|
||||
: 50,
|
||||
} as PostFrontmatter,
|
||||
// Pass the Lexical content object rather than raw markdown string
|
||||
content: doc.content as any,
|
||||
};
|
||||
});
|
||||
|
||||
// Integrity check: only show posts with a featured image in listings/sitemap
|
||||
return posts.filter((p) => !!p.frontmatter.featuredImage);
|
||||
const validPosts = posts.filter(
|
||||
(p): p is PostData => p !== null && !!p.frontmatter.featuredImage,
|
||||
);
|
||||
return validPosts.sort(
|
||||
(a, b) => new Date(b.frontmatter.date).getTime() - new Date(a.frontmatter.date).getTime(),
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getAllPosts failed for ${locale}:`, error);
|
||||
console.error(`getAllPosts failed for ${locale}:`, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
159
lib/pages.ts
159
lib/pages.ts
@@ -1,6 +1,6 @@
|
||||
import { getPayload } from 'payload';
|
||||
import configPromise from '@payload-config';
|
||||
import { mapSlugToFileSlug } from './slugs';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
import { config } from '@/lib/config';
|
||||
|
||||
export interface PageFrontmatter {
|
||||
@@ -47,138 +47,57 @@ function mapDoc(doc: any): PageData {
|
||||
|
||||
export async function getPageBySlug(slug: string, locale: string): Promise<PageData | null> {
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
const fileSlug = await mapSlugToFileSlug(slug, locale);
|
||||
const filePath = path.join(process.cwd(), 'content', 'pages', `${slug}.mdx`);
|
||||
const fileContent = await fs.readFile(filePath, 'utf-8');
|
||||
const { data, content } = matter(fileContent);
|
||||
|
||||
// Try finding exact match first
|
||||
let result = await payload.find({
|
||||
collection: 'pages',
|
||||
where: {
|
||||
and: [
|
||||
{ slug: { equals: fileSlug } },
|
||||
...(!config.showDrafts ? [{ _status: { equals: 'published' } }] : []),
|
||||
],
|
||||
},
|
||||
locale: locale as any,
|
||||
depth: 1,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
// Fallback: search ALL locales
|
||||
if (result.docs.length === 0) {
|
||||
const crossResult = await payload.find({
|
||||
collection: 'pages',
|
||||
where: {
|
||||
and: [
|
||||
{ slug: { equals: fileSlug } },
|
||||
...(!config.showDrafts ? [{ _status: { equals: 'published' } }] : []),
|
||||
],
|
||||
},
|
||||
locale: 'all',
|
||||
depth: 1,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
if (crossResult.docs.length > 0) {
|
||||
// Fetch missing exact match by internal id
|
||||
result = await payload.find({
|
||||
collection: 'pages',
|
||||
where: {
|
||||
id: { equals: crossResult.docs[0].id },
|
||||
},
|
||||
locale: locale as any,
|
||||
depth: 1,
|
||||
limit: 1,
|
||||
});
|
||||
let parsedContent = content;
|
||||
try {
|
||||
if (content.trim().startsWith('{')) {
|
||||
parsedContent = JSON.parse(content);
|
||||
}
|
||||
} catch (e) {
|
||||
// Not JSON
|
||||
}
|
||||
|
||||
if (result.docs.length > 0) {
|
||||
const doc = result.docs[0];
|
||||
|
||||
return {
|
||||
slug: doc.slug,
|
||||
redirectUrl: doc.redirectUrl,
|
||||
redirectPermanent: doc.redirectPermanent ?? true,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
excerpt: doc.excerpt || '',
|
||||
featuredImage:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
||||
: null,
|
||||
focalX:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalX
|
||||
: 50,
|
||||
focalY:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalY
|
||||
: 50,
|
||||
layout:
|
||||
doc.layout === 'fullBleed' || doc.layout === 'default'
|
||||
? doc.layout
|
||||
: ('default' as const),
|
||||
},
|
||||
content: doc.content,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
return {
|
||||
slug,
|
||||
redirectUrl: data.redirectUrl,
|
||||
redirectPermanent: data.redirectPermanent ?? true,
|
||||
frontmatter: {
|
||||
title: data.title || '',
|
||||
excerpt: data.excerpt || '',
|
||||
featuredImage: data.featuredImage?.url || data.featuredImage || null,
|
||||
focalX: data.featuredImage?.focalX || data.focalX || 50,
|
||||
focalY: data.featuredImage?.focalY || data.focalY || 50,
|
||||
layout: data.layout === 'fullBleed' || data.layout === 'default' ? data.layout : 'default',
|
||||
public: data.public ?? true,
|
||||
},
|
||||
content: parsedContent,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getPageBySlug failed for ${slug}:`, error);
|
||||
console.error(`getPageBySlug failed for ${slug}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllPages(locale: string): Promise<PageData[]> {
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
|
||||
const result = await payload.find({
|
||||
collection: 'pages' as any,
|
||||
locale: locale as any,
|
||||
limit: 100,
|
||||
});
|
||||
|
||||
return (result.docs as any[]).map(mapDoc);
|
||||
const dir = path.join(process.cwd(), 'content', 'pages');
|
||||
const files = await fs.readdir(dir);
|
||||
const slugs = files.filter((f) => f.endsWith('.mdx')).map((f) => f.replace('.mdx', ''));
|
||||
const pages = await Promise.all(slugs.map((slug) => getPageBySlug(slug, locale)));
|
||||
return pages.filter((p): p is PageData => p !== null);
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getAllPages failed for ${locale}:`, error);
|
||||
console.error(`getAllPages failed for ${locale}:`, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllPagesMetadata(locale: string): Promise<Partial<PageData>[]> {
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
|
||||
const result = await payload.find({
|
||||
collection: 'pages' as any,
|
||||
locale: locale as any,
|
||||
limit: 100,
|
||||
});
|
||||
|
||||
return (result.docs as any[]).map((doc: any) => ({
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
excerpt: doc.excerpt || '',
|
||||
featuredImage:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
||||
: null,
|
||||
focalX:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalX
|
||||
: 50,
|
||||
focalY:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalY
|
||||
: 50,
|
||||
} as PageFrontmatter,
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getAllPagesMetadata failed for ${locale}:`, error);
|
||||
return [];
|
||||
}
|
||||
const pages = await getAllPages(locale);
|
||||
return pages.map((p) => ({
|
||||
slug: p.slug,
|
||||
frontmatter: p.frontmatter,
|
||||
}));
|
||||
}
|
||||
|
||||
207
lib/products.ts
207
lib/products.ts
@@ -1,6 +1,6 @@
|
||||
import { getPayload } from 'payload';
|
||||
import configPromise from '@payload-config';
|
||||
import { mapSlugToFileSlug } from './slugs';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
import { config } from '@/lib/config';
|
||||
|
||||
export interface ProductFrontmatter {
|
||||
@@ -25,188 +25,79 @@ export async function getProductMetadata(
|
||||
slug: string,
|
||||
locale: string,
|
||||
): Promise<Partial<ProductData> | null> {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
const fileSlug = await mapSlugToFileSlug(slug, locale);
|
||||
const p = await getProductBySlug(slug, locale);
|
||||
if (!p) return null;
|
||||
return {
|
||||
slug: p.slug,
|
||||
frontmatter: p.frontmatter,
|
||||
};
|
||||
}
|
||||
|
||||
const result = await payload.find({
|
||||
collection: 'products',
|
||||
where: {
|
||||
and: [
|
||||
{ slug: { equals: fileSlug } },
|
||||
...(!config.showDrafts ? [{ _status: { equals: 'published' } }] : []),
|
||||
],
|
||||
},
|
||||
locale: locale as any,
|
||||
depth: 1,
|
||||
limit: 1,
|
||||
});
|
||||
export async function getProductBySlug(slug: string, locale: string): Promise<ProductData | null> {
|
||||
try {
|
||||
const filePath = path.join(process.cwd(), 'content', 'products', `${slug}.mdx`);
|
||||
const fileContent = await fs.readFile(filePath, 'utf-8');
|
||||
const { data, content } = matter(fileContent);
|
||||
|
||||
if (result.docs.length > 0) {
|
||||
const doc = result.docs[0];
|
||||
let parsedContent = content;
|
||||
try {
|
||||
if (content.trim().startsWith('{')) {
|
||||
parsedContent = JSON.parse(content);
|
||||
}
|
||||
} catch (e) {
|
||||
// Not JSON
|
||||
}
|
||||
|
||||
// Process Images
|
||||
const resolvedImages = ((doc.images as any[]) || [])
|
||||
.map((img) => (typeof img === 'string' ? img : img.url))
|
||||
// Filter images
|
||||
const resolvedImages = ((data.images as any[]) || [])
|
||||
.map((img) => (typeof img === 'string' ? img : img?.url))
|
||||
.filter(Boolean);
|
||||
|
||||
if (resolvedImages.length === 0) return null;
|
||||
|
||||
return {
|
||||
slug: doc.slug,
|
||||
slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
sku: doc.sku,
|
||||
description: doc.description,
|
||||
categories: Array.isArray(doc.categories) ? doc.categories.map((c: any) => c.category) : [],
|
||||
title: data.title || '',
|
||||
sku: data.sku || '',
|
||||
description: data.description || '',
|
||||
categories: Array.isArray(data.categories)
|
||||
? data.categories.map((c: any) => c?.category || c)
|
||||
: [],
|
||||
images: resolvedImages,
|
||||
focalX: data.focalX || 50,
|
||||
focalY: data.focalY || 50,
|
||||
},
|
||||
content: parsedContent,
|
||||
application: data.application,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function getProductBySlug(slug: string, locale: string): Promise<ProductData | null> {
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
const fileSlug = await mapSlugToFileSlug(slug, locale);
|
||||
|
||||
const result = await payload.find({
|
||||
collection: 'products',
|
||||
where: {
|
||||
and: [
|
||||
{ slug: { equals: fileSlug } },
|
||||
...(!config.showDrafts ? [{ _status: { equals: 'published' } }] : []),
|
||||
],
|
||||
},
|
||||
locale: locale as any,
|
||||
depth: 1,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
if (result.docs.length > 0) {
|
||||
const doc = result.docs[0];
|
||||
|
||||
// Map Images correctly from resolved Media docs
|
||||
const resolvedImages = ((doc.images as any[]) || [])
|
||||
.map((img) => (typeof img === 'string' ? img : img.url))
|
||||
.filter(Boolean);
|
||||
|
||||
if (resolvedImages.length === 0) return null;
|
||||
|
||||
return {
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
sku: doc.sku,
|
||||
description: doc.description,
|
||||
categories: Array.isArray(doc.categories)
|
||||
? doc.categories.map((c: any) => c.category)
|
||||
: [],
|
||||
images: resolvedImages,
|
||||
focalX:
|
||||
Array.isArray(doc.images) && doc.images.length > 0 && typeof doc.images[0] === 'object'
|
||||
? doc.images[0].focalX
|
||||
: 50,
|
||||
focalY:
|
||||
Array.isArray(doc.images) && doc.images.length > 0 && typeof doc.images[0] === 'object'
|
||||
? doc.images[0].focalY
|
||||
: 50,
|
||||
},
|
||||
content: doc.content,
|
||||
application: doc.application,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getProductBySlug failed for ${slug}:`, error);
|
||||
console.error(`getProductBySlug failed for ${slug}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllProductSlugs(locale: string): Promise<string[]> {
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
const result = await payload.find({
|
||||
collection: 'products',
|
||||
where: {
|
||||
...(!config.showDrafts ? { _status: { equals: 'published' } } : {}),
|
||||
},
|
||||
locale: locale as any,
|
||||
pagination: false,
|
||||
});
|
||||
|
||||
return result.docs.map((doc) => doc.slug);
|
||||
const dir = path.join(process.cwd(), 'content', 'products');
|
||||
const files = await fs.readdir(dir);
|
||||
return files.filter((f) => f.endsWith('.mdx')).map((f) => f.replace('.mdx', ''));
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getAllProductSlugs failed for ${locale}:`, error);
|
||||
console.error(`getAllProductSlugs failed for ${locale}:`, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllProducts(locale: string): Promise<ProductData[]> {
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
|
||||
const selectFields = {
|
||||
title: true,
|
||||
slug: true,
|
||||
sku: true,
|
||||
description: true,
|
||||
categories: true,
|
||||
images: true,
|
||||
} as const;
|
||||
|
||||
const result = await payload.find({
|
||||
collection: 'products',
|
||||
where: {
|
||||
...(!config.showDrafts ? { _status: { equals: 'published' } } : {}),
|
||||
},
|
||||
locale: locale as any,
|
||||
depth: 1,
|
||||
pagination: false,
|
||||
select: selectFields,
|
||||
});
|
||||
|
||||
console.log(`[Payload] getAllProducts for ${locale}: Found ${result.docs.length} docs`);
|
||||
|
||||
let products: ProductData[] = result.docs.map((doc) => {
|
||||
const resolvedImages = ((doc.images as any[]) || [])
|
||||
.map((img) => (typeof img === 'string' ? img : img.url))
|
||||
.filter(Boolean) as string[];
|
||||
|
||||
const plainCategories = Array.isArray(doc.categories)
|
||||
? doc.categories.map((c: any) => String(c.category))
|
||||
: [];
|
||||
|
||||
return {
|
||||
slug: String(doc.slug),
|
||||
frontmatter: {
|
||||
title: String(doc.title),
|
||||
sku: doc.sku ? String(doc.sku) : '',
|
||||
description: doc.description ? String(doc.description) : '',
|
||||
categories: plainCategories,
|
||||
images: resolvedImages,
|
||||
focalX:
|
||||
Array.isArray(doc.images) && doc.images.length > 0 && typeof doc.images[0] === 'object'
|
||||
? doc.images[0].focalX
|
||||
: 50,
|
||||
focalY:
|
||||
Array.isArray(doc.images) && doc.images.length > 0 && typeof doc.images[0] === 'object'
|
||||
? doc.images[0].focalY
|
||||
: 50,
|
||||
},
|
||||
content: null,
|
||||
application: null,
|
||||
};
|
||||
});
|
||||
|
||||
// Filter out products with 0 images (data integrity check to prevent 404s)
|
||||
products = products.filter((p) => p.frontmatter.images && p.frontmatter.images.length > 0);
|
||||
|
||||
return products;
|
||||
const slugs = await getAllProductSlugs(locale);
|
||||
const products = await Promise.all(slugs.map((slug) => getProductBySlug(slug, locale)));
|
||||
return products.filter(
|
||||
(p): p is ProductData =>
|
||||
p !== null && p.frontmatter.images && p.frontmatter.images.length > 0,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getAllProducts failed for ${locale}:`, error);
|
||||
console.error(`getAllProducts failed for ${locale}:`, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,14 +17,6 @@ export class PinoLoggerService implements LoggerService {
|
||||
this.logger = pino({
|
||||
name: name || 'app',
|
||||
level: config.logging.level,
|
||||
transport: useTransport
|
||||
? {
|
||||
target: 'pino-pretty',
|
||||
options: {
|
||||
colorize: true,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user