Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
fix RSS feed
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed May 7, 2019
1 parent 15ea915 commit f885212
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 82 deletions.
80 changes: 12 additions & 68 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -12,7 +12,7 @@
"test": "run-p --race dev cy:run"
},
"dependencies": {
"compression": "^1.7.1",
"@polka/send": "^0.4.0",
"node-fetch": "^2.5.0",
"polka": "^0.5.0",
"sirv": "^0.4.0"
Expand Down
21 changes: 10 additions & 11 deletions src/routes/[list]/rss.js
@@ -1,23 +1,24 @@
import fetch from 'node-fetch';
import send from '@polka/send';

const render = (list, items) => `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Svelte HN (${list})</title>
<link>https://hn.svelte.technology/${list}/1</link>
<link>https://hn.svelte.dev/${list}/1</link>
<description>Links from the orange site</description>
<image>
<url>https://hn.svelte.technology/favicon.png</url>
<url>https://hn.svelte.dev/favicon.png</url>
<title>Svelte HN (${list})</title>
<link>https://hn.svelte.technology/${list}/1</link>
<link>https://hn.svelte.dev/${list}/1</link>
</image>
${items.map(item => `
<item>
<title>${item.title}${item.domain ? ` (${item.domain})` : ''}</title>
<link>https://hn.svelte.technology/item/${item.id}</link>
<link>https://hn.svelte.dev/item/${item.id}</link>
<description><![CDATA[${
item.url ? `<a href="${item.url}">link</a> / ` : ''
}<a href="https://hn.svelte.technology/item/${item.id}">comments</a>
}<a href="https://hn.svelte.dev/item/${item.id}">comments</a>
]]></description>
<pubDate>${new Date(item.time * 1000).toUTCString()}</pubDate>
</item>
Expand All @@ -32,15 +33,13 @@ export function get(req, res) {
req.params.list
);

res.set({
'Cache-Control': `max-age=0, s-max-age=${600}`, // 10 minutes
'Content-Type': 'application/rss+xml'
});

fetch(`https://api.hnpwa.com/v0/${list}/1.json`)
.then(r => r.json())
.then(items => {
const feed = render(list, items);
res.end(feed);
send(res, 200, feed, {
'Cache-Control': `max-age=0, s-max-age=${600}`, // 10 minutes
'Content-Type': 'application/rss+xml'
});
});
}
2 changes: 0 additions & 2 deletions src/server.js
@@ -1,14 +1,12 @@
import sirv from 'sirv';
import polka from 'polka';
import compression from 'compression';
import * as sapper from '@sapper/server';

const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';

polka() // You can also use Express
.use(
compression({ threshold: 0 }),
sirv('static', { dev }),
sapper.middleware()
)
Expand Down

0 comments on commit f885212

Please sign in to comment.