Members-only posts on a Fumadocs blog
Fumadocs has no private pages, but its typed posts make them a small job. How 01orbital keeps some posts for license holders, and keeps them out of every public list.
Audrey KlammerFumadocsNext.js
Some posts on this blog are for people who bought a 01orbital license. I assumed Fumadocs would have a switch for that. It doesn't: it has no notion of private pages. What it does have is a typed description of every post, and that turns out to be most of the work.
Mark the post
Fumadocs MDX lets a collection describe its posts with a schema (collections). One more field says who a post is for:
schema: pageSchema.extend({
date: z.coerce.date().optional(),
draft: z.boolean().default(false),
access: z.enum(["public", "members"]).default("public"),
}),A post opts in at the top of its file:
---
title: A members post
description: A short line anyone may read.
access: members
---
The default is public, so every existing post stays as it was.
Keep it out of every public list
This is the part that matters most. A members post has to be missing from
everything a stranger or a crawler can read: the blog's list, the sitemap,
the RSS feed and llms.txt.
The safest way is to have all of those read one function, and to filter there:
const isMembersOnly = (post: Post) => post.data.access === "members"
// The sitemap, the feed and llms.txt all read this, so none of them can
// list a members post.
export function getPublishedPosts() {
return blogSource
.getPages()
.filter(isPublished)
.filter((post) => !isMembersOnly(post))
}
export function getMemberPost(slug: string) {
const post = blogSource.getPage([slug])
return post && isMembersOnly(post) ? post : undefined
}Fumadocs' loader() hands you the posts as plain data
(source API), so a filter is
all it takes.
Give it its own address
Members posts live under /blog/members/<name>, apart from the public ones.
That keeps the public post page fully static, and puts every members post
under one path, which the sign-in cookie below uses.
Decide on the server, per visit
The members page reads the visitor's cookie, checks their license, and only then renders the post:
import { notFound } from "next/navigation"
import { getMemberPost } from "@/lib/blog"
import { getMember } from "@/lib/member"
export const dynamic = "force-dynamic"
export async function generateMetadata({ params }) {
const post = getMemberPost((await params).slug)
return post
? {
title: post.data.title,
description: post.data.description,
// A crawler has no license.
robots: { index: false, follow: false },
}
: {}
}
export default async function MembersPostPage({ params }) {
const post = getMemberPost((await params).slug)
if (!post) notFound()
const Body = post.data.body
return (await getMember()) ? (
<article>
<h1>{post.data.title}</h1>
<Body />
</article>
) : (
<MembersWall title={post.data.title} />
)
}Reading a cookie already makes a Next.js page render per request
(cookies());
force-dynamic
(route segment config)
says so out loud, so nobody later turns the page static by accident. A
static members page would bake one visitor's answer into a file everyone
gets.
Because the post's body is rendered only on the server, and only in the member branch, someone without a license gets the title, the short description and a locked notice. None of the body reaches their browser.
The cookie
Buyers already keep their license token in the browser. The server can't read that, so a small route checks the token once and sets a cookie the members page can read (Set-Cookie):
;(await cookies()).set("member", token, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
path: "/blog/members",
maxAge: 60 * 60 * 24 * 365,
})httpOnlykeeps it away from scripts.pathmeans the browser sends it only to members pages, not with every request the site makes.- The members page checks the license on every visit, so a revoked license stops working at once, however long the cookie has left.
Check that nothing leaks
Before trusting it, I put a line no one could mistake in a test post and
looked for it everywhere a stranger could look: the public post address, the
members address without a license, the feed, llms.txt and the site's
JavaScript files. It turned up only behind a license.
That check is worth keeping. The easy mistake is not in the members page at all. It is a new list somewhere, a feed or a search index, that reads every post instead of the filtered ones.
What it took
One field in the schema, one filter that every public list reads, one page that decides per visit, and one cookie. Fumadocs never needed to know any of it, which is the nice part: the posts are still plain files, and Fumadocs Studio edits a members post like any other.