From 2e77b0cd99b5abeb94315011ca9c1a5fc77604d3 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Mon, 19 Jun 2023 11:29:41 +0200 Subject: [PATCH] chore(auto-domains): add abc.net.au --- scripts/postinstall | 106 +++++++++++++++++++++++------------------- src/index.js | 9 +++- test/auto-domains.js | 9 ++++ test/is-fetch-mode.js | 12 +++++ 4 files changed, 85 insertions(+), 51 deletions(-) create mode 100644 test/auto-domains.js create mode 100644 test/is-fetch-mode.js diff --git a/scripts/postinstall b/scripts/postinstall index fd8cf2c..02d9ced 100755 --- a/scripts/postinstall +++ b/scripts/postinstall @@ -8,59 +8,67 @@ const writeJsonFile = require('write-json-file') const topsites = require('top-sites') const domains = [ - 'apple', - 'bbc', - 'blogspot', - 'csdn', - 'deviantart', - 'digg', - 'dribbble', - 'engadget', - 'etsy', - 'eventbrite', - 'flickr', - 'ghost', - 'giphy', - 'github', - 'gitlab', - 'google', - 'huffingtonpost', - 'imdb', - 'imgur', - 'meetup', - 'microsoft', - 'nytimes', - 'pinterest', - 'producthunt', - 'slideshare', - 'soundcloud', - 'sourceforge', - 'spotify', - 'stackoverflow', - 'substack', - 'techcrunch', - 'telegraph', - 'theguardian', - 'theverge', - 'tumblr', - 'vimeo', - 'wikipedia', - 'wordpress', - 'ycombinator', - 'yelp', - 'youtube', - 'zoom' + [['domainWithoutSuffix', 'apple']], + [['domainWithoutSuffix', 'bbc']], + [['domainWithoutSuffix', 'blogspot']], + [['domainWithoutSuffix', 'csdn']], + [['domainWithoutSuffix', 'deviantart']], + [['domainWithoutSuffix', 'digg']], + [['domainWithoutSuffix', 'dribbble']], + [['domainWithoutSuffix', 'engadget']], + [['domainWithoutSuffix', 'etsy']], + [['domainWithoutSuffix', 'eventbrite']], + [['domainWithoutSuffix', 'flickr']], + [['domainWithoutSuffix', 'ghost']], + [['domainWithoutSuffix', 'giphy']], + [['domainWithoutSuffix', 'github']], + [['domainWithoutSuffix', 'gitlab']], + [['domainWithoutSuffix', 'google']], + [['domainWithoutSuffix', 'huffingtonpost']], + [['domainWithoutSuffix', 'imdb']], + [['domainWithoutSuffix', 'imgur']], + [['domainWithoutSuffix', 'meetup']], + [['domainWithoutSuffix', 'microsoft']], + [['domainWithoutSuffix', 'nytimes']], + [['domainWithoutSuffix', 'pinterest']], + [['domainWithoutSuffix', 'producthunt']], + [['domainWithoutSuffix', 'slideshare']], + [['domainWithoutSuffix', 'soundcloud']], + [['domainWithoutSuffix', 'sourceforge']], + [['domainWithoutSuffix', 'spotify']], + [['domainWithoutSuffix', 'stackoverflow']], + [['domainWithoutSuffix', 'substack']], + [['domainWithoutSuffix', 'techcrunch']], + [['domainWithoutSuffix', 'telegraph']], + [['domainWithoutSuffix', 'theguardian']], + [['domainWithoutSuffix', 'theverge']], + [['domainWithoutSuffix', 'tumblr']], + [['domainWithoutSuffix', 'vimeo']], + [['domainWithoutSuffix', 'wikipedia']], + [['domainWithoutSuffix', 'wordpress']], + [['domainWithoutSuffix', 'ycombinator']], + [['domainWithoutSuffix', 'yelp']], + [['domainWithoutSuffix', 'youtube']], + [['domainWithoutSuffix', 'zoom']], + [['domain', 'abc.net.au']] ] const { top, rest } = reduce( domains, - (acc, domain) => { - const index = findIndex( - topsites, - ({ rootDomain }) => parseUrl(rootDomain).domainWithoutSuffix === domain - ) - if (index !== -1) acc.top[index] = domain - else acc.rest.push(domain) + (acc, conditions) => { + for (const [key, value] of conditions) { + const index = findIndex(topsites, ({ rootDomain }) => { + const parsedUrl = parseUrl(rootDomain) + return parsedUrl[key] === value + }) + + if (index !== -1) { + acc.top[index] = conditions + return acc + } + } + + acc.rest.push(conditions) return acc }, { top: new Array(topsites.length), rest: [] } diff --git a/src/index.js b/src/index.js index 9588554..0f94e74 100644 --- a/src/index.js +++ b/src/index.js @@ -138,8 +138,12 @@ const prerender = PCancelable.fn( const modes = { fetch, prerender } -const isFetchMode = url => - autoDomains.includes(parseUrl(url).domainWithoutSuffix) +const isFetchMode = url => { + const parsedUrl = parseUrl(url) + return autoDomains.some(conditions => + conditions.every(([prop, value]) => parsedUrl[prop] === value) + ) +} const determinateMode = (url, { prerender }) => { if (prerender === false || isMediaUrl(url)) return 'fetch' @@ -210,3 +214,4 @@ module.exports = PCancelable.fn( ) module.exports.REQ_TIMEOUT = REQ_TIMEOUT +module.exports.isFetchMode = isFetchMode diff --git a/test/auto-domains.js b/test/auto-domains.js new file mode 100644 index 0000000..9711475 --- /dev/null +++ b/test/auto-domains.js @@ -0,0 +1,9 @@ +'use strict' + +const test = require('ava') + +const autoDomains = require('../src/auto-domains.json') + +test('domains are sorted by popularity', t => { + t.is(autoDomains[0][0][1], 'youtube') +}) diff --git a/test/is-fetch-mode.js b/test/is-fetch-mode.js new file mode 100644 index 0000000..5c6ab1b --- /dev/null +++ b/test/is-fetch-mode.js @@ -0,0 +1,12 @@ +'use strict' + +const test = require('ava') + +const { isFetchMode } = require('..') + +test('true', t => { + const url = + 'https://www.abc.net.au/news/2023-06-14/idpwd-2023-calling-all-budding-storytellers-with-disability/102388090' + + t.true(isFetchMode(url)) +})