I note that each time I make a GET/POST request to fetch external data in a Worker, two requests are made. An initial request, quickly followed by a subrequest immediately after.
The issue is that I’m making an external API request to trigger document generation and I don’t want to make a duplicate request. Is there any way to avoid this?
Oddly enough, I note that a subrequest is only made when loading the URL in a user agent (I’m using a web browser). That is, if I open make the request from the web interface (below), only a single request is made.
Here’s some code to reproduce this behavior:
const baseUrl = 'https://hookb.in/'
const url = baseUrl + 'G9O31lm1LqcWGGeQq13l'
async function gatherResponse(response) {
return await response.text()
}
async function handleRequest() {
const init = {
headers: {
'Content-Type': 'application/json'
}
}
const responses = await Promise.all([
await fetch(url, init)
// will add others here
])
const results = await Promise.all([
gatherResponse(responses[0])
])
return new Response(results.join(), init)
}
addEventListener('fetch', event => {
return event.respondWith(handleRequest())
})