From 099fcdec8bd17e09b8f74f9d62155416bcb4e2e3 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Thu, 12 Jun 2025 19:04:42 -0500 Subject: [PATCH] Refine feed formatting --- app/rss.xml/route.ts | 48 +++++++++++++++++++-------------------- src/app/feed.ts | 54 +++++++++++++++++++------------------------- 2 files changed, 46 insertions(+), 56 deletions(-) diff --git a/app/rss.xml/route.ts b/app/rss.xml/route.ts index 79573fca..100065d3 100644 --- a/app/rss.xml/route.ts +++ b/app/rss.xml/route.ts @@ -1,4 +1,3 @@ -import { INFINITE_SCROLL_FEED_INITIAL } from '@/photo'; import { getPhotosCached } from '@/photo/cache'; import { BASE_URL, @@ -6,40 +5,39 @@ import { META_TITLE, SITE_FEEDS_ENABLED, } from '@/app/config'; -import { feedPhotoToXml, formatPhotoForFeedRss } from '@/app/feed'; -import { ABSOLUTE_PATH_FOR_FEED_JSON } from '@/app/paths'; +import { createRssItems, FEED_PHOTO_REQUEST_LIMIT } from '@/app/feed'; +import { ABSOLUTE_PATH_FOR_RSS_XML } from '@/app/paths'; export const dynamic = 'force-static'; export async function GET() { if (SITE_FEEDS_ENABLED) { const photos = await getPhotosCached({ - limit: INFINITE_SCROLL_FEED_INITIAL, + limit: FEED_PHOTO_REQUEST_LIMIT, sortBy: 'createdAt', }); - const items = photos.map(formatPhotoForFeedRss).map(feedPhotoToXml); - return new Response( - ` - + const items = createRssItems(photos); - - ${META_TITLE} - - ${BASE_URL} - ${META_DESCRIPTION} - ${items.join('\n')} - - - `, - { - headers: { - 'Content-Type': 'text/xml', - }, - }, + return new Response(` + + + + ${META_TITLE} + + ${BASE_URL} + ${META_DESCRIPTION} + ${items.join('\n')} + + + `, + { headers: { 'Content-Type': 'text/xml' } }, ); } else { return new Response('RSS feed access disabled', { status: 404 }); diff --git a/src/app/feed.ts b/src/app/feed.ts index 12d4eab5..cf9ddd9f 100644 --- a/src/app/feed.ts +++ b/src/app/feed.ts @@ -12,22 +12,6 @@ export const FEED_PHOTO_WIDTH_SMALL = 200; export const FEED_PHOTO_WIDTH_MEDIUM = 640; export const FEED_PHOTO_WIDTH_LARGE = 1200; -export interface PublicFeedJson { - meta: { - title: string - url: string - } - photos: PublicFeedPhotoJson[] -} - -export interface PublicFeedRss { - meta: { - title: string - url: string - } - photos: PublicFeedPhotoRss[] -} - interface PublicFeedMedia { url: string width: number @@ -42,10 +26,7 @@ interface PublicFeedPhotoJson { model?: string tags?: string[] takenAtNaive: string - src: Record< - 'small' | 'medium' | 'large', - PublicFeedMedia - > + src: Record<'small' | 'medium' | 'large', PublicFeedMedia> } interface PublicFeedPhotoRss { @@ -54,10 +35,15 @@ interface PublicFeedPhotoRss { description?: string link: string publicationDate: Date - media: Record< - 'content' | 'thumb', - PublicFeedMedia - > + media: Record<'content' | 'thumb', PublicFeedMedia> +} + +export interface PublicFeedJson { + meta: { + title: string + url: string + } + photos: PublicFeedPhotoJson[] } const generateFeedMedia = ( @@ -89,7 +75,7 @@ export const formatPhotoForFeedJson = (photo: Photo): PublicFeedPhotoJson => ({ }, }); -export const formatPhotoForFeedRss = (photo: Photo): PublicFeedPhotoRss => ({ +const formatPhotoForFeedRss = (photo: Photo): PublicFeedPhotoRss => ({ ...getCoreFeedFields(photo), link: absolutePathForPhoto({ photo }), publicationDate: photo.createdAt, @@ -99,7 +85,7 @@ export const formatPhotoForFeedRss = (photo: Photo): PublicFeedPhotoRss => ({ }, }); -export const feedPhotoToXml = (photo: PublicFeedPhotoRss): string => { +const feedPhotoToXml = (photo: PublicFeedPhotoRss): string => { const formattedDate = formatDate({ date: photo.publicationDate, length: 'rss', @@ -112,14 +98,20 @@ export const feedPhotoToXml = (photo: PublicFeedPhotoRss): string => { - - + height="${photo.media.content.height}" + > + `; }; + +export const createRssItems = (photos: Photo[]) => + photos.map(formatPhotoForFeedRss).map(feedPhotoToXml);