Service Binding - Pages Function to Worker - Can't call path

I found this thread (below) and it’s the exact issue I’m having… but I don’t understand the solution in my context. I was hoping for some clarity.

Here’s the solution I found:

Here’s how I’m trying to use one of my Service Bindings.

I have a login function that checks a bunch of stuff… but if the user is in my user database, I immediately go to my app database to get their details.

I have a User Worker that handles all backend logic for anything related to the user and their user account.

So I get the userId and try to send the request via Service Binding like this:

getUserRequest(userId: string) {
    const url = `${BASE_WORKER_PATH}/get-user`;
    const auth = this.createAuthHeader(userId);
    return new Request(url, {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Basic ${auth}`,
      },
    });
  }

This is just a little utility function to create the Request.

I them take the returned request and go:

const getUserRes = await env.FWW_LIVE_USERS.fetch(getUserRequest);

BUT…

I know you can use paths according to the thread above. But without a path how would the request know to go to the correct handler function in my User Worker.

I feel like I’m missing something obvious but I’m stuck right now.

How can you create a custom Request and have it get to the Worker and the correct handler?


I’ve also tried setting up a RPC connection with via a new Service Binding and an entry point. But this does not work either because the sayHello function is not defined.

It is defined but I’m assuming this is the case because I need to setup the correct types.

const getUserRes = await env.FWW_LIVE_USERS_ADMIN.sayHello("Dan");

fetch (along with Request ) doesn’t accept paths, you need to provide a hostname (since it won’t be used with service bindings, you can provide anything as long as it’s valid)

So the prior solution said this.

But to me this made no sense at the time. I initially took it as leave the pathname out and only put in a hostname.

DON’T DO THAT

What he/she meant was… create a full url with the appropriate pathname that will invoke the handler in your receiving Worker.

The Service Binding ignores the hostname completely and only looks at the pathname.

So when you create a custom Request using new Request(...)

Make sure your URL is something like: https://HOSTNAME.EXTENSION/path1/path2/path3

Let me know if you have any questions but this solved the issue for me.

Thought I still can’t get my RPC implementation to work.

Try updating your compatibility date. Workers edited in the browser appear to automatically use the latest compatibility date, but anything published via wrangler (be that explicit usage, or Cloudflare Pages built in git integration) only uses a specified date. For pages, that seems to be the date the Pages instance was created. So, if you created the Pages instance before RPC came out and haven’t updated the compatibility date, the RPC feature essentially does not exist.

Hi @danboyle8637
Did you managed to get the RPC method to work between frontend and backend?
Are you using any framework for the frontend that uses the Cloudflare Pages adapter?

Some framework like Qwik for example will have a platform param that let you access the env before you can use it for RPC.

I’m also looking for a solution to this.