Skip to content

Commit

Permalink
fix(logo-favicon): dedupe memoize key from input
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 27, 2022
1 parent 373d7f2 commit 1e21849
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/metascraper-logo-favicon/index.js
Expand Up @@ -7,6 +7,7 @@ const { getDomain } = require('tldts')

const {
logo,
memoizeOne,
normalizeUrl,
toRule,
url: urlFn
Expand Down Expand Up @@ -107,16 +108,19 @@ const pickBiggerSize = sizes => {
pickBiggerSize.sortBySize = collection =>
orderBy(collection, ['size.priority'], ['desc'])

const getFaviconUrl = memoizeOne(url => logo('/favicon.ico', { url }))

const createGetLogo = ({ gotOpts, keyvOpts }) => {
const getLogo = async url => {
const faviconUrl = logo('/favicon.ico', { url })
const faviconUrl = getFaviconUrl(url)
if (!faviconUrl) return

const response = await reachableUrl(faviconUrl, gotOpts)
return reachableUrl.isReachable(response) ? faviconUrl : undefined
}

return memoize(getLogo, keyvOpts, {
key: getFaviconUrl,
value: value => (value === undefined ? null : value)
})
}
Expand All @@ -143,3 +147,5 @@ module.exports = ({ gotOpts, keyvOpts, pickFn = pickBiggerSize } = {}) => {
]
}
}

module.exports.createGetLogo = createGetLogo
15 changes: 15 additions & 0 deletions packages/metascraper-logo-favicon/test/index.js
Expand Up @@ -6,6 +6,8 @@ const should = require('should')

const createMetascraper = opts => require('metascraper')([require('..')(opts)])

const { createGetLogo } = require('..')

const createHtml = meta =>
`<!DOCTYPE html>
<html lang="en">
Expand All @@ -29,6 +31,19 @@ describe('metascraper-logo-favicon', () => {
})
})

describe('.createGetLogo', () => {
it('dedupe key from input', async () => {
const cache = new Map()
const getLogo = createGetLogo({ keyvOpts: { store: cache } })

await getLogo('https://help.kinopio.club/about/')
await getLogo('https://help.kinopio.club/legal/')
await getLogo('https://help.kinopio.club/')
await getLogo('https://kinopio.club/')

should(cache.size).be.equal(2)
})
})
it('create an absolute favicon url if the logo is not present', async () => {
const url = 'https://www.nytimes.com'
const metascraper = createMetascraper()
Expand Down

0 comments on commit 1e21849

Please sign in to comment.