Skip to content

Commit d2f4291

Browse files
authored
perf(logo-favicon): reduce favicon candidate allocations (#814)
* perf(logo-favicon): reduce favicon candidate allocations * test: update snapshot
1 parent c175eb4 commit d2f4291

4 files changed

Lines changed: 37 additions & 46 deletions

File tree

packages/metascraper-logo-favicon/src/index.js

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { isEmpty, first, toNumber, chain, orderBy } = require('lodash')
3+
const { isEmpty, first, toNumber, orderBy } = require('lodash')
44
const reachableUrl = require('reachable-url')
55
const memoize = require('@keyvhq/memoize')
66

@@ -27,10 +27,7 @@ const isValidContenType = (contentType, contentTypes) =>
2727
const toSize = (input, url) => {
2828
if (isEmpty(input)) return
2929

30-
const [verticalSize, horizontalSize] = chain(input)
31-
.replace(/×/g, 'x')
32-
.split('x')
33-
.value()
30+
const [verticalSize, horizontalSize] = input.replace(/×/g, 'x').split('x')
3431

3532
const height = toNumber(verticalSize) || 0
3633
const width = toNumber(horizontalSize) || 0
@@ -67,30 +64,31 @@ const getSize = (url, sizes) =>
6764
toSize.fallback(url)
6865

6966
const getDomNodeSizes = (domNodes, attr, url) =>
70-
chain(domNodes)
71-
.reduce((acc, domNode) => {
72-
const relativeUrl = domNode.attribs[attr]
73-
if (!relativeUrl || relativeUrl === url) return acc
74-
const normalizedUrl = normalizeUrl(url, relativeUrl)
75-
if (!normalizedUrl) return acc
76-
return [
77-
...acc,
78-
{
79-
...domNode.attribs,
80-
url: normalizedUrl,
81-
size: getSize(normalizedUrl, domNode.attribs.sizes)
82-
}
83-
]
84-
}, [])
85-
.value()
67+
domNodes.reduce((acc, domNode) => {
68+
const relativeUrl = domNode.attribs[attr]
69+
if (!relativeUrl || relativeUrl === url) return acc
70+
71+
const normalizedUrl = normalizeUrl(url, relativeUrl)
72+
if (!normalizedUrl) return acc
73+
74+
acc.push({
75+
...domNode.attribs,
76+
url: normalizedUrl,
77+
size: getSize(normalizedUrl, domNode.attribs.sizes)
78+
})
79+
80+
return acc
81+
}, [])
8682

8783
const getSizes = ($, collection, url) =>
88-
chain(collection)
89-
.reduce((acc, { tag, attr }) => {
90-
const domNodes = $(tag).get()
91-
return [...acc, ...getDomNodeSizes(domNodes, attr, url)]
92-
}, [])
93-
.value()
84+
collection.reduce((acc, { tag, attr }) => {
85+
const domNodes = $(tag).get()
86+
const domNodeSizes = getDomNodeSizes(domNodes, attr, url)
87+
88+
for (const size of domNodeSizes) acc.push(size)
89+
90+
return acc
91+
}, [])
9492

9593
const sizeSelectors = [
9694
{ tag: 'link[rel*="icon" i]', attr: 'href' }, // apple-icon, // fluid-icon
@@ -187,13 +185,17 @@ const createGetLogo = ({
187185
withFavicon,
188186
withGoogle
189187
}) => {
190-
const getLogo = async url => {
191-
const providers = ALLOWED_EXTENSION_CONTENT_TYPES.map(
192-
ext => withFavicon && createFavicon(ext, resolveFaviconUrl)
193-
)
194-
.concat(withGoogle && google)
195-
.filter(Boolean)
188+
const providers = []
189+
190+
if (withFavicon) {
191+
for (const extensionAndTypes of ALLOWED_EXTENSION_CONTENT_TYPES) {
192+
providers.push(createFavicon(extensionAndTypes, resolveFaviconUrl))
193+
}
194+
}
195+
196+
if (withGoogle) providers.push(google)
196197

198+
const getLogo = async url => {
197199
for (const provider of providers) {
198200
const logoUrl = await provider(url, { gotOpts })
199201
if (logoUrl) return logoUrl

packages/metascraper-manifest/test/index.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,6 @@ test('from data uri', async t => {
6868
t.snapshot(metadata)
6969
})
7070

71-
test('fallback to `name` when `short_name` is missing', async t => {
72-
const metascraper = createMetascraper()
73-
const url = 'https://example.com'
74-
const html = createHtml([
75-
'<link rel="manifest" href="data:application/json;base64,eyJuYW1lIjoiTmFtZSBPbmx5In0=">'
76-
])
77-
const metadata = await metascraper({ url, html })
78-
t.is(metadata.publisher, 'Name Only')
79-
t.is(metadata.logo, null)
80-
})
81-
8271
test('does nothing if data uri is malformed', async t => {
8372
const metascraper = createMetascraper()
8473
const url = 'https://krafla-landing-g19o4bcij-trence.vercel.app/'

packages/metascraper-manifest/test/snapshots/index.js.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Generated by [AVA](https://avajs.dev).
3434
description: null,
3535
lang: null,
3636
logo: 'https://krafla-landing-g19o4bcij-trence.vercel.app/icon/android-icon-192x192.png',
37-
publisher: 'App',
37+
publisher: null,
3838
}
3939

4040
## does nothing if data uri is malformed
@@ -67,7 +67,7 @@ Generated by [AVA](https://avajs.dev).
6767
description: null,
6868
lang: null,
6969
logo: null,
70-
publisher: 'LinkedIn',
70+
publisher: null,
7171
}
7272

7373
## medium.com
-15 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)