Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions modules/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import MarkdownItAnchor from 'markdown-it-anchor'
import { defu } from 'defu'
import { read } from 'gray-matter'
import { array, safeParse } from 'valibot'
import { Feed } from 'feed'
import {
AuthorSchema,
RawBlogPostSchema,
Expand Down Expand Up @@ -159,6 +160,7 @@ export default defineNuxtModule({
const resolver = createResolver(import.meta.url)
const blogDir = resolver.resolve('../app/pages/blog')
const blogImagesDir = resolver.resolve('../public/blog/avatar')
const publicDir = resolver.resolve('../public')
const resolveAvatars = !nuxt.options._prepare

nuxt.options.extensions.push('.md')
Expand Down Expand Up @@ -221,5 +223,52 @@ export default defineNuxtModule({
}
}
}

// Generate content for RSS, Atom and JSON feeds
const feed = new Feed({
title: 'Blog - npmx',
description: 'a fast, modern browser for the npm registry',
id: 'https://npmx.dev/',
link: 'https://npmx.dev/',
language: 'en',
image: 'https://npmx.dev/logo.svg',
favicon: 'https://npmx.dev/favicon.ico',
feedLinks: {
rss: 'https://npmx.dev/rss.xml',
atom: 'https://npmx.dev/atom.xml',
json: 'https://npmx.dev/feed.json',
},
})

allPosts
.filter(post => !post.draft)
.forEach(post => {
feed.addItem({
title: post.title,
id: new URL(post.path, 'https://npmx.dev').toString(),
link: new URL(post.path, 'https://npmx.dev').toString(),
description: post.description,
author: post.authors.map(author => ({
name: author.name,
link: author.profileUrl ?? undefined,
// author.avatar is a relative URL - make it absolute to work in feed readers
avatar: author.avatar
? new URL(author.avatar, 'https://npmx.dev').toString()
: undefined,
})),
date: new Date(post.date),
image: post.image,
})
})

const rssPath = 'rss.xml'
const atomPath = 'atom.xml'
const jsonFeedPath = 'feed.json'

await Promise.all([
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of saving these files to disk, creating pre-rendered server-side routes could be a cleaner approach 🤔. Similar to server/routes/opensearch.xml.get.ts with https://nuxt.com/docs/4.x/getting-started/prerendering#selective-pre-rendering

Copy link
Copy Markdown
Contributor Author

@Kiwow Kiwow Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like exactly what I was looking for and didn't find. Thanks!

I'll add a mention of this to the PR description and look into it when I have time to work on this again.

writeFile(join(publicDir, rssPath), feed.rss2()),
writeFile(join(publicDir, atomPath), feed.atom1()),
writeFile(join(publicDir, jsonFeedPath), feed.json1()),
])
},
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"devalue": "5.6.4",
"eslint-plugin-regexp": "3.1.0",
"fast-check": "4.6.0",
"feed": "5.2.0",
"h3": "1.15.8",
"h3-next": "npm:h3@2.0.1-rc.16",
"knip": "6.0.5",
Expand Down
Loading
Loading