Skip to content

Commit b6cb960

Browse files
committed
Implement a more efficient way to fetch the feeds. Fix sources that do not provide a title
1 parent 06c66ce commit b6cb960

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

index.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ addEventListener('fetch', event => {
3030
/**
3131
* Deliver aggregated content according to the formats requested
3232
* @param {Request} request
33+
* @returns Response
3334
*/
3435
async function handleRequest(request) {
3536
const cacheUrl = new URL(request.url)
@@ -69,23 +70,30 @@ async function handleRequest(request) {
6970

7071
/**
7172
* Fetch all source feeds and generate the aggregated content
72-
*
73-
* TODO implement a faster way to fetch several sources
7473
*/
7574
async function handleScheduled() {
7675
let feeds = FEEDS.split(',')
7776
let content = []
7877
let sources = []
79-
let items
78+
79+
let promises = []
8080
for (let url of feeds) {
81-
try {
82-
items = await fetchAndHydrate(url)
83-
sources.push({ name: items[0].source_title, link: items[0].source_link })
84-
} catch (error) {
85-
console.log(`Failed to fetch ${url}`)
86-
console.log(error)
81+
promises.push(fetchAndHydrate(url))
82+
}
83+
const results = await Promise.allSettled(promises)
84+
85+
for (let [index, result] of results.entries()) {
86+
if (result.status == 'fulfilled') {
87+
let posts = result.value
88+
let title = posts[0].source_title
89+
let link = posts[0].source_link
90+
let name = title != '' ? title : new URL(link).host
91+
sources.push({ name, link })
92+
content.push(...posts)
93+
} else {
94+
console.log(`Failed to fetch ${feeds[index]}`)
95+
console.log(result.reason)
8796
}
88-
content.push(...items)
8997
}
9098

9199
//sort all the elements chronologically (recent first)
@@ -116,7 +124,8 @@ async function handleScheduled() {
116124

117125
/**
118126
* Take a feed URL, fetch all items and attach source information
119-
* @param {Array} feeds
127+
* @param {String} feed The URL of the feed to be fetched and parsed
128+
* @returns Array containing all the feed items parsed by rss-parser
120129
*/
121130
async function fetchAndHydrate(feed) {
122131
console.log(`[fetchAndHydrate] start to fetch feed: ${feed}`)
@@ -141,7 +150,8 @@ async function fetchAndHydrate(feed) {
141150

142151
/**
143152
* Builds a feed object from the provided items
144-
* @param {Array} items
153+
* @param {Array} items parsed by rss-parser
154+
* @return Feed object created by feed
145155
*/
146156
function createFeed(items) {
147157
console.log(`[createFeed] start building the aggregated feed`)
@@ -175,8 +185,8 @@ function createFeed(items) {
175185
}
176186
/**
177187
* Generate the HTML page with the aggregated contents
178-
* @param {*} items
179-
* @returns
188+
* @param {Array} items parsed by rss-parser
189+
* @returns String with HTML page containing the parsed contents
180190
*/
181191
function createHTML(items, sources) {
182192
console.log(`[createHTML] building the HTML document`)
@@ -185,7 +195,7 @@ function createHTML(items, sources) {
185195

186196
for (let item of items) {
187197
let shortdescription = striptags(item.content).substring(0, 250)
188-
item.description = shortdescription ? shortdescription + ' [...]' : ""
198+
item.description = shortdescription ? shortdescription + ' [...]' : ''
189199
item.formattedDate = item.pubDate
190200
? dateFormatter.format(new Date(item.pubDate))
191201
: ''

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"format": "prettier --write '**/*.{js,css,json,md}' '!**/worker/*' '!**/templates/*'",
99
"template": "handlebars -c handlebars/runtime",
1010
"build": "webpack",
11-
"dev": "wrangler dev",
11+
"dev": "webpack && wrangler dev",
1212
"deploy": "wrangler deploy"
1313
},
1414
"author": "Gonçalo Valério <[email protected]>",

0 commit comments

Comments
 (0)