Skip to content

Commit

Permalink
fix(gatsby-recipes): Cache readme and always return string (#26833)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Sep 9, 2020
1 parent 9db8b00 commit 58dc15e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/gatsby-recipes/src/providers/gatsby/plugin.js
Expand Up @@ -102,16 +102,27 @@ const getNameForPlugin = node => {
const getDescriptionForPlugin = async name => {
const pkg = await readPackageJSON({}, name)

return pkg ? pkg.description : null
return pkg?.description || ``
}

const readmeCache = new Map()

const getReadmeForPlugin = async name => {
if (readmeCache.has(name)) {
return readmeCache.get(name)
}

try {
return fetch(`https://unpkg.com/${name}/README.md`)
const readme = await fetch(`https://unpkg.com/${name}/README.md`)
.then(res => res.text())
.catch(() => null)

if (readme) {
readmeCache.set(name, readme)
}
return readme || ``
} catch (err) {
return null
return ``
}
}

Expand Down Expand Up @@ -244,8 +255,8 @@ const read = async ({ root }, id) => {

return {
id,
description: description || null,
readme: readme,
description,
readme,
...plugin,
shadowedFiles,
shadowableFiles,
Expand Down

0 comments on commit 58dc15e

Please sign in to comment.