How to use service bindings in Remix?
# workers-help
t
Could anyone give me some guidance on how to use a worker sevice binding locally from within a remix cloudflare pages or (if pages is not possible) worker project? I added the binding to the wrangler.toml like so
Copy code
services = [
  { binding = "GATEWAY", service = "gateway-main" }
]
When I locally start the remix project, I see the following output in console
Copy code
▲ [WARNING] This worker is bound to live services: GATEWAY (gateway-main)


Your worker has access to the following bindings:
- Services:
  - GATEWAY: gateway-main
▲ [WARNING] ⎔ Support for service bindings in local mode is experimental and may change.
But I have no idea how to access the binding. The docs make it seem like I can do context.env.GATEWAY or context.GATEWAY in my loader functions - I can't. I also tried looking at the context parameter on server.ts
Copy code
export const onRequest = createPagesFunctionHandler({
    build,
    getLoadContext: (context) => {
        console.log(context);

        return context.env;
    },
    mode: process.env.NODE_ENV,
});
But the context object only has an ASSETS Fetcher on its env parameter.
Copy code
{
  ...
  functionPath: '/',
  next: [AsyncFunction: next],
  params: [Object: null prototype] {},
  data: {},
  env: { ASSETS: Fetcher {} },
  waitUntil: [Function: bound waitUntil],
  passThroughOnException: [Function: passThroughOnException]
}
I'm a bit very stuck with this and would like advice. Thank you!
Answering my own question, I got the local service binding working by running wrangler dev and wrangler pages dev with the --local flag. I find this a bit weird, because the --help of wrangler pages dev mentions that the default for --local is true?
Copy code
node ➜ /workspaces/monorepo/apps/web-pages (main) $ pnpm exec wrangler pages dev --help
wrangler pages dev [directory] [-- command..]

🧑‍💻 Develop your full-stack Pages application locally

Positionals:
  directory  The directory of static assets to serve  [string]
  command    The proxy command to run  [string]

Flags:
  -j, --experimental-json-config  Experimental: Support wrangler.json  [boolean]
  -e, --env                       Environment to use for operations and .env files  [string]
  -h, --help                      Show help  [boolean]
  -v, --version                   Show version number  [boolean]

Options:
      --local                                      Run on my machine  [boolean] [default: true]
      ...
      --log-level                                  Specify logging level  [choices: "debug", "info", "log", "warn", "error", "none"]

🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose