Await fetch() is no longer working properly for Remix loader on CF pages

On a site on Cloudflare Pages that has been live for many months, the fetch() call to a graphql persisted query endpoint (a plain GET to return JSON) now MOSTLY seems to error, but sometimes returns the correct data.

fetch always works running locally
no code changes to the Remix app for months
CF Pages app uses Unbound functions
changing the CF pages build version or compatibilty date doesn’t affect issue
The CMS that serves the data has had updates, but loading the data uri in a browser always works, always returns status 200, usually in around 200-500ms

Code:

export let loader: LoaderFunction = async ({ params, request, context }) => {
const uri = params.page ? ‘/’ + params.page : ‘/’;

let data:any = {};
let res = null

try {
  res = await fetch(getGraphqlEndpoint(context.CF_ENV) + '?queryId=page&variables={"path":"' + uri + '"}')
} catch (e) {
   data.error = e;
}

if (res && res.ok) {
data = await res.json();
data = data[‘data’];
}
// for logging
data.response = res

return json(data, {
status: data.page ? 200 : 404,
});
};

If I console.log the data in the Remix default function from useLoaderData, and I can see that

  1. the data obj contains a response key which is an empty object. I think this means it didn’t catch an error, but also the response was empty.
  2. if I refresh 2 or 5 or 10 times, I’ll eventually get the correctly fetched json data logged and the page will load.

Any suggestions about what might be wrong with my use of fetch here, and a better approach?
I’ve tried changing to using ky with 25 retries for the data request, but I get the exact same result, except I can never actually get the data load after any number of manual reloads.

I feel like await isn’t working properly inside this async function on Cloudflare Pages anymore.

To be clear, this issue only occurs on the deployed app, not when running locally with Remix’s wrangler-based emulator.

Have checked this isn’t an issue with Remix itself—e.g. have you started building with a newer version that has broken something?

@remix-run/[email protected] was last published around 20 hours ago (as I type) so this might have introduced a bug. Try reverting to a previous version, and perhaps pin that version if it works.

“No code changes to Remix app in months”

I have three Remix sites running on Pages. This one Remix 1.6, and another Remix 1.13, both use fetch in the same way, have not had any code changes in some time, and have started showing the exact same problem. The third site has Remix 1.9, and does NOT have the error, but it doesn’t use fetch. Instead it uses Apollo Graphql client to send the graphql, rather than fetching a GET uri to trigger the origin’s persisted query. I would like to figure out the fetch problem rather than convert the two erroring sites to use the Apollo method. Some small progress: I have updated my local site to the latest Remix 1.19 and ALSO wrangler to 3.5, and now I can reproduce the problem locally. So I’m pretty certain that the problem is some change that happened in Cloudflare Pages functions execution that caused my existing code to not be compatible. Hopefully I can sort it out locally now.