#How to deploy worker?

8 messages · Page 1 of 1 (latest)

pliant dome
#

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:

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?

amber viper
# pliant dome I'm probably being a bit stupid here, but I'm really new to Workers, having been...

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

pliant dome
#

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.....
amber viper
#

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);
});

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

This guide will show you how to migrate your Workers from the Service Worker format to the ES modules format.

pliant dome
#

(No, it was not generated by CGPT).

That didn't seem to work, I'll have a look at converting it.

amber viper
#

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

pliant dome
#

(that's probably a bad example as a crawler)