Settings

Theme

Counting website hits using Cloudflare workers

blog.aawadia.dev

4 points by asadawadia 3 years ago · 2 comments

Reader

kentonv 3 years ago

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().

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection