fix(mdx): support recursive product file searching for OG images and routing
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 7s
Build & Deploy / 🧪 QA (push) Failing after 45s
Build & Deploy / 🏗️ Build (push) Has been skipped
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Smoke Test (push) Has been skipped
Build & Deploy / ⚡ Lighthouse (push) Has been skipped
Build & Deploy / ♿ WCAG (push) Has been skipped
Build & Deploy / 📸 Visual Diff (push) Has been skipped
Build & Deploy / 🛡️ Quality Gates (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 31s

This commit is contained in:
2026-02-22 11:22:35 +01:00
parent 1a39e9c0e4
commit a55680ed41

View File

@@ -26,42 +26,43 @@ export async function getProductMetadata(
const fileSlug = await mapSlugToFileSlug(slug, locale); const fileSlug = await mapSlugToFileSlug(slug, locale);
const productsDir = path.join(process.cwd(), 'data', 'products', locale); const productsDir = path.join(process.cwd(), 'data', 'products', locale);
// Try exact slug first if (!fs.existsSync(productsDir)) return null;
let filePath = path.join(productsDir, `${fileSlug}.mdx`);
if (!fs.existsSync(filePath)) { // Recursive search for the file
// Try with -2 suffix (common in the dumped files) const findFile = (dir: string): string | null => {
filePath = path.join(productsDir, `${fileSlug}-2.mdx`); const files = fs.readdirSync(dir);
} for (const file of files) {
const fullPath = path.join(dir, file);
if (!fs.existsSync(filePath)) { const stat = fs.statSync(fullPath);
// Fallback to English if locale is not 'en' if (stat.isDirectory()) {
if (locale !== 'en') { const found = findFile(fullPath);
const enProductsDir = path.join(process.cwd(), 'data', 'products', 'en'); if (found) return found;
let enFilePath = path.join(enProductsDir, `${fileSlug}.mdx`); } else if (file === `${fileSlug}.mdx` || file === `${fileSlug}-2.mdx`) {
if (!fs.existsSync(enFilePath)) { return fullPath;
enFilePath = path.join(enProductsDir, `${fileSlug}-2.mdx`);
}
if (fs.existsSync(enFilePath)) {
const fileContent = fs.readFileSync(enFilePath, 'utf8');
const { data } = matter(fileContent);
return {
slug: fileSlug,
frontmatter: {
...data,
isFallback: true,
} as ProductFrontmatter & { isFallback?: boolean },
};
} }
} }
} else { return null;
};
let filePath = findFile(productsDir);
if (!filePath && locale !== 'en') {
// Fallback to English
const enProductsDir = path.join(process.cwd(), 'data', 'products', 'en');
if (fs.existsSync(enProductsDir)) {
filePath = findFile(enProductsDir);
}
}
if (filePath && fs.existsSync(filePath)) {
const fileContent = fs.readFileSync(filePath, 'utf8'); const fileContent = fs.readFileSync(filePath, 'utf8');
const { data } = matter(fileContent); const { data } = matter(fileContent);
return { return {
slug: fileSlug, slug: fileSlug,
frontmatter: data as ProductFrontmatter, frontmatter: {
...data,
isFallback: filePath.includes('/en/'),
} as any,
}; };
} }
@@ -73,68 +74,83 @@ export async function getProductBySlug(slug: string, locale: string): Promise<Pr
const fileSlug = await mapSlugToFileSlug(slug, locale); const fileSlug = await mapSlugToFileSlug(slug, locale);
const productsDir = path.join(process.cwd(), 'data', 'products', locale); const productsDir = path.join(process.cwd(), 'data', 'products', locale);
// Try exact slug first if (!fs.existsSync(productsDir)) return null;
let filePath = path.join(productsDir, `${fileSlug}.mdx`);
if (!fs.existsSync(filePath)) { // Recursive search for the file
// Try with -2 suffix (common in the dumped files) const findFile = (dir: string): string | null => {
filePath = path.join(productsDir, `${fileSlug}-2.mdx`); const files = fs.readdirSync(dir);
} for (const file of files) {
const fullPath = path.join(dir, file);
let product: ProductMdx | null = null; const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
if (!fs.existsSync(filePath)) { const found = findFile(fullPath);
// Fallback to English if locale is not 'en' if (found) return found;
if (locale !== 'en') { } else if (file === `${fileSlug}.mdx` || file === `${fileSlug}-2.mdx`) {
const enProductsDir = path.join(process.cwd(), 'data', 'products', 'en'); return fullPath;
let enFilePath = path.join(enProductsDir, `${fileSlug}.mdx`);
if (!fs.existsSync(enFilePath)) {
enFilePath = path.join(enProductsDir, `${fileSlug}-2.mdx`);
}
if (fs.existsSync(enFilePath)) {
const fileContent = fs.readFileSync(enFilePath, 'utf8');
const { data, content } = matter(fileContent);
product = {
slug: fileSlug,
frontmatter: {
...data,
isFallback: true,
} as ProductFrontmatter & { isFallback?: boolean },
content,
};
} }
} }
} else { return null;
};
let filePath = findFile(productsDir);
let isFallback = false;
if (!filePath && locale !== 'en') {
// Fallback to English
const enProductsDir = path.join(process.cwd(), 'data', 'products', 'en');
if (fs.existsSync(enProductsDir)) {
filePath = findFile(enProductsDir);
if (filePath) isFallback = true;
}
}
if (filePath && fs.existsSync(filePath)) {
const fileContent = fs.readFileSync(filePath, 'utf8'); const fileContent = fs.readFileSync(filePath, 'utf8');
const { data, content } = matter(fileContent); const { data, content } = matter(fileContent);
const product = {
product = {
slug: fileSlug, slug: fileSlug,
frontmatter: data as ProductFrontmatter, frontmatter: {
...data,
isFallback,
} as any,
content, content,
}; };
}
// Filter out products without images // Filter out products without images
if ( if (
product && !product.frontmatter.images ||
(!product.frontmatter.images ||
product.frontmatter.images.length === 0 || product.frontmatter.images.length === 0 ||
!product.frontmatter.images[0]) !product.frontmatter.images[0]
) { ) {
return null; return null;
}
return product;
} }
return product; return null;
} }
export async function getAllProductSlugs(locale: string): Promise<string[]> { export async function getAllProductSlugs(locale: string): Promise<string[]> {
const productsDir = path.join(process.cwd(), 'data', 'products', locale); const productsDir = path.join(process.cwd(), 'data', 'products', locale);
if (!fs.existsSync(productsDir)) return []; if (!fs.existsSync(productsDir)) return [];
const files = fs.readdirSync(productsDir); const slugs: string[] = [];
return files.filter((file) => file.endsWith('.mdx')).map((file) => file.replace(/\.mdx$/, '')); const walk = (dir: string) => {
const files = fs.readdirSync(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
walk(fullPath);
} else if (file.endsWith('.mdx')) {
slugs.push(file.replace(/\.mdx$/, ''));
}
}
};
walk(productsDir);
return slugs;
} }
export async function getAllProducts(locale: string): Promise<ProductMdx[]> { export async function getAllProducts(locale: string): Promise<ProductMdx[]> {