Генерация RSS-фида для блога
RSS-фид блога позволяет читателям подписаться на новые публикации. Правильно настроенный фид ускоряет индексацию поисковиками и увеличивает охват аудитории.
Node.js: пакет feed
import { Feed } from 'feed';
export async function generateBlogRss(): Promise<string> {
const feed = new Feed({
title: 'MyApp Blog',
description: 'Статьи о разработке и технологиях',
id: 'https://example.com',
link: 'https://example.com',
language: 'ru',
image: 'https://example.com/logo.png',
favicon: 'https://example.com/favicon.ico',
copyright: `© ${new Date().getFullYear()} MyApp`,
generator: 'MyApp',
feedLinks: {
rss: 'https://example.com/feed',
atom: 'https://example.com/feed/atom',
json: 'https://example.com/feed/json',
},
author: {
name: 'Редакция MyApp',
email: '[email protected]',
link: 'https://example.com/about',
},
});
const posts = await prisma.post.findMany({
where: { published: true },
orderBy: { publishedAt: 'desc' },
take: 50,
include: { author: true, tags: true },
});
posts.forEach(post => {
feed.addItem({
title: post.title,
id: `https://example.com/blog/${post.slug}`,
link: `https://example.com/blog/${post.slug}`,
description: post.excerpt,
content: post.htmlContent,
author: [{ name: post.author.name, email: post.author.email }],
date: post.publishedAt,
image: post.coverImage || undefined,
category: post.tags.map(t => ({ name: t.name })),
});
});
return feed.rss2(); // или feed.atom1(), feed.json1()
}
// Express маршрут
app.get('/feed', async (_req, res) => {
const rss = await generateBlogRss();
res
.type('application/rss+xml')
.set('Cache-Control', 'public, max-age=900')
.send(rss);
});
Next.js: статический RSS при сборке
// scripts/generate-rss.ts
import { writeFileSync } from 'fs';
import { generateBlogRss } from '../lib/rss';
async function main() {
const rss = await generateBlogRss();
writeFileSync('./public/feed.xml', rss);
console.log('RSS generated: public/feed.xml');
}
main();
// package.json
{
"scripts": {
"build": "next build && ts-node scripts/generate-rss.ts"
}
}
Валидация фида
Проверить корректность фида: validator.w3.org/feed. Инструмент указывает на ошибки кодировки, отсутствующие обязательные поля, некорректные даты.
Срок реализации
RSS-фид для блога на Node.js или Laravel: 0.5 дня. С JSON Feed, Atom, кэшированием и autodiscovery: 1 день.







