mirror of
https://github.com/mmahdium/portfolio.git
synced 2025-12-20 09:23:54 +01:00
fix(prerender): Optimize blog route prerendering strategy
- Refactor prerender plugin to use content:file:afterParse hook - Dynamically collect blog post routes during file parsing - Remove complex locale-based post fetching logic - Add conditional prerendering to prevent unnecessary processing - Improve logging for prerender route generation - Simplify route collection and prerendering process Improves performance and reliability of static site generation for blog content.
This commit is contained in:
@@ -1,37 +1,22 @@
|
||||
export default defineNitroPlugin((nitroApp) => {
|
||||
nitroApp.hooks.hook('prerender:routes' as any, async (ctx: any) => {
|
||||
try {
|
||||
// Import serverQueryContent dynamically
|
||||
const { serverQueryContent } = await import('#content/server' as any)
|
||||
export default defineNitroPlugin(async (nitroApp) => {
|
||||
if (!import.meta.prerender) {
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch all blog posts from both English and Persian locales
|
||||
const locales = ['en', 'fa']
|
||||
const allPosts: any[] = []
|
||||
const routes: string[] = []
|
||||
|
||||
for (const locale of locales) {
|
||||
const posts = await serverQueryContent(nitroApp as any, `${locale}/blog`)
|
||||
.where({ draft: { $ne: true } })
|
||||
.find()
|
||||
|
||||
allPosts.push(...posts)
|
||||
nitroApp.hooks.hook('content:file:afterParse', (file) => {
|
||||
// Collect blog post routes for prerendering
|
||||
if (file._path && !file.draft) {
|
||||
if (file._path.startsWith('/en/blog/') || file._path.startsWith('/fa/blog/')) {
|
||||
routes.push(file._path)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (allPosts.length === 0) {
|
||||
console.log('No blog posts found for prerendering')
|
||||
return
|
||||
}
|
||||
|
||||
// Add routes for each published post
|
||||
for (const post of allPosts) {
|
||||
if (post._path) {
|
||||
ctx.routes.add(post._path)
|
||||
console.log(`Added route for prerendering: ${post._path}`)
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Total blog routes added for prerendering: ${allPosts.length}`)
|
||||
} catch (error) {
|
||||
console.error('Error generating prerender routes:', error)
|
||||
nitroApp.hooks.hook('prerender:generate', async () => {
|
||||
for (const route of routes) {
|
||||
console.log(`Prerendering blog route: ${route}`)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user