How to deploy worker?

I'm probably being a bit stupid here, but I'm really new to Workers, having been using page rules before (looking for a bit more flexibility on cookie bypass etc, and don't want to shell out for enterprise/business). I have a Worker script (pastebin for anyone interested: https://pastebin.com/5kUGaL2S) that I think does what I need (bypass on certain cookies etc), and I've tried to set it up, but I'm having a bit of difficulty getting it to actually work. Basically, I have a domain (domain.com, redacted in the script) and several subdomains (.domain.com, wildcard), and I need the worker to be the one to intercept the request and then pass it to the server if it can't be served from the cache. I've done the following: - saved and deployed - added a route for domain.com with the zone domain.com - added a route for *.domain.com with the zone domain.com, but it doesn't seem to be actually working (I can tell because my worker should be logging the start time to the console), am I being really stupid, or? Does anyone know what I'm doing wrong? Oddly enough, I can see 1.2k requests when I look at the worker?
7 Replies
Chaika
Chaika•16mo ago
A few things: Are you checking your browser console for the console.log("start!:", Date.now())? If so, that will never be there, it's logging in the worker, which has its own logs. From the Worker overview in the cf dash, you can go to Logs -> Real time logs -> Begin log stream and see the events and logs from your worker You also aren't ever calling processRequest on the fetch event listener
Original Authority
Original AuthorityOP•16mo ago
Ah, I was checking the browser console, wasn't sure where it logged 😮 Thanks for pointing that out, would here be an appropriate place to put it?
// process requests coming through
async function processRequest(originalRequest, event) {

// prepare
let {request, mobileAction, isMobile} = prepareRequest(originalRequest);
let url = new URL(request.url)


// only target pages
if ( url.pathname == '/' || (url.pathname).startsWith('/wiki/') || url.pathname == '/index.php' ) {
event.respondWith(processRequest(request, event));
}


// check if we should bypass, and why?
let {status, bypassCache} = shouldBypass(request);
// more.....
// process requests coming through
async function processRequest(originalRequest, event) {

// prepare
let {request, mobileAction, isMobile} = prepareRequest(originalRequest);
let url = new URL(request.url)


// only target pages
if ( url.pathname == '/' || (url.pathname).startsWith('/wiki/') || url.pathname == '/index.php' ) {
event.respondWith(processRequest(request, event));
}


// check if we should bypass, and why?
let {status, bypassCache} = shouldBypass(request);
// more.....
Chaika
Chaika•16mo ago
No.. out of curiosity and meaning no offense, are you using chatgpt/did chatgpt generate it? You might want to be careful if so, you're using old service worker format instead of ES Module format as well, which is a bit slower/older/not supported in all features: https://developers.cloudflare.com/workers/learning/migrate-to-module-workers/ In Service Workers, the addEventListener fetch is where all requests go through. Right now, you just do nothing with the request, you'd probably want something like:
addEventListener('fetch', event => {
console.log("start!:", Date.now())

// if we get an unknown error, skip the worker rather than throwing an error
event.passThroughOnException();

const request = event.request;

+ await processRequest(request, event);
});
addEventListener('fetch', event => {
console.log("start!:", Date.now())

// if we get an unknown error, skip the worker rather than throwing an error
event.passThroughOnException();

const request = event.request;

+ await processRequest(request, event);
});
There are a few open source/googable examples of doing this if you're in need of one, I don't see where you're returning a response either
Migrate to ES modules format · Cloudflare Workers docs
This guide will show you how to migrate your Workers from the Service Worker format to the ES modules format.
Original Authority
Original AuthorityOP•16mo ago
(No, it was not generated by CGPT). That didn't seem to work, I'll have a look at converting it.
Chaika
Chaika•16mo ago
If you tail it/view the logs, it should give you an error or some more information. I'm guessing it's because you're never returning a response
Original Authority
Original AuthorityOP•16mo ago
doesn't seem to throw any erorrs https://pastebin.com/XRSWCSMg (that's probably a bad example as a crawler)
Chaika
Chaika•16mo ago
oh it looks like service workers don't get upset and throw exceptions even if you don't return a response, they just pass through to origin. Module Workers would, haven't used service workers in a long time. Your problem with your code is just logical looks like, with !bypassCache, you just set the request to be made and continue on (should probably respond with the fetch(request) at that point), you then try to access response before it is set, you also never use the rewriter you make
Want results from more Discord servers?
Add your server