Skip to content

Instantly share code, notes, and snippets.

View Nooshu's full-sized avatar

Matt Hobbs Nooshu

View GitHub Profile
@Nooshu
Nooshu / worker-inline-css.js
Created March 1, 2021 23:53
Use a CF worker to add inline CSS to the head.
// set the site we are modifying
const site = 'www.example.com';
// do this on a fetch
addEventListener('fetch', event => {
const request = event.request
const url = new URL(request.url)
event.respondWith(handleRequest(request))
});
@Nooshu
Nooshu / worker-remove-elements.js
Created March 1, 2021 22:44
Get a CF Worker to remove selected elements from a pages HTML
// set the site we are modifying
const site = 'www.example.com';
// do this on a fetch
addEventListener('fetch', event => {
const request = event.request
const url = new URL(request.url)
event.respondWith(handleRequest(request))
});
@Nooshu
Nooshu / worker-modify-css-js.js
Created March 1, 2021 18:52
Make a CF worker modify a CSS file and / or a JavaScript file on the fly.
// set the site we are modifying
const site = 'www.example.com';
// do this on a fetch
addEventListener('fetch', event => {
const request = event.request
const url = new URL(request.url)
event.respondWith(handleRequest(request))
});
@Nooshu
Nooshu / worker-resource-hint-header.js
Created March 1, 2021 17:10
Make the CF worker add resource hint headers to the HTML response
// set the site we are modifying
const site = 'www.example.com';
// do this on a fetch
addEventListener('fetch', event => {
const request = event.request
const url = new URL(request.url)
event.respondWith(handleRequest(request))
});
@Nooshu
Nooshu / worker-resource-hints.js
Created March 1, 2021 10:27
Getting a cloudflare worker to add resource hints to the head.
// set the site we are modifying
const site = 'www.example.com';
var resourceHints = `
<link rel="preload" href="/assets/font/font1.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="dns-prefetch" href="https://fonts.gstatic.com/">
<link href="https://cdn.domain.com" rel="preconnect" crossorigin>
`
// do this on a fetch
@Nooshu
Nooshu / worker-rewrite-urls.js
Created February 28, 2021 23:13
Here we are rewriting the URLs so they all flow through our worker
// set the site we are modifying
const site = 'www.example.com';
// domain rewriter vars
const OLD_URL = "https://www.example.com/";
const NEW_URL = "/";
// do this on a fetch
addEventListener('fetch', event => {
const request = event.request
@Nooshu
Nooshu / worker-boilerplate.js
Created February 28, 2021 22:27
Boilerplate code for our web performance modifications to the page.
// set the site we are modifying
const site = 'www.example.com';
// do this on a fetch
addEventListener('fetch', event => {
const request = event.request
event.respondWith(handleRequest(request))
});
async function handleRequest(request) {
@Nooshu
Nooshu / workre.js
Created February 20, 2021 23:35
Cloudflare Worker code used when modifying the priority hints for the Nike UK homepage.
// set the site we are modifying
const site = 'www.nike.com';
// do this on a fetch
addEventListener('fetch', event => {
const request = event.request
const url = new URL(request.url)
event.respondWith(handleRequest(request))
});
@Nooshu
Nooshu / chrome-cli-command.txt
Created February 17, 2021 23:41
Example Cloudflare Worker script to add preloads to the `<head>` of a page and enable priority hints
// add this command to the CLI to enable priority hints
--enable-experimental-web-platform-features
@Nooshu
Nooshu / tLzDTqLp_DIXfkYgNJgGugn-3s93RZHddkCbnbx3f5I.js
Last active September 1, 2021 09:08
Random request from Google...
(function() {
var c = function(n) {
return n
},
L = function(n, w, Z, C, B) {
if (!(C = (B = Z, E.trustedTypes), C) || !C.createPolicy) return B;
try {
B = C.createPolicy(w, {
createHTML: c,
createScript: c,