Next.js 16 RSS feed: why your rss.xml route builds as dynamic, and how to fix it
I added an RSS feed to a fresh Next.js 16 project. The build passed and the feed worked, but the route table said this: └ ƒ /rss.xml ƒ means dynamic. Every feed reader poll would read the posts from disk and reb
I added an RSS feed to a fresh Next.js 16 project. The build passed and the feed worked, but the route table said this:
└ ƒ /rss.xml
ƒ means dynamic. Every feed reader poll would read the posts from disk and rebuild the XML, and Next.js gave no warning about it.
This post builds a feed with an App Router route handler, explains why it ends up dynamic, and shows the fix for a server deployment and for a static export. Everything here was tested on Next.js 16.4.6 and React 19.3.0 with a project from create-next-app, which turns on cacheComponents: true in the generated next.config.ts.
1. Load your posts
My posts are Markdown files in content/posts/ with front matter. I parse them with gray-matter:
---
title: "Static export & GitHub Pages: what broke"
date: "2026-11-20"
summary: "basePath, images and a 404 on refresh. Notes from <deploying> a Next.js 16 site."
---
Quote the date. Unquoted, YAML turns 2026-11-20 into a Date object, and String() of that gives you something like Fri Nov 20 2026 07:00:00 GMT+0700 (Western Indonesia Time) instead of 2026-11-20.
// src/lib/posts.ts
import { readdir, readFile } from "node:fs/promises";
import path from "node:path";
import matter from "gray-matter";
export type Post = {
slug: string;
title: string;
summary: string;
date: string; // "YYYY-MM-DD"
};
const POSTS_DIR = path.join(process.cwd(), "content/posts");
export async function getPosts(): Promise<Post[]> {
const files = (await readdir(POSTS_DIR)).filter((f) => f.endsWith(".md"));
const posts = await Promise.all(
files.map(async (file) => {
const { data } = matter(await readFile(path.join(POSTS_DIR, file), "utf8"));
return {
slug: file.replace(/\.md$/, ""),
title: String(data.title),
summary: String(data.summary),
date: String(data.date),
};
}),
);
return posts.sort((a, b) => b.date.localeCompare(a.date));
}
// src/lib/site.ts
export const SITE_URL = "https://example.com";
export const SITE_NAME = "Example Blog";
2. The feed route
A folder named rss.xml with a route.ts inside serves /rss.xml. The Next.js route handler docs use app/rss.xml/route.ts as their own example of a non-UI response.
// src/app/rss.xml/route.ts
import { getPosts } from "@/lib/posts";
import { SITE_NAME, SITE_URL } from "@/lib/site";
function escapeXml(value: string) {
return value
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
async function buildFeed() {
const posts = await getPosts();
const items = posts
.map((post) => {
const url = `${SITE_URL}/blog/${post.slug}`;
return ` <item>
<title>${escapeXml(post.title)}</title>
<link>${url}</link>
<guid isPermaLink="true">${url}</guid>
<description>${escapeXml(post.summary)}</description>
<pubDate>${new Date(post.date).toUTCString()}</pubDate>
</item>`;
})
.join("\n");
return `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${escapeXml(SITE_NAME)}</title>
<link>${SITE_URL}</link>
<description>Notes on shipping Next.js sites</description>
<language>en</language>
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
<atom:link href="${SITE_URL}/rss.xml" rel="self" type="application/rss+xml" />
${items}
</channel>
</rss>`;
}
export async function GET() {
return new Response(await buildFeed(), {
headers: { "Content-Type": "application/rss+xml; charset=utf-8" },
});
}
Three details that matter:
-
Escape titles and summaries. One
&or<in a title makes the whole feed invalid XML. With the escape, my test title becameStatic export & GitHub Pages: what broke. -
Dates. The RSS 2.0 spec wants RFC 822 dates.
toUTCString()givesFri, 20 Nov 2026 00:00:00 GMT, which matches the spec's own examples. -
atom:link rel="self". It's optional, but the RSS Best Practices Profile recommends it so the feed carries its own URL.
3. Why the build marks it dynamic
Here is the route table from npx next build:
Route (app)
┌ ○ /
├ ○ /_not-found
└ ƒ /rss.xml
The Route Handlers docs explain it. With Cache Components on, GET handlers "run at request time by default", and prerendering stops when the handler touches "async file system operations" or "non-deterministic operations".
This route does both: readdir and readFile in getPosts(), and new Date() for lastBuildDate. I tested them one at a time, and either one alone keeps the route at ƒ.
You can see it with next start. Two requests a second apart return two different build dates, and there's no Cache-Control header:
<lastBuildDate>Wed, 25 Nov 2026 07:45:30 GMT</lastBuildDate>
<lastBuildDate>Wed, 25 Nov 2026 07:45:31 GMT</lastBuildDate>
4. Fix for a server deployment: "use cache"
The docs say use cache can't go directly in the route handler body, so you put it in a helper. buildFeed() already is one. Add three lines at the top:
// src/app/rss.xml/route.ts
import { cacheLife, cacheTag } from "next/cache";
async function buildFeed() {
"use cache";
cacheLife("days");
cacheTag("posts"); // used by the webhook in section 5
const posts = await getPosts();
// ...the rest stays the same
}
The build now prerenders it:
Route (app) Revalidate Expire
┌ ○ /
├ ○ /_not-found
├ ƒ /api/revalidate
└ ○ /rss.xml 1d 1w
And the response comes from the cache:
x-nextjs-cache: HIT
cache-control: s-maxage=86400, stale-while-revalidate=518400
content-type: application/rss+xml; charset=utf-8
The cacheLife docs list days for "blog posts, articles": revalidate after 1 day, expire after 1 week. The new Date() inside the cached function is fine now, because it runs only when the cache entry is created.
Two things that put you back at ƒ or break the build:
export const dynamic = "force-static". It's the old answer, and it fails when Cache Components is on:
Error: Route segment config "dynamic" is not compatible with `nextConfig.cacheComponents`. Please remove it.
Reading request.url to build absolute links. I added new URL(request.url).origin to GET, and the route went back to ƒ, even with the cached helper. The docs list request properties like req.url among the things that stop prerendering. Use a constant like SITE_URL instead.
5. Refresh the feed when a post comes from a CMS
If your posts live in the repo, a new post means a new build, and the feed updates with it. If they come from a CMS, call revalidateTag from the CMS webhook:
// src/app/api/revalidate/route.ts
import { revalidateTag } from "next/cache";
export async function POST(request: Request) {
if (request.headers.get("x-revalidate-secret") !== process.env.REVALIDATE_SECRET) {
return new Response("Unauthorized", { status: 401 });
}
revalidateTag("posts", "max");
return Response.json({ revalidated: true });
}
In Next.js 16 the second argument is a cache profile (the one-argument form is deprecated), and the docs recommend "max": the next request gets the old feed while a fresh one is built. That's what I saw after calling the webhook:
<lastBuildDate>Wed, 25 Nov 2026 07:45:43 GMT</lastBuildDate> x-nextjs-cache: STALE
<lastBuildDate>Wed, 25 Nov 2026 07:45:52 GMT</lastBuildDate> x-nextjs-cache: HIT
6. Static export (GitHub Pages, Cloudflare Pages)
With output: "export" and Cache Components still on, the cached route fails the build:
Error: export const dynamic = "force-static"/export const revalidate not configured on route "/rss.xml" with "output: export".
Adding force-static then triggers the "not compatible with cacheComponents" error from section 4. So for a static export, turn Cache Components off:
// next.config.ts
const nextConfig: NextConfig = {
output: "export",
// remove cacheComponents and partialPrefetching,
// keep the other options create-next-app generated
};
Then remove the three cache lines from buildFeed() ("use cache" needs Cache Components) and mark the route static, as the static exports guide asks for route handlers:
// src/app/rss.xml/route.ts
export const dynamic = "force-static";
The build writes out/rss.xml. One difference: your Content-Type header isn't saved anywhere. out/ only has the file, so the type comes from your host, usually based on the .xml extension.
7. Let browsers and feed readers find it
Add the feed to the root layout's metadata:
// src/app/layout.tsx
import type { Metadata } from "next";
import { SITE_NAME, SITE_URL } from "@/lib/site";
import "./globals.css";
export const metadata: Metadata = {
metadataBase: new URL(SITE_URL),
title: SITE_NAME,
alternates: {
types: {
"application/rss+xml": [{ url: "/rss.xml", title: SITE_NAME }],
},
},
};
// RootLayout stays the same
That renders this in the <head> of every page:
<link rel="alternate" type="application/rss+xml" href="https://example.com/rss.xml" title="Example Blog"/>
To check the feed itself, I ran it through xmllint --noout (no errors) and Python's feedparser, which read it as rss20 with both items and their dates.
Wrapping up
- Put the feed in
app/rss.xml/route.tsand build the XML in a helper function. - Escape every title and summary, and use
toUTCString()for dates. - Check the route table after
next build.ƒ /rss.xmlmeans it runs on every request. - Server deployment:
"use cache"pluscacheLife("days")in the helper. Don't useforce-static, and don't readrequest.url. - Static export: turn off Cache Components and use
export const dynamic = "force-static". - Add
alternates.typesto your layout metadata so readers can find the feed.
Do you still add RSS feeds to your Next.js sites, or have you dropped them?
I keep launch checks like these as free, MIT-licensed agent skills for Claude Code, Cursor and Codex: npx skills add nandot12/nextjs-launch-skills · GitHub
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.