-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (57 loc) · 1.93 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
/* eslint-disable no-undef */
import escapeStringRegexp from 'escape-string-regexp'
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event))
})
const EXCLUDED_PATHS = ['/ghost', '/rss', '/content', '/assets']
const EXCLUDED_SUFFIXES = ['/amp']
const IMG_SELECTORS = ['kg-image', 'post-card-image']
const CDN_IMG_SELECTORS = [
'kg-image',
'post-card-image',
'feature-image',
'author-profile-image',
]
class ElementHandler {
element(element) {
const imgClass = element.getAttribute('class') || ''
const imgSrc = element.getAttribute('src') || ''
const imgSrcSet = element.getAttribute('srcset') || ''
const escapedUrl = escapeStringRegexp(ENV_LOCAL_CONTENT_URL)
const localContentRegex = new RegExp(escapedUrl, 'giu')
if (IMG_SELECTORS.some((val) => imgClass.includes(val))) {
// Add lazy loading if not defined already
if (!element.getAttribute('loading')) {
element.setAttribute('loading', 'lazy')
}
}
if (
ENV_ENABLE_CDN_IMAGE === 'on' &&
CDN_IMG_SELECTORS.some((val) => imgClass.includes(val))
) {
// If image is loaded from local content, rewrite to pull from CDN
if (
imgSrc.startsWith(ENV_LOCAL_CONTENT_URL) ||
imgSrc.startsWith(ENV_RELATIVE_CONTENT_PREFIX)
) {
element.setAttribute('src', ENV_CDN_FETCH_URL + imgSrc)
}
if (imgSrcSet.includes(ENV_LOCAL_CONTENT_URL)) {
element.setAttribute(
'srcset',
imgSrcSet.replace(localContentRegex, ENV_CDN_FETCH_URL)
)
}
}
}
}
async function handleRequest(event) {
const pathname = new URL(event.request.url).pathname.toLowerCase()
const res = await fetch(event.request)
if (EXCLUDED_PATHS.some((val) => pathname.startsWith(val))
|| EXCLUDED_SUFFIXES.some((val) => pathname.endsWith(val))) {
return res
} else {
return new HTMLRewriter().on('img', new ElementHandler()).transform(res)
}
}