feat: migrate Payload CMS to MDX and harden static infrastructure
This commit is contained in:
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,
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user