-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
executable file
·100 lines (90 loc) · 3.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
import SitemapXMLParser from 'datuan-sitemap-parser'
import Sitemap from './sitemap.js'
import Warmer from './warmer.js'
import utils from './utilities.js'
import fetch from 'node-fetch'
import Logger from 'logplease'
import yargs from 'yargs'
const argv = yargs(process.argv.slice(2))
.usage('Usage: $0' + ' domain.com')
.alias('v', 'version')
.alias('h', 'help')
.alias('r', 'range')
.describe('range', 'Only warm up URLs with lastModified newer than this value (in seconds). Default: 300s (5 minutes)')
.default('range', 300)
.alias('d', 'delay')
.describe('delay', 'Delay (in milliseconds) between each warm up call. If you using the low-end hosting, keep this value higher. Default: 500ms')
.default('delay', 500)
.describe('images', 'Enable images warm up. Default: true')
.default('images', true)
.describe('css', 'Enable CSS warm up. Default: true')
.default('css', true)
.describe('js', 'Enable Javascript warm up. Default: true')
.default('js', true)
.describe('brotli', 'Enable Brotli compress warm up. Default: true')
.default('brotli', true)
.describe('webp', 'Enable WebP images warm up. Default: true')
.default('webp', true)
.describe('avif', 'Enable AVIF images warm up. Default: true')
.default('avif', true)
.alias('a', 'all')
.describe('all', 'Ignore --range parameter and warm up all URLs in sitemap')
.alias('q', 'quite')
.describe('quite', 'Disable debug logging if you feel it\'s too much')
.alias('p', 'purge')
.describe('purge', 'Enable purging the resources before warm up.')
.default('purge', 0)
.describe('headers', 'Add custom headers with warmup request. Example --headers.auth \'Bearer secret_token\'')
.default('headers', {})
.argv
const logger = Logger.create('main', {
useLocalTime: true,
})
if (argv.quite) {
Logger.setLogLevel(Logger.LogLevels.INFO)
}
const settings = {
all: argv.all,
sitemap: process.argv[2],
domain: null,
newer_than: parseInt(argv.range) || 300,
delay: parseInt(argv.delay) || 500,
warmup_images: argv.images,
warmup_css: argv.css,
warmup_js: argv.js,
warmup_brotli: argv.brotli,
warmup_webp: argv.webp,
warmup_avif: argv.avif,
purge: parseInt(argv.purge) || 0,
custom_headers: argv.headers,
}
settings.sitemap = utils.tryValidURL(settings.sitemap)
settings.sitemap = new URL(settings.sitemap)
if (utils.isValidURL(settings.sitemap) === false) {
logger.error(`Please specific an valid URL! Your URL ${settings.sitemap} seems not correct.`)
process.exit()
}
if (settings.sitemap.pathname === '/') {
settings.sitemap = new URL('/sitemap.xml', settings.sitemap.href)
}
settings.domain = `${settings.sitemap.protocol}//${settings.sitemap.hostname}`
// Pre-check for issue: https://github.com/tdtgit/sitemap-warmer/issues/4
fetch(settings.sitemap.href).then((res) => {
if (res.ok === false) {
throw new Error(res.statusText)
}
}).then(() => {
logger.info(`📬 Getting sitemap from ${settings.sitemap.href}`)
const sitemapXMLParser = new SitemapXMLParser(settings.sitemap.href, { delay: 3000 })
sitemapXMLParser.fetch().then(urls => {
let sitemap = new Sitemap(settings)
urls.forEach(url => {
sitemap.addURL(url)
})
let warmer = new Warmer(sitemap, settings)
warmer.warmup().then(r => r)
})
}).catch(error => {
logger.error(error)
})