divby0
divby0
Explore posts from servers
CDCloudflare Developers
Created by divby0 on 4/5/2024 in #workers-help
How does the caches.default interact with fetch by default?
No description
1 replies
CDCloudflare Developers
Created by divby0 on 10/9/2023 in #pages-help
Make stack trace use the provided sourcemap (single file, [[path]].js + .map)
I have configured a build that yields a functions/[[path]].js and a functions/[[path]].js.map file. How do I make cloudflare actually use the sourcemap so I can read and unsterand them when an error happens? Currently for my small project I am just hitting a webhook with the stacktrace that sends the stacktrace as a telegram to me but most of the time they are not really readable.
2 replies
CDCloudflare Developers
Created by divby0 on 7/3/2023 in #workers-help
Reverse Proxy for blog returns 301 on custom domain, but works correct on preview url?
I've got the domain rank-hub.com added as a custom domain for a worker and a blog lives on ghost.rank-hub.com. The worker script creates a proxy request and returns its result so I can have rank-hub.com/blog proxied to ghost.rank-hub.com/blog. This is very easy with workers and I appreciate it, but I am probably missing a little something as the setup works on the preview url: https://rank-hub-ghost-proxy.divby0.workers.dev/blog but not on the live one https://rank-hub.com/blog Somehow cloudflare returns a 301 to my request when I use the apex domain (so I get redirected to blog.rank-hub.com). When I use the preview url, the content is correctly proxied. What am I missing? Worker script:
export default {
async fetch(request, env, ctx) {
try {

const urlObject = new URL(request.url);

// If the request is to the Ghost subdirectory
if (/^\/blog/.test(urlObject.pathname)) {
// Then Proxy to Ghost
const GHOST_URL = "ghost.rank-hub.com";
const CUSTOM_URL = "rank-hub.com";

let url = new URL(request.url);

url.hostname = GHOST_URL;

let proxyRequest = new Request(url, request);

proxyRequest.headers.set('Host', GHOST_URL);

//Have an X-Forwarded-Host header that matches the custom domain in my.ghost.org.

proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);

//Include the X-Forwarded-Proto header set to https not http.

proxyRequest.headers.set("X-Forwarded-Proto", "https");

//Include the X-Forwarded-For header, populated with the remote IP of the original request.

let ip = proxyRequest.headers.get("CF-Connecting-IP");

proxyRequest.headers.set("X-Forwarded-For", ip);

return await fetch(proxyRequest);
}

} catch (error) {
return await fetch(request);
}

return await fetch(request);
},
};
export default {
async fetch(request, env, ctx) {
try {

const urlObject = new URL(request.url);

// If the request is to the Ghost subdirectory
if (/^\/blog/.test(urlObject.pathname)) {
// Then Proxy to Ghost
const GHOST_URL = "ghost.rank-hub.com";
const CUSTOM_URL = "rank-hub.com";

let url = new URL(request.url);

url.hostname = GHOST_URL;

let proxyRequest = new Request(url, request);

proxyRequest.headers.set('Host', GHOST_URL);

//Have an X-Forwarded-Host header that matches the custom domain in my.ghost.org.

proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);

//Include the X-Forwarded-Proto header set to https not http.

proxyRequest.headers.set("X-Forwarded-Proto", "https");

//Include the X-Forwarded-For header, populated with the remote IP of the original request.

let ip = proxyRequest.headers.get("CF-Connecting-IP");

proxyRequest.headers.set("X-Forwarded-For", ip);

return await fetch(proxyRequest);
}

} catch (error) {
return await fetch(request);
}

return await fetch(request);
},
};
16 replies
CDCloudflare Developers
Created by divby0 on 6/12/2023 in #workers-help
HTMLRewriter treats self closing tags like <img/> as text
I am using HTMLRewriter to get the plain text of a website. Using the following code listening to the text event, it results in uggly <img ....../> or <iframe .../> being included in my result:
let total = "";

const rewriter = new HTMLRewriter()
.on(`article`, {
text(text: Text): void | Promise<void> {
total += text.text;
}
})
;
let total = "";

const rewriter = new HTMLRewriter()
.on(`article`, {
text(text: Text): void | Promise<void> {
total += text.text;
}
})
;
After transforming an html response, this is how response can look like:
<iframe class="hide_iframe_" src="[...]"></iframe><img class="hide_iframe" height="1" width="1" alt="" src="[...]"/> Actual normal text <img src="[...]" alt="..." placeholder="blur" /> ...rest of the article
<iframe class="hide_iframe_" src="[...]"></iframe><img class="hide_iframe" height="1" width="1" alt="" src="[...]"/> Actual normal text <img src="[...]" alt="..." placeholder="blur" /> ...rest of the article
1 replies