Home > Software design >  RSS feed with SvelteKit
RSS feed with SvelteKit

Time:03-16

I have been trying to generate RSS feed for my website. Although it perfectly works in the dev environment, the xml file is not generated when building for production.

Since I have created some json files in the same way and those are properly generated, I suspect xml.ts format might not be regarded as the build target.

Does anyone know the solution?

source code (src/routes/rss.xml.ts)

import { getPosts } from '../lib/posts/getPosts';

const xml = (posts: postMeta[]) => `<?xml version="1.0" encoding="UTF-8" ?>
<rss xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:atom="https://www.w3.org/2005/Atom" version="2.0">
<channel>
  <title>Kota Yatagai</title>
  <link>https://kota-yata.com</link>
  <description><![CDATA[Personal Blog & Stuffs by Kota Yatagai]]></description>
  ${posts.map(
    post => `
      <item>
        <title><![CDATA[${post.meta.title}]]></title>
        <description><![CDATA[${post.meta.description}]]></description>
        <category>${post.meta.category}</category>
        <link>https://kota-yata.com/posts/${post.path}</link>
        <guid isPermaLink="true">https://kota-yata.com/posts/${post.path}</guid>
        <pubDate><![CDATA[ ${post.meta.date}]]></pubDate>
        <enclosure url="${post.meta.ogp || `https://kota-yata.com/ogp.webp`}" length="0" type="image/webp"/>
        <dc:creator>Kota Yatagai</dc:creator>
      </item>
    `
  ).join('')}
</channel>
</rss>`;

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const get = () => {
  const headers = {
    'Cache-Control': 'max-age=0, s-maxage=600',
    'Content-Type': 'application/xml',
  };
  const posts = getPosts();
  const body = xml(posts);
  return { body, headers };
};

svelte.config.js (just in case)

import adapter from '@sveltejs/adapter-static';
import preprocess from 'svelte-preprocess';

/** @type {import('@sveltejs/kit').Config} */
const config = {
  // Consult https://github.com/sveltejs/svelte-preprocess
  // for more information about preprocessors
  preprocess: preprocess(),

  kit: {
    // hydrate the <div id="svelte"> element in src/app.html
    target: '#svelte',
    adapter: adapter({
      pages: 'build',
      assets: 'build',
      fallback: null
    })
  },
};

export default config;

CodePudding user response:

One likely cause is that you do not have anything linked to this endpoint.

When SvelteKit builds your static page it will start at home page (index.svelte) and crawl your entire site, "clicking" on each link, rendering the page and writing it to the filesystem. But this means that any non-linked pages will not be rendered.

It could be this is your issue, if so simply add a link somewhere to rss.xml and it will generate that page correctly as well.

  • Related