R2 backend functions ignore editor environment selector in published and iframe-hosted modes

Has anyone else run into this on R2 React apps?

Setup: we have a Postgres resource with separate staging
and production environments. We're testing booking flows that
chain into Supabase edge functions.

The behavior we're seeing:

  • I flip the editor's env pill from production → staging
  • Sometimes the next backend call still routes to production
    resources (we can tell because our resource returns the
    PROD Postgres cluster's system_identifier instead of staging's)
  • Sometimes a full page reload fixes it. Sometimes it doesn't.
  • It seems most consistent when I open the app initially in
    production and then switch to staging — the switch doesn't
    fully take.

Confounding things:

  • execute_backend_code (via the agent tool) ALWAYS picks up the
    env selector correctly. It's only the app's actual backend
    function calls that sometimes don't.
  • I peeked at useBackendFunction.ts (the system hook) and
    noticed it builds executeV2 payloads with
    environment: 'production' hardcoded, then branches by mode:
    plain editor sandbox strips it out; published and
    iframe-hosted paths send it through.

Could that be the
mechanism? Not sure if I'm reading it right.

What we did as a workaround:
We added a tiny probe inside our env-sensitive handlers that
runs SELECT system_identifier FROM pg_control_system()
against our resource and maps known cluster IDs → env name. The
function then decides based on the cluster, not the FE-sent
value. It self-corrects to whichever resource the executor
actually bound, which is fine for our flows but it's a
band-aid — we can't surface "you THINK you're on staging
but you're not" to the user before they hit submit.

Anyone seen the prod-→-staging stickiness before? Is there a
documented way to force the editor to re-bind resources after
switching the env pill? Any pointers welcome.

Thanks for flagging, @CCM! Just to be clear, published apps should only ever run queries against production resources. The environment toggle is only for the editor IDE, but there certainly shouldn't be any latency or stickiness.

I'll try to reproduce the behavior you're describing and identify potential root causes. That said, we are actively working on a variety of issues related to resource environments that might very well resolve this issue without the need for an additional ticket. I'll let you know what I find!