timlrx / tailwind-nextjs-starter-blog

This is a Next.js, Tailwind CSS blogging starter template. Comes out of the box configured with the latest technologies to make technical writing a breeze. Easily configurable and customizable. Perfect as a replacement to existing Jekyll and Hugo individual blogs.
https://tailwind-nextjs-starter-blog.vercel.app/
MIT License
7.43k stars 1.87k forks source link

Blog Posts with 'draft' Status Incorrectly Displayed #877

Closed immortalrover closed 3 months ago

immortalrover commented 4 months ago

Describe the bug Hello, I've encountered an issue where blog posts marked as 'draft' are still appearing on the homepage.

Additional context I found that the type definition for blog post status in .contentlayer/generated/types.d.ts is set to:

export type Blog = {
  /** File path relative to `contentDirPath` */
  _id: string
  _raw: Local.RawDocumentData
  type: 'Blog'
  title: string
  date: IsoDateTimeString
  tags: string[]
  lastmod?: IsoDateTimeString | undefined
  status: 'draft' | 'published'                         /**              This is the initially defined type.           */
  summary?: string | undefined
  images?: any | undefined
  authors?: string[] | undefined
  layout?: string | undefined
  bibliography?: string | undefined
  canonicalUrl?: string | undefined
  /** MDX file body */
  body: MDX 
  readingTime: json
  slug: string
  path: string
  filePath: string
  toc: string
  structuredData: json
}  

However, in the contentlayer.config.ts file, the status is set differently:

export const Blog = defineDocumentType(() => ({
  name: 'Blog',
  filePathPattern: 'blog/**/*.mdx',
  contentType: 'mdx',
  fields: {
    title: { type: 'string', required: true },
    date: { type: 'date', required: true },
    tags: { type: 'list', of: { type: 'string' }, default: [] },
    lastmod: { type: 'date' },
    draft: { type: 'boolean' },                         //          Here, the 'draft' is used as a boolean type.
    summary: { type: 'string' },
    images: { type: 'json' },
    authors: { type: 'list', of: { type: 'string' } },
    layout: { type: 'string' },
    bibliography: { type: 'string' },
    canonicalUrl: { type: 'string' },
  },
  computedFields: {
    ...computedFields,
    structuredData: {
      type: 'json',
      resolve: (doc) => ({
        '@context': 'https://schema.org',
        '@type': 'BlogPosting',
        headline: doc.title,
        datePublished: doc.date,
        dateModified: doc.lastmod || doc.date,
        description: doc.summary,
        image: doc.images ? doc.images[0] : siteMetadata.socialBanner,
        url: `${siteMetadata.siteUrl}/${doc._raw.flattenedPath}`,
      }),
    },
  },
}))

To resolve this issue temporarily, I added a .filter function to each sortPost function in the part where allPosts are processed, specifically filtering out posts marked as 'draft'.

Could someone please help clarify if this is an intended behavior or a configuration mismatch? Additionally, if there are recommended practices for handling post statuses in this framework, I would appreciate guidance on the same.

timlrx commented 3 months ago

Sorry for the late reply, but there's no status field in Blog as you correctly pointed out. It's just a draft boolean variable. To hide it set draft: true in the frontmatter.

You could try deleting the .contentlayer generated folder and re-generating it again.

immortalrover commented 3 months ago

Thank you for your response. I have found a solution to the problem, and it has been resolved. I appreciate your assistance.