Is this Worker adding delay to the request?

For those experienced with Workers. Is this Worker adding delay to the request? Here’s my code:

addEventListener("fetch", (event) => {
  event.respondWith(
    handleRequest(event.request).catch(
      (err) => new Response(err.stack, { status: 500 })
    )
  );
});

async function handleRequest(request) {
  const response = await fetch(request);
  if ( request.headers.get('accept').includes('text/html') || request.headers.get('accept').includes('application/json') ) {
    await fetch('https://myapi.com/send', {
      method: 'POST',
      body: JSON.stringify({
        ip: request.headers.get('cf-connecting-ip'),
        number: request.cf?.asn,
        name: request.cf?.asOrganization,
        country: request.cf?.country,
        protocol: request.cf?.httpProtocol,
        code: response.status,
        user_agent: request.headers.get('user-agent'),
        api_key: 'my_api_key'
      }),
      headers: {
        accept: 'application/json',
        'content-type': 'application/json'
      }
    });
  }
  return response;
}

The idea is to avoid having delays when processing the request, but I’m not sure if that’s the case, specially when I am sending the “response.status” to the API.

1 Like

Yes, since you’re waiting for your API to respond before returning the response to the user.

Use waitUntil for a non-blocking way to make another request without blocking the response.

2 Likes

Ok, thanks for the suggestion! If I implement the “waitUntil” to not wait for the API response, will the “response.status” param be an issue in terms of delays? Because that’s something that comes on the response, not on the request, and to my understanding, that could cause some delay, because the response is needed to process the data.

The flow of requests will look like this:

image

export default {
    async fetch(req, env, ctx) {
        try {
            const response = await fetch(req); // the Worker won't continue past this point until the origin responds
            if (req.headers.get('accept').includes('text/html') || req.headers.get('accept').includes('application/json')) {
                ctx.waitUntil(
                    fetch('https://myapi.com/send', {
                        method: 'POST',
                        body: JSON.stringify({
                            ip: req.headers.get('cf-connecting-ip'),
                            number: req.cf?.asn,
                            name: req.cf?.asOrganization,
                            country: req.cf?.country,
                            protocol: req.cf?.httpProtocol,
                            code: response.status, // so response.status is available by the time we get to the point of putting this promise into waitUntil
                            user_agent: req.headers.get('user-agent'),
                            api_key: 'my_api_key'
                        }),
                        headers: {
                            accept: 'application/json',
                            'content-type': 'application/json'
                        }
                    });
            )
            }
            return response;
        } catch (e) {
            return new Response(e.stack, {status: 500})
        }
    }
}
2 Likes