Skip to content

Commit

Permalink
fix: bindings to named entrypoints on workers with assets (#8274)
Browse files Browse the repository at this point in the history
* don't point to router worker if named entrypoint

* add test

* changeset
  • Loading branch information
emily-shen authored Feb 27, 2025
1 parent 23be4f4 commit fce642d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-bears-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"miniflare": patch
"wrangler": patch
---

fix bindings to entrypoints on the same worker in workers with assets
14 changes: 14 additions & 0 deletions fixtures/workers-with-assets/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { WorkerEntrypoint } from "cloudflare:workers";

export interface Env {
// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
ASSETS: Fetcher;
NAMED: Fetcher<NamedEntrypoint>;
}

export default {
Expand All @@ -9,10 +12,21 @@ export default {
if (url.pathname === "/assets-binding") {
return await env.ASSETS.fetch(new URL("binding.html", request.url));
}

if (url.pathname === "/named-entrypoint") {
const res = await env.NAMED.sayHello();
return new Response(res);
}
// 404s from the Asset Worker will return this:
return new Response(
"There were no assets at this route! Hello from the user Worker instead!" +
`\n${new Date()}`
);
},
};

export class NamedEntrypoint extends WorkerEntrypoint {
async sayHello() {
return "hello from a named entrypoint";
}
}
9 changes: 9 additions & 0 deletions fixtures/workers-with-assets/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,13 @@ describe("[Workers + Assets] dynamic site", () => {
);
expect(text).toContain("<h1>✨This is from a user Worker binding✨</h1>");
});

it("should be able to use a binding to a named entrypoint", async ({
expect,
}) => {
let response = await fetch(`http://${ip}:${port}/named-entrypoint`);
let text = await response.text();
expect(response.status).toBe(200);
expect(text).toContain("hello from a named entrypoint");
});
});
9 changes: 7 additions & 2 deletions fixtures/workers-with-assets/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
name = "worker-with-assets"
main = "src/index.ts"
compatibility_date = "2024-01-01"
compatibility_date = "2024-12-12"

[assets]
directory = "./public"
binding="ASSETS"
experimental_serve_directly = true
run_worker_first = false

[[services]]
binding = "NAMED"
service = "worker-with-assets"
entrypoint ="NamedEntrypoint"
2 changes: 1 addition & 1 deletion packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ export class Miniflare {
(worker) =>
worker.core.name === targetWorkerName && worker.assets.assets
);
if (maybeAssetTargetService) {
if (maybeAssetTargetService && !binding.service?.entrypoint) {
assert(binding.service?.name);
binding.service.name = `${ROUTER_SERVICE_NAME}:${targetWorkerName}`;
}
Expand Down

0 comments on commit fce642d

Please sign in to comment.