fix: pre build static pages
This commit is contained in:
@@ -29,6 +29,13 @@ export async function generateMetadata({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function generateStaticParams() {
|
||||||
|
const pages = await pageSource.getPages();
|
||||||
|
return pages.map((page) => ({
|
||||||
|
pages: page.url.split('/').slice(1),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
export default async function Page({
|
export default async function Page({
|
||||||
params,
|
params,
|
||||||
}: {
|
}: {
|
||||||
|
|||||||
@@ -50,23 +50,37 @@ const __filename = fileURLToPath(import.meta.url);
|
|||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
const contentDir = path.join(__dirname, '../../content/compare');
|
const contentDir = path.join(__dirname, '../../content/compare');
|
||||||
|
|
||||||
const files = fs
|
function loadCompareSource(): CompareData[] {
|
||||||
.readdirSync(contentDir)
|
try {
|
||||||
.filter((file) => file.endsWith('.json'));
|
// Check if directory exists before trying to read it
|
||||||
|
if (!fs.existsSync(contentDir)) {
|
||||||
export const compareSource: CompareData[] = files
|
return [];
|
||||||
.map((file) => {
|
|
||||||
const filePath = path.join(contentDir, file);
|
|
||||||
const fileContents = fs.readFileSync(filePath, 'utf8');
|
|
||||||
try {
|
|
||||||
return JSON.parse(fileContents) as CompareData;
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error parsing compare data for ${file}:`, error);
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.flatMap((item) => (item ? [item] : []))
|
const files = fs
|
||||||
.map((item) => ({
|
.readdirSync(contentDir)
|
||||||
...item,
|
.filter((file) => file.endsWith('.json'));
|
||||||
url: `/compare/${item.slug}`,
|
|
||||||
}));
|
return files
|
||||||
|
.map((file) => {
|
||||||
|
const filePath = path.join(contentDir, file);
|
||||||
|
const fileContents = fs.readFileSync(filePath, 'utf8');
|
||||||
|
try {
|
||||||
|
return JSON.parse(fileContents) as CompareData;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error parsing compare data for ${file}:`, error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.flatMap((item) => (item ? [item] : []))
|
||||||
|
.map((item) => ({
|
||||||
|
...item,
|
||||||
|
url: `/compare/${item.slug}`,
|
||||||
|
}));
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error loading compare source:', error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const compareSource: CompareData[] = loadCompareSource();
|
||||||
|
|||||||
Reference in New Issue
Block a user