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 productsDir = path.join(process.cwd(), 'data', 'products', locale);
// Try exact slug first
let filePath = path.join(productsDir, `${fileSlug}.mdx`);
if (!fs.existsSync(productsDir)) return null;
if (!fs.existsSync(filePath)) {
// Try with -2 suffix (common in the dumped files)
filePath = path.join(productsDir, `${fileSlug}-2.mdx`);
}
if (!fs.existsSync(filePath)) {
// Fallback to English if locale is not 'en'
if (locale !== 'en') {
const enProductsDir = path.join(process.cwd(), 'data', 'products', 'en');
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 } = matter(fileContent);
return {
slug: fileSlug,
frontmatter: {
...data,
isFallback: true,
} as ProductFrontmatter & { isFallback?: boolean },
};
// Recursive search for the file
const findFile = (dir: string): string | null => {
const files = fs.readdirSync(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
const found = findFile(fullPath);
if (found) return found;
} else if (file === `${fileSlug}.mdx` || file === `${fileSlug}-2.mdx`) {
return fullPath;
}
}
} 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 { data } = matter(fileContent);
return {
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 productsDir = path.join(process.cwd(), 'data', 'products', locale);
// Try exact slug first
let filePath = path.join(productsDir, `${fileSlug}.mdx`);
if (!fs.existsSync(productsDir)) return null;
if (!fs.existsSync(filePath)) {
// Try with -2 suffix (common in the dumped files)
filePath = path.join(productsDir, `${fileSlug}-2.mdx`);
}
let product: ProductMdx | null = null;
if (!fs.existsSync(filePath)) {
// Fallback to English if locale is not 'en'
if (locale !== 'en') {
const enProductsDir = path.join(process.cwd(), 'data', 'products', 'en');
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,
};
// Recursive search for the file
const findFile = (dir: string): string | null => {
const files = fs.readdirSync(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
const found = findFile(fullPath);
if (found) return found;
} else if (file === `${fileSlug}.mdx` || file === `${fileSlug}-2.mdx`) {
return fullPath;
}
}
} 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 { data, content } = matter(fileContent);
product = {
const product = {
slug: fileSlug,
frontmatter: data as ProductFrontmatter,
frontmatter: {
...data,
isFallback,
} as any,
content,
};
}
// Filter out products without images
if (
product &&
(!product.frontmatter.images ||
// Filter out products without images
if (
!product.frontmatter.images ||
product.frontmatter.images.length === 0 ||
!product.frontmatter.images[0])
) {
return null;
!product.frontmatter.images[0]
) {
return null;
}
return product;
}
return product;
return null;
}
export async function getAllProductSlugs(locale: string): Promise<string[]> {
const productsDir = path.join(process.cwd(), 'data', 'products', locale);
if (!fs.existsSync(productsDir)) return [];
const files = fs.readdirSync(productsDir);
return files.filter((file) => file.endsWith('.mdx')).map((file) => file.replace(/\.mdx$/, ''));
const slugs: string[] = [];
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[]> {