Skip to content

Commit

Permalink
feat(logo): ensure logo from markup is reachable
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Feb 11, 2024
1 parent 9a22b5b commit d24551f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
21 changes: 16 additions & 5 deletions packages/metascraper-logo-favicon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ const sizeSelectors = [
{ tag: 'meta[name*="msapplication" i]', attr: 'content' } // Windows 8, Internet Explorer 11 Tiles
]

const pickBiggerSize = sizes => {
const firstReachable = async (domNodeSizes, { url, gotOpts }) => {
for (const domNodeSize of domNodeSizes) {
const absoluteUrl = normalizeUrl(url, domNodeSize.url)
const response = await reachableUrl(absoluteUrl, gotOpts)
if (reachableUrl.isReachable(response)) {
return response.url
}
}
}

const pickBiggerSize = async (sizes, opts) => {
const sorted = sizes.reduce(
(acc, item) => {
acc[item.size.square ? 'square' : 'nonSquare'].push(item)
Expand All @@ -97,8 +107,8 @@ const pickBiggerSize = sizes => {
)

return (
first(pickBiggerSize.sortBySize(sorted.square)) ||
first(pickBiggerSize.sortBySize(sorted.nonSquare))
(await firstReachable(pickBiggerSize.sortBySize(sorted.square), opts)) ||
(await firstReachable(pickBiggerSize.sortBySize(sorted.nonSquare), opts))
)
}

Expand Down Expand Up @@ -170,9 +180,9 @@ module.exports = ({
const rootFavicon = createRootFavicon({ getLogo, withRootFavicon })
return {
logo: [
toLogo($ => {
toLogo(($, url) => {
const sizes = getSizes($, sizeSelectors)
const size = pickFn(sizes, pickBiggerSize)
const size = pickFn(sizes, { url, gotOpts })
return get(size, 'url')
}),
({ url }) => getLogo(normalizeUrl(url)),
Expand All @@ -185,3 +195,4 @@ module.exports.favicon = favicon
module.exports.google = google
module.exports.createRootFavicon = createRootFavicon
module.exports.createGetLogo = createGetLogo
module.exports.pickBiggerSize = pickBiggerSize
7 changes: 6 additions & 1 deletion packages/metascraper-logo-favicon/test/favicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ const test = require('ava')

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

test('with { contentType: \'image/vnd.microsoft.icon\' }', async t => {
test('return undefined if favicon is not reachable', async t => {
const url = 'https://idontexist.lol'
t.is(await favicon(url), undefined)
})

test("with { contentType: 'image/vnd.microsoft.icon' }", async t => {
const url = 'https://microlink.io/'
t.is(await favicon(url), 'https://microlink.io/favicon.ico')
})
2 changes: 1 addition & 1 deletion packages/metascraper-logo-favicon/test/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const got = require('got')

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

test('return undefined under no logo', async t => {
test('return undefined if favicon is not reachable', async t => {
const url = 'https://idontexist.lol'
t.is(await google(url), undefined)
})
Expand Down
44 changes: 44 additions & 0 deletions packages/metascraper-logo-favicon/test/pick-fn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const test = require('ava')

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

test('ensure logo is reachable', async t => {
const sizes = [
{
rel: 'icon',
type: 'image/png',
sizes: '16x16',
href: 'https://www.android.com/=w16',
url: 'https://www.android.com/=w16',
size: { height: 16, width: 16, square: true, priority: 80 }
},
{
rel: 'icon',
type: 'image/png',
sizes: '32x32',
href: 'https://www.android.com/=w32',
url: 'https://www.android.com/=w32',
size: { height: 32, width: 32, square: true, priority: 160 }
},
{
rel: 'apple-touch-icon-precomposed',
sizes: '180x180',
href: 'https://www.android.com/=w180',
url: 'https://www.android.com/=w180',
size: { height: 180, width: 180, square: true, priority: 900 }
},
{
rel: 'shortcut icon',
href: 'https://www.android.com/static/img/favicon.ico?cache=33c79c9',
url: 'https://www.android.com/static/img/favicon.ico?cache=33c79c9',
size: { width: 0, height: 0, square: true, priority: 5 }
}
]

t.is(
await pickBiggerSize(sizes),
'https://www.android.com/static/img/favicon.ico?cache=33c79c9'
)
})

0 comments on commit d24551f

Please sign in to comment.