@@ -12,7 +12,7 @@ import { isProduction } from '../config/env'
1212
1313/**
1414 * Scans the blog directory for .md files and extracts validated frontmatter.
15- * Returns only non-draft posts sorted by date descending.
15+ * Returns all posts (including drafts) sorted by date descending.
1616 */
1717function loadBlogPosts ( blogDir : string ) : BlogPostFrontmatter [ ] {
1818 const files : string [ ] = globSync ( join ( blogDir , '*.md' ) )
@@ -35,8 +35,6 @@ function loadBlogPosts(blogDir: string): BlogPostFrontmatter[] {
3535 const result = safeParse ( BlogPostSchema , frontmatter )
3636 if ( ! result . success ) continue
3737
38- if ( result . output . draft ) continue
39-
4038 posts . push ( result . output )
4139 }
4240
@@ -78,13 +76,13 @@ export default defineNuxtModule({
7876 } ) ,
7977 )
8078
81- // Expose frontmatter for published posts to avoid bundling the full content
82- // of all posts in `/blog` page.
79+ // Expose frontmatter for the `/blog` listing page.
80+ const showDrafts = nuxt . options . dev || ! isProduction
8381 addTemplate ( {
8482 filename : 'blog/posts.ts' ,
8583 write : true ,
8684 getContents : ( ) => {
87- const posts = loadBlogPosts ( blogDir )
85+ const posts = loadBlogPosts ( blogDir ) . filter ( p => showDrafts || ! p . draft )
8886 return [
8987 `import type { BlogPostFrontmatter } from '#shared/schemas/blog'` ,
9088 `` ,
@@ -95,25 +93,15 @@ export default defineNuxtModule({
9593
9694 nuxt . options . alias [ '#blog/posts' ] = join ( nuxt . options . buildDir , 'blog/posts' )
9795
98- // In production, remove page routes for draft posts
99- if ( ! nuxt . options . dev && isProduction ) {
100- const publishedPosts = loadBlogPosts ( blogDir )
101- const publishedSlugs = new Set ( publishedPosts . map ( p => p . slug ) )
102-
103- nuxt . hook ( 'pages:extend' , pages => {
104- // Walk the pages tree and remove draft blog post pages
105- for ( let i = pages . length - 1 ; i >= 0 ; i -- ) {
106- const page = pages [ i ] !
107- // Blog post pages are at /blog/<slug> — the file is blog/<slug>.md
108- if ( page . file ?. endsWith ( '.md' ) && page . file ?. includes ( '/blog/' ) ) {
109- // Extract the slug from the filename
110- const filename = page . file . split ( '/' ) . pop ( ) ?. replace ( '.md' , '' )
111- if ( filename && filename !== 'index' && ! publishedSlugs . has ( filename ) ) {
112- pages . splice ( i , 1 )
113- }
114- }
96+ // Add X-Robots-Tag header for draft posts to prevent indexing
97+ const posts = loadBlogPosts ( blogDir )
98+ for ( const post of posts ) {
99+ if ( post . draft ) {
100+ nuxt . options . routeRules ||= { }
101+ nuxt . options . routeRules [ `/blog/${ post . slug } ` ] = {
102+ headers : { 'X-Robots-Tag' : 'noindex, nofollow' } ,
115103 }
116- } )
104+ }
117105 }
118106 } ,
119107} )
0 commit comments