Skip to content

Commit d3c8f67

Browse files
authored
Merge pull request #695 from thekoc/skip-draft-posts
Prevent drafts from showing on PostLayout
2 parents 9eb8876 + 598623e commit d3c8f67

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

app/blog/[...slug]/page.tsx

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'katex/dist/katex.css'
44
import PageTitle from '@/components/PageTitle'
55
import { components } from '@/components/MDXComponents'
66
import { MDXLayoutRenderer } from 'pliny/mdx-components'
7-
import { sortPosts, coreContent } from 'pliny/utils/contentlayer'
7+
import { sortPosts, coreContent, allCoreContent } from 'pliny/utils/contentlayer'
88
import { allBlogs, allAuthors } from 'contentlayer/generated'
99
import type { Authors, Blog } from 'contentlayer/generated'
1010
import PostSimple from '@/layouts/PostSimple'
@@ -13,7 +13,6 @@ import PostBanner from '@/layouts/PostBanner'
1313
import { Metadata } from 'next'
1414
import siteMetadata from '@/data/siteMetadata'
1515

16-
const isProduction = process.env.NODE_ENV === 'production'
1716
const defaultLayout = 'PostLayout'
1817
const layouts = {
1918
PostSimple,
@@ -82,11 +81,25 @@ export const generateStaticParams = async () => {
8281

8382
export default async function Page({ params }: { params: { slug: string[] } }) {
8483
const slug = decodeURI(params.slug.join('/'))
85-
const sortedPosts = sortPosts(allBlogs) as Blog[]
86-
const postIndex = sortedPosts.findIndex((p) => p.slug === slug)
87-
const prev = coreContent(sortedPosts[postIndex + 1])
88-
const next = coreContent(sortedPosts[postIndex - 1])
89-
const post = sortedPosts.find((p) => p.slug === slug) as Blog
84+
// Filter out drafts in production
85+
const sortedCoreContents = allCoreContent(sortPosts(allBlogs))
86+
const postIndex = sortedCoreContents.findIndex((p) => p.slug === slug)
87+
if (postIndex === -1) {
88+
return (
89+
<div className="mt-24 text-center">
90+
<PageTitle>
91+
Under Construction{' '}
92+
<span role="img" aria-label="roadwork sign">
93+
🚧
94+
</span>
95+
</PageTitle>
96+
</div>
97+
)
98+
}
99+
100+
const prev = sortedCoreContents[postIndex + 1]
101+
const next = sortedCoreContents[postIndex - 1]
102+
const post = allBlogs.find((p) => p.slug === slug) as Blog
90103
const authorList = post?.authors || ['default']
91104
const authorDetails = authorList.map((author) => {
92105
const authorResults = allAuthors.find((p) => p.slug === author)
@@ -105,26 +118,13 @@ export default async function Page({ params }: { params: { slug: string[] } }) {
105118

106119
return (
107120
<>
108-
{isProduction && post && 'draft' in post && post.draft === true ? (
109-
<div className="mt-24 text-center">
110-
<PageTitle>
111-
Under Construction{' '}
112-
<span role="img" aria-label="roadwork sign">
113-
🚧
114-
</span>
115-
</PageTitle>
116-
</div>
117-
) : (
118-
<>
119-
<script
120-
type="application/ld+json"
121-
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
122-
/>
123-
<Layout content={mainContent} authorDetails={authorDetails} next={next} prev={prev}>
124-
<MDXLayoutRenderer code={post.body.code} components={components} toc={post.toc} />
125-
</Layout>
126-
</>
127-
)}
121+
<script
122+
type="application/ld+json"
123+
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
124+
/>
125+
<Layout content={mainContent} authorDetails={authorDetails} next={next} prev={prev}>
126+
<MDXLayoutRenderer code={post.body.code} components={components} toc={post.toc} />
127+
</Layout>
128128
</>
129129
)
130130
}

0 commit comments

Comments
 (0)