SvelteKit生成的RSS Feed无法在阅读器显示内容求助
问题解决:RSS Feed验证通过但阅读器不显示内容
你的RSS 2.0 Feed通过了W3C验证,但阅读器无法显示内容的核心原因是:主流RSS阅读器默认优先读取content:encoded字段展示文章内容,而当前代码仅提供了description字段。以下是具体修复方案:
关键修复点
- 为每个
<item>添加content:encoded字段,注入文章的完整内容(或适合预览的HTML内容) - 对内容中的XML特殊字符进行转义,避免破坏XML结构
- 替换占位符日期为真实的文章发布时间(优化阅读器排序逻辑)
修改后的完整代码
// @ts-nocheck import { db } from '$lib/app'; export const prerender = true; const siteURL = 'https://learn.flucoma.org'; // XML特殊字符转义函数 const escapeXML = (str) => { return str .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, '''); }; export function GET({ }) { const posts = db.filter(x => x.flair == 'podcast'); const render = (posts) => (`<?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" > <channel> <title>FluCoMa Learn Podcasts</title> <description>The latest FluCoMa podcasts, hosted by Jacob Hart with artists working with machine learning and machine listening technology in their artistic practices.</description> <language>en-gb</language> <link>https://learn.flucoma.org</link> <atom:link href="${siteURL}/explore/rss.xml" rel="self" type="application/rss+xml"/> ${posts .map( (post) => `<item> <guid isPermaLink="true">${siteURL}${post.url}</guid> <title>${escapeXML(post.title)}</title> <link>${siteURL}${post.url}</link> <description>${escapeXML(post.blurb)}</description> <!-- 添加content:encoded字段,注入文章内容 --> <content:encoded><![CDATA[${post.content || post.blurb}]]></content:encoded> <author>info@flucoma.org</author> <!-- 替换为真实的文章发布日期,示例假设post有publishedAt字段 --> <pubDate>${post.publishedAt ? new Date(post.publishedAt).toUTCString() : new Date().toUTCString()}</pubDate> </item>` ) .join('') } </channel> </rss> `); const xml = render(posts); return new Response(xml, { headers: { 'Cache-Control': 'max-age=0, s-maxage=3600', 'Content-Type': 'application/rss+xml', // 修正为标准RSS MIME类型 } }); }
额外优化说明
content:encoded使用CDATA块:CDATA可以直接包含HTML内容而无需额外转义(但如果内容本身包含]]>则仍需处理),适合存放带格式的文章内容- 修正Content-Type:将
application/xml改为标准的application/rss+xml,帮助阅读器更准确识别Feed类型 - 标题和描述转义:确保标题和简介中的特殊字符不会破坏XML结构
内容的提问来源于stack exchange,提问作者james
相关产品推荐
相关产品推荐

