from Hacker News

Counting website hits using Cloudflare workers

by asadawadia on 7/3/23, 5:10 PM with 2 comments

  • by kentonv on 7/3/23, 5:26 PM

    Nice tutorial. One catch:

        // asynchronously make the request to the counter service
        // don't wait for this to finish before responding back to the client
        fetch("https://api/" + counterName, requestOptions)
    
    I would recommend wrapping this in `event.waitUntil()`, like:

        event.waitUntil(fetch("https://api/" + counterName, requestOptions))
    
    Otherwise, the Workers Runtime will cancel this call as soon as the top-level response is returned. That may not be causing any trouble here because the cancellation happens too late to actually have an effect (the cancellation would manifest as the HTTP connection to the counter API being closed, but by the time it notices it has probably already finished incrementing the counter). Still, it's a good idea to be safe add add the waitUntil().