Logo

dev-resources.site

for different kinds of informations.

Implementing RSS feed with Remix

Published at
11/12/2024
Categories
remix
rss
Author
potyoma
Categories
2 categories in total
remix
open
rss
open
Author
7 person written this
potyoma
open
Implementing RSS feed with Remix

RSS is a standard format for publishing frequently updated content, such as blog posts, news articles, and podcast episodes. It allows users to subscribe to a feed, which then delivers new content automatically, often in the form of brief summaries and links to full articles and bla, bla, bla. Simply put RSS is an XML doc which describes how to retrieve your content to a 3rd parties.

I use Remix for my blog and I want to allow the readers to subscribe to the content via RSS. To be honest, I've never worked with it before and at first it sounded to me like some wobbly-bubbly thing. Though after reading a W3School's article it became extremely simple.

Although Remix is considered a React framework, its capabilities are way more. Resource routes allow creating endpoints and handling of custom routes. For my feed I decided to go with /blog/rss.xml route. The only thing I needed in that file is a loader which retrieves blog's data and formats it to a valid RSS XML. I ended up with the following code.
blog.[rss.xml].tsx

import { api } from 'api'
import { toRssFeed } from 'utils/xml'

export const loader = async () => {
  const blogInfo = await api.pages.getByName('blog')
  const posts = await api.blog.getAll()
  return new Response(toRssFeed(blogInfo, posts), {
    status: 200,
    headers: {
      'Content-Type': 'application/rss+xml',
    },
  })
}
Enter fullscreen mode Exit fullscreen mode

And the transformation was as simple as this.

const toRssFeed = (blogInfo: Page, posts: BlogPost[]) => {
  const postItems = posts
    .map((post) => {
      const link = env.BASE_URL + '/blog/' + post.slug
      const date =
        post.datePublished && new Date(post.datePublished).toUTCString()
      return `<item>
          <title>${post.name}</title>
          <link>${link}</link>
          ${date ? `<pubDate>${date}</pubDate>` : ''}
          <guid>${link}</guid>
         </item>`
    })
    .join('')

  const rssDescription = `
    <title>${blogInfo.title}</title>
    <description>${blogInfo.description}</description>
    <link>${env.BASE_URL + '/blog/rss.xml'}</link>
  `

  return `<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0"><channel>${rssDescription}${postItems}</channel></rss>`
}
Enter fullscreen mode Exit fullscreen mode
remix Article's
30 articles in total
Favicon
How to disable a link in React Router 7 / Remix
Favicon
Verifying Your Shopify App Embed is Actually Enabled with Remix: A Step-by-Step Guide
Favicon
Headless e-commerce structure
Favicon
πŸš€ OpenAI's Transition from Next.js to Remix: A Strategic Move in Web Development 🌐
Favicon
# Key New Features in React Router 7: Embracing the Remix Future
Favicon
React Router V7: A Crash Course
Favicon
Stop Running to Next.js β€” Remix is the Future of React, and Here’s Why You’re Missing Out
Favicon
Introducing React Page Tracker: Simplify Navigation Tracking
Favicon
Create an Admin Panel for your project in 5Β minutes
Favicon
Remix Drizzle Auth Template
Favicon
I used GitHub as a CMS
Favicon
Remix vs. Next.js: Why Choose Remix?
Favicon
Choosing Remix as a Server-Side Rendering (SSR) Framework
Favicon
Next.js vs Remix: Which Framework is Better?
Favicon
Using PostHog in Remix Loaders and Actions on Cloudflare Pages
Favicon
Creating a marketplace with Stripe Connect: The onboarding process
Favicon
In-Depth Analysis of Next.js, Gatsby, and Remix
Favicon
Implementing RSS feed with Remix
Favicon
Cloudflare + Remix + PostgreSQL with Prisma Accelerate's Self Hosting
Favicon
Membangun Aplikasi Verifikasi Kode Autentikasi dengan Twilio Menggunakan Go dan Remix
Favicon
Google Analytics (GA4) implementation with React - Remix example
Favicon
Day 3 of Brylnt: Next.js vs Remix
Favicon
𝐌𝐚𝐧𝐭𝐒𝐧𝐞 𝐁𝐨𝐚𝐫𝐝𝐬 πŸš€
Favicon
How to Integrate Mixpanel with Remix?
Favicon
Remix, React, the Discogs API & filtermusikk.no
Favicon
How to navigate between Shopify app
Favicon
Framer Motion useAnimation()
Favicon
An Introduction to Remix
Favicon
Remix vs Next.js: Which Framework Should You Choose?
Favicon
Remix vs Next.js: Which Framework Should You Choose?

Featured ones: