Snippet failing with 'exceededSnippetSubrequests'

We've been using snippets for a while to override frontend requests to fetch different assets, but we're now looking at doing this based on config from our API (with heavy caching on the responses).

I understand there's a limit of 2 subrequests for the Pro plan and that each redirect counts as a subrequest, but I still don't understand why this is triggering the exception. Upon testing, I don't hit any redirects and I've also set redirect: 'manual' to avoid this too.

To debug, I've been wrapping the snippet body in a try ... catch and printing out with console.error, and I see some that the document requests are successful, but intermittently, assets such as .js and .css files trigger this exception. But hitting the rewritten path directly (eg. curl -s -o /dev/null -D - ...) shows there's no redirects and it's just a 200 response, meaning I should be within the 2 sub-request limit.

export default {
  /**
   * @param {Request} request 
   * @returns {Promise<Response>}
   */
  async fetch(request) {
    // sub-request 1
    const config = fetch(`https://some.api.endpoint/...`, {
      headers: request.headers,
      redirect: 'manual',
    })

    // parse `config` as json to extract frontend version to route to 
    const origin = "https://foo.bar.pages.dev"

    const queryParams = url.searchParams.size !== 0 ? `?${url.searchParams}` : ``
    const frontendUrl = `${origin}${url.pathname}${queryParams}`

    // subrequest 2
    const frontendResponse = await fetch(frontendUrl, {
      headers: request.headers,
      redirect: 'manual',
    })
    const response = new Response(frontendResponse.body, frontendResponse)

    // add headers, etc.

    return response;
  }
}
Was this page helpful?