Skip to content

Commit

Permalink
feat: rss feed (#25)
Browse files Browse the repository at this point in the history
* feat: setup rss feed

* chore: align route file naming pattern

* feat: add robots.txt
  • Loading branch information
edmundhung authored Nov 21, 2022
1 parent a5e6fca commit e4d5fe8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
59 changes: 59 additions & 0 deletions app/routes/[rss.xml].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { LoaderArgs } from '@remix-run/cloudflare';
import { search } from '~/resources';

interface FeedEntry {
title: string;
description: string | null | undefined;
pubDate: string;
guid: string;
}

export async function loader({ context }: LoaderArgs) {
const domain = 'https://remix.guide';
const resources = await context.resourceStore.list();
const list = search(resources, {
limit: 25,
sort: 'new',
});

const entries = list.entries.map<FeedEntry>((resource) => ({
title: resource.title ?? '',
description: resource.description,
pubDate: new Date(resource.createdAt).toUTCString(),
guid: `${domain}/resources/${resource.id}`,
}));

const rss = `
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Remix Guide</title>
<description>A platform for the Remix community</description>
<link>https://remix.guide</link>
<language>en-us</language>
<generator>remix-guide</generator>
<ttl>60</ttl>
<atom:link href="https://remix.guide/rss.xml" rel="self" type="application/rss+xml" />
${entries
.map((entry) =>
`
<item>
<title><![CDATA[${entry.title}]]></title>
<description><![CDATA[${entry.description}]]></description>
<pubDate>${entry.pubDate}</pubDate>
<guid>${entry.guid}</guid>
</item>
`.trim(),
)
.join('\n')}
</channel>
</rss>
`.trim();

return new Response(rss, {
headers: {
'Content-Type': 'application/xml',
'Content-Length': String(new TextEncoder().encode(rss).length),
},
});
}
30 changes: 12 additions & 18 deletions app/routes/sitemap[.]xml.tsx → app/routes/[sitemap.xml].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@ interface SitemapEntry {
}

export async function loader({ context }: LoaderArgs) {
function formatEntry(entry: SitemapEntry) {
return `
<url>
<loc>${entry.loc}</loc>
${entry.lastmod ? `<lastmod>${entry.lastmod}</lastmod>` : ''}
${
entry.changefreq
? `<changefreq>${entry.changefreq}</changefreq>`
: ''
}
${
entry.priority ? `<priority>${entry.priority}</priority>` : ''
}
</url>
`.trim();
}

const guide = await context.resourceStore.getData();
const domain = 'https://remix.guide';
const entries: SitemapEntry[] = [
Expand All @@ -58,7 +41,18 @@ export async function loader({ context }: LoaderArgs) {
const sitemap = `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${entries.map(formatEntry)}
${entries
.map((entry) =>
`
<url>
<loc>${entry.loc}</loc>
${entry.lastmod ? `<lastmod>${entry.lastmod}</lastmod>` : ''}
${entry.changefreq ? `<changefreq>${entry.changefreq}</changefreq>` : ''}
${entry.priority ? `<priority>${entry.priority}</priority>` : ''}
</url>
`.trim(),
)
.join('\n')}
</urlset>
`.trim();

Expand Down
4 changes: 4 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User-agent: *
Allow: /

Sitemap: https://remix.guide/sitemap.xml

0 comments on commit e4d5fe8

Please sign in to comment.