@@ -8,11 +8,10 @@ import { read } from 'gray-matter'
88import { safeParse } from 'valibot'
99import { BlogPostSchema , type BlogPostFrontmatter } from '../shared/schemas/blog'
1010import { globSync } from 'tinyglobby'
11- import { isProduction } from '../config/env'
1211
1312/**
1413 * Scans the blog directory for .md files and extracts validated frontmatter.
15- * Returns only non-draft posts sorted by date descending.
14+ * Returns all posts (including drafts) sorted by date descending.
1615 */
1716function loadBlogPosts ( blogDir : string ) : BlogPostFrontmatter [ ] {
1817 const files : string [ ] = globSync ( join ( blogDir , '*.md' ) )
@@ -35,8 +34,6 @@ function loadBlogPosts(blogDir: string): BlogPostFrontmatter[] {
3534 const result = safeParse ( BlogPostSchema , frontmatter )
3635 if ( ! result . success ) continue
3736
38- if ( result . output . draft ) continue
39-
4037 posts . push ( result . output )
4138 }
4239
@@ -95,25 +92,15 @@ export default defineNuxtModule({
9592
9693 nuxt . options . alias [ '#blog/posts' ] = join ( nuxt . options . buildDir , 'blog/posts' )
9794
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- }
95+ // Add X-Robots-Tag header for draft posts to prevent indexing
96+ const posts = loadBlogPosts ( blogDir )
97+ for ( const post of posts ) {
98+ if ( post . draft ) {
99+ nuxt . options . routeRules ||= { }
100+ nuxt . options . routeRules [ `/blog/${ post . slug } ` ] = {
101+ headers : { 'X-Robots-Tag' : 'noindex, nofollow' } ,
115102 }
116- } )
103+ }
117104 }
118105 } ,
119106} )
0 commit comments