Skip to content

Instantly share code, notes, and snippets.

@vdbelt
vdbelt / cloudflare-purge-cache-service-worker.js
Created August 21, 2018 11:43
A CloudFlare service worker that proxies purge cache requests. Example: https://example.com/__purge_cache?zone=XX
addEventListener('fetch', event => {
event.respondWith(purgeCache(event.request))
})
async function purgeCache(request) {
const url = new URL(request.url)
@vdbelt
vdbelt / cloudflare-block-countries-worker.js
Created August 24, 2018 08:23
Blocks certain countries from accessing your page, while giving the flexibility to whitelist certain paths.
addEventListener('fetch', event => {
event.respondWith(byPassOrBlock(event.request))
})
async function byPassOrBlock(request) {
let url = new URL(request.url)
let blockedCountries = ['XX']
let whitelistedPaths = ['/about*']
@vdbelt
vdbelt / cloudflare-hot-link-protection-with-whitelist.js
Last active October 29, 2023 12:41
Cloudflare service worker hot link protection with whitelist
@vdbelt
vdbelt / cloudflare-ignore-query-string.js
Created October 3, 2018 08:31
Ignore query string for optimal caching
addEventListener('fetch', event => {
event.respondWith(ignoreQueryString(event.request))
})
async function ignoreQueryString(request) {
let url = new URL(request.url)
url.search = ''
let modifiedRequest = new Request(url, request)
@vdbelt
vdbelt / cloudflare-worker-device-header-for-origin.js
Last active October 9, 2018 15:21
Send info about device back to origin
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
function isMobile(userAgent) {
return userAgent.match(/iPhone|Android|webOS/i) ? 'Yes' : 'No';
}
async function fetchAndApply(request) {
let isMobileResult = isMobile(request.headers.get('user-agent'))
@vdbelt
vdbelt / dump.sh
Created June 2, 2020 18:31
Mysql dump with ionice
ionice -c2 nice -n19 mysqldump --opt --single-transaction --quick --hex-blob --events --force --user=$MYSQLUSER -p $db | ionice -c2 nice -n19 gzip > sql_dump.sql.gz