Ignore a mdx file (from build) in contentlayer based on field

26 Views Asked by At

I'm building a blog using Next.js and contenlayer. I have the following code which display a list of blog posts based on a condition in the "All Posts" page. But this just filters the list in the "All Posts" page - that means if the user manually types a url to a blogpost which exists but doesn't meet the condition, they will still be able to view it.

How can I avoid that?

import { allPosts, type Post } from "contentlayer/generated";

const posts = allPosts.filter((post: Post) => post.published);

For Example, in the above code, the posts variable just removes the posts where the field published: false, but if I have a page called sample-page with field published: false and manually go to a url http://localhost:3000/blog/sample-post - the page still gets rendered.

How can I get a 404 in this case?

0

There are 0 best solutions below