Skip to content

Commit

Permalink
feat(logo-favicon): root domain resolution (#514)
Browse files Browse the repository at this point in the history
This new rule is oriented for resolving favicon.ico at subdomain urls.

e.g., cdn.teslahunt.io/favicon.ico will be missing, but teslahunt.io/favicon.io will be available.
  • Loading branch information
Kikobeats committed Mar 25, 2022
1 parent 7f6a2b9 commit 98f806d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/metascraper-logo-favicon/index.js
Expand Up @@ -4,6 +4,7 @@ const { isEmpty, first, toNumber, chain, get, orderBy } = require('lodash')
const { logo, url: urlFn, toRule } = require('@metascraper/helpers')
const reachableUrl = require('reachable-url')
const memoize = require('@keyvhq/memoize')
const { getDomain } = require('tldts')

const SIZE_REGEX_BY_X = /\d+x\d+/

Expand Down Expand Up @@ -114,6 +115,8 @@ const createGetLogo = ({ gotOpts, keyvOpts }) => {
})
}

const castNull = value => (value === null ? undefined : value)

module.exports = ({ gotOpts, keyvOpts, pickFn = pickBiggerSize } = {}) => {
const getLogo = createGetLogo({ gotOpts, keyvOpts })

Expand All @@ -124,9 +127,12 @@ module.exports = ({ gotOpts, keyvOpts, pickFn = pickBiggerSize } = {}) => {
const size = pickFn(sizes, pickBiggerSize)
return get(size, 'url')
}),
async ({ url }) => castNull(await getLogo(url)),
async ({ url }) => {
const value = await getLogo(url)
return value === null ? undefined : value
const urlObj = new URL(url)
urlObj.hostname = getDomain(url)
const result = await getLogo(urlObj.toString())
return castNull(result)
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion packages/metascraper-logo-favicon/package.json
Expand Up @@ -26,7 +26,8 @@
"@keyvhq/memoize": "~1.6.6",
"@metascraper/helpers": "^5.26.0",
"lodash": "~4.17.21",
"reachable-url": "~1.6.7"
"reachable-url": "~1.6.7",
"tldts": "~5.7.70"
},
"devDependencies": {
"mocha": "latest",
Expand Down
7 changes: 7 additions & 0 deletions packages/metascraper-logo-favicon/test/index.js
Expand Up @@ -212,4 +212,11 @@ describe('metascraper-logo-favicon', () => {
const meta = await metascraper({ url, html })
should(meta.logo).be.null()
})

it('resolve domain favicon', async () => {
const url = 'https://cdn.teslahunt.io/foo/bar'
const metascraper = createMetascraper()
const meta = await metascraper({ url })
should(meta.logo).be.equal('https://teslahunt.io/favicon.ico')
})
})

0 comments on commit 98f806d

Please sign in to comment.