Back to blog

How to Add an RSS Feed to a Next.js App Router Blog

Generate /rss.xml from your blog’s published posts, then check the deployed feed for a newly published article and the absence of drafts.

RSS feed for published posts in a Next.js App Router blog

Add app/rss.xml/route.ts, have its GET handler read the same published-post source as your blog, and return RSS XML with absolute article links. Then deploy it and check the public feed against a post that actually loads. Next.js documents this route location for a custom RSS endpoint; the feed and its post selection are yours to implement.

Use the blog’s published-post source

Before writing XML, identify the function or query that supplies your article pages. The feed should use that source rather than a second list of slugs you must update by hand. Decide which record means “published”: for the illustrative implementation below, a post needs status: 'published' and a valid publishedAt date that is not in the future. Adapt those field names and that rule to your own blog. You also need each post’s slug, title and excerpt, plus your production site origin and the URL pattern used by its article pages.

The example assumes one slug segment per article and URLs under /blog/. If your pages use a different path, locale prefix or nested slugs, change the URL construction to match the links your blog actually serves. Use the article’s stable public URL as both its feed link and identifier; the RSS specification allows an item’s guid to be a permalink when marked as such.

Create the RSS Route Handler

Create app/rss.xml/route.ts and replace the illustrative getPosts import with your blog’s post loader. The assumed loader returns records with slug, title, excerpt, status and publishedAt; it must be usable by a server-side Route Handler. Next.js supports a GET export in an App Router Route Handler and XML responses. The RSS specification requires channel title, link and description; this handler also supplies item titles, links, descriptions, unique permalink GUIDs and publication dates.

Here is a complete illustrative handler; change the import, site origin, blog path and field mapping before using it: import { getPosts } from '@/lib/posts'; type Post = { slug: string; title: string; excerpt: string; status: 'draft' | 'published'; publishedAt: string | null }; const site = 'https://example.com'; const xml = (value: string) => value.replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&apos;' })[c]!); export async function GET() { const posts: Post[] = await getPosts(); const items = posts.filter((p) => p.status === 'published' && Number.isFinite(Date.parse(p.publishedAt ?? '')) && Date.parse(p.publishedAt!) <= Date.now()).map((p) => { const url = new URL('/blog/' + encodeURIComponent(p.slug), site).toString(); return '<item><title>' + xml(p.title) + '</title><link>' + xml(url) + '</link><guid isPermaLink="true">' + xml(url) + '</guid><description>' + xml(p.excerpt) + '</description><pubDate>' + new Date(p.publishedAt!).toUTCString() + '</pubDate></item>'; }).join(''); const body = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>Example Blog</title><link>' + xml(site) + '</link><description>Articles from Example Blog</description>' + items + '</channel></rss>'; return new Response(body, { headers: { 'Content-Type': 'application/rss+xml; charset=utf-8' } }); }

The xml helper escapes text inserted into markup, including titles and excerpts supplied by your post source. Next.js explicitly advises sanitizing input used to generate markup. Keep the escaped text distinct from the URL construction: encodeURIComponent handles the illustrative single-segment slug, while new URL supplies an absolute link.

Verify a newly published item on the live site

For a fictional example, suppose your post source now contains slug: 'new-guide', title: 'New Guide', excerpt: 'A short introduction.', status: 'published' and publishedAt: '2026-11-16T12:00:00Z'. With the example origin and path, its item should contain <link>https://example.com/blog/new-guide</link>, the same absolute URL in <guid isPermaLink="true">, and <pubDate>Mon, 16 Nov 2026 12:00:00 GMT</pubDate>. A draft record should produce no item.

Release the feed handler with your site. If the article and feed cannot be released together, publish the article destination before pointing readers to it. First open https://example.com/blog/new-guide on the deployed site; then open https://example.com/rss.xml and inspect its response. Confirm it returns XML with an RSS channel, contains one item with that exact article link and publication date, and contains no draft-only URL. Try an XML parser or feed reader as a second check: an XML-looking page in a browser is not enough if the markup is malformed.

This distinction matters when using assisted article publishing. Ranks documents publishing support for Next.js App Router sites using TypeScript and distinguishes a generated draft from a deployed public article. Ranks can supply the article; your site’s handler selects it for the feed when the post meets your published rule. Check separately that its public URL loads.

Match feed updates to your deployment mode

If the article is live but absent from the feed, check whether the deployed handler is reading the updated post source before editing the XML template. Next.js says Route Handlers are not cached by default, but GET handlers can opt into caching; Cache Components mode has additional prerendering behavior. Account for your data loader’s caching as well. A handler reading live data and a feed generated from build-time files will not necessarily update at the same moment.

For a static-export site, plan to generate the feed from posts available at build time and rebuild when a new post is published. Next.js limits export-mode Route Handlers to GET with dynamic set to force-static; adapt the example for that mode instead of expecting its feed to read newly added content at request time. After the rebuild, check the deployed XML again.

An RSS item is a distribution check, not a sitemap entry or proof that the article page works. If the page does not load, use the Next.js blog post 404 guide. If your separate sitemap omits the live URL, use the new-post sitemap guide.

Sources

  1. How to use Next.js as a backend for your frontendnextjs.org
  2. Route Handlersnextjs.org
  3. RSS 2.0 Specificationrssboard.org
  4. Ranks public product referenceranks.so

Grow your SaaS traffic on autopilot

One vetted backlink submission and one targeted blog article per day, on autopilot. Track your growth, all in one dashboard.

Try Ranks