Too many subrequests error when fetching data

I have a Nuxt 3 app that makes a POST request from my frontend to my backend and generates an AI image use Replicate’s API. I am hosting on Cloudflare Pages free tier

    const {data} =  await useFetch('/api/runPrediction', {
        method: 'POST',
        body: bodyData,
        timeout: 180000  // 3 minutes
        })

I was previously using $fetch which would often timeout as the response from Replicate can take well over 30 seconds up to 3 minutes. I switched to useFetch which allows me to set a max timeout.
This solved me timeout issue, but only on localhost. It now waits up to 3 minutes for a response before timing out.
However, when pushed to production, it doesn’t work. Any response that takes under 30seconds returns to the client fine, but when it goes about 40 or so seconds, it throws a ‘Too many subrequests error’.

{
“message”: “Error: Too many subrequests.”,
“details”: “Error: Too many subrequests.\n at chunks/index2.mjs:1:379125\n at chunks/index2.mjs:1:379545\n at Generator.next ()\n at fulfilled (chunks/index2.mjs:1:378667)”,
“hint”: “”,
“code”: “”
}

Even when the response takes over 30 seconds, it returns to the server succesfully, but doesn’t make it to the client. This makes me assume that the issue is indeed with the POST request timing out.

I don’t think I’m actually making “Too many subrequests”, I do chain quite a few requests in my endpoint, but at any given moment theres only about 2-3 deliberate, concurrent POST/GET requests.

I tried to switch to websockets and SSE respectively but I’ve had a lot of problems setting them up, would appreciate some insight into fixing the POST timeout issue specifically.

Is useFetch possibly retrying and causing the too many subrequests (or maybe the other fetches?)? I’m not aware of a timeout causing that error

You’re able to do up to 50 subrequests an invocation on the free plan (ref).

I wouldn’t expect a timeout around 40 seconds though, it should timeout after 100 seconds (as far as I’m aware)

1 Like

I don’t think it’s retrying as I don’t see the other requests apart from the original that throws an error.

I’m doing a workaround by just polling Replicate’s API rather than leaving the POST request open, but I would say I haven’t fixed the error.

Is there a way to see how many subrequests are made per request on the dashboard somewhere?