Skip to content

Commit 882adc7

Browse files
author
Brandon Smith
committed
Catch errors when loading HN comments
1 parent b43cb7c commit 882adc7

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

loadHnComments.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ export default async function getCommentsCached(postName: string) {
1717
const data = await loadAndRender(postName)
1818
COMMENTS_CACHE.set(postName, { data, timestamp: Date.now() })
1919
return data
20-
} else if (!POST_COMMENTS_LOADING.get(postName) && (Date.now() - cached.timestamp > COMMENTS_CACHE_LIFETIME)) {
21-
// if comments are cached but expired, start updating them but return cached for now
22-
POST_COMMENTS_LOADING.set(postName, true)
23-
loadAndRender(postName)
24-
.then(data =>
25-
COMMENTS_CACHE.set(postName, { data, timestamp: Date.now() }))
26-
.finally(() => POST_COMMENTS_LOADING.set(postName, false))
27-
28-
return cached.data
2920
} else {
30-
// if cached and not expired, just return
21+
if (!POST_COMMENTS_LOADING.get(postName) && (Date.now() - cached.timestamp > COMMENTS_CACHE_LIFETIME)) {
22+
try {
23+
// if comments are cached but expired, start updating them but return cached for now
24+
POST_COMMENTS_LOADING.set(postName, true)
25+
loadAndRender(postName)
26+
.then(data =>
27+
COMMENTS_CACHE.set(postName, { data, timestamp: Date.now() }))
28+
.finally(() => POST_COMMENTS_LOADING.set(postName, false))
29+
} catch {
30+
}
31+
}
32+
3133
return cached.data
3234
}
3335
}
@@ -94,7 +96,7 @@ function renderComments({ id, by, text, time, kids }: Comment): string {
9496
html`<div class="children">
9597
${kids.map(renderComments).join('')}
9698
</div>`
97-
: ''}
99+
: ''}
98100
`
99101
}
100102

0 commit comments

Comments
 (0)