Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/fix-vite-dev-miniflare-zone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

Pass the local dev zone through `unstable_getMiniflareWorkerOptions`

When `@cloudflare/vite-plugin` asks Wrangler for Miniflare worker options, the returned object did not include the inferred `zone`. Miniflare then fell back to `<worker>.example.com` for the `CF-Worker` header, which differs from the `wrangler dev --local` path and can cause some upstreams to reject local subrequests.

This now forwards the same `dev.host` or inferred route host that Wrangler already uses in local dev so Vite-backed local development matches the existing local runtime behavior.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test (and probably some handling in the code) for the case where the host cannot be inferred? e.g. routes: "*/*".

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, it } from "vitest";
import { unstable_getMiniflareWorkerOptions } from "../api";
import type { Config } from "@cloudflare/workers-utils";

function makeConfig(
overrides: Partial<{
dev: { host?: string; enable_containers?: boolean; container_engine?: string };
route: string;
routes: string[];
}> = {}
): Config {
return {
name: "test-worker",
main: "./src/index.ts",
compatibility_date: "2025-06-17",
compatibility_flags: [],
rules: [],
queues: { producers: [], consumers: [] },
migrations: [],
tail_consumers: undefined,
streaming_tail_consumers: undefined,
containers: [],
assets: undefined,
define: {},
bindings: [],
compliance_region: undefined,
dev: {
ip: "localhost",
port: undefined,
local_protocol: "http",
upstream_protocol: "http",
enable_containers: false,
...overrides.dev,
},
...overrides,
} as unknown as Config;
Comment on lines +27 to +36

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 makeConfig spread ordering causes dev defaults to be silently overwritten

In the makeConfig helper, when overrides.dev is provided, the dev defaults (ip, port, local_protocol, upstream_protocol, enable_containers) are first correctly merged via ...overrides.dev inside the dev object (line 33), but then immediately overwritten by ...overrides at the top level (line 35). Because overrides contains the key dev: { host: "example.workers.dev" }, the top-level spread replaces the entire merged dev with just { host: "example.workers.dev" }, discarding all defaults.

The first test passes only by coincidence—getMiniflareZone only reads config.dev.host and returns early. But any future test that supplies overrides.dev and relies on the default dev properties (e.g., enable_containers) further down in unstable_getMiniflareWorkerOptions (packages/wrangler/src/api/integrations/platform/index.ts:522) will silently get undefined values instead of the intended defaults.

Suggested change
dev: {
ip: "localhost",
port: undefined,
local_protocol: "http",
upstream_protocol: "http",
enable_containers: false,
...overrides.dev,
},
...overrides,
} as unknown as Config;
dev: {
ip: "localhost",
port: undefined,
local_protocol: "http",
upstream_protocol: "http",
enable_containers: false,
...overrides.dev,
},
...overrides,
dev: {
ip: "localhost",
port: undefined,
local_protocol: "http",
upstream_protocol: "http",
enable_containers: false,
...overrides.dev,
},
} as unknown as Config;
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

}

describe("unstable_getMiniflareWorkerOptions", () => {
it("forwards dev.host as the Miniflare zone", ({ expect }) => {
const { workerOptions } = unstable_getMiniflareWorkerOptions(
makeConfig({
dev: { host: "example.workers.dev" },
})
);

expect(workerOptions.zone).toBe("example.workers.dev");
});

it("infers the Miniflare zone from the first route when dev.host is unset", ({
expect,
}) => {
const { workerOptions } = unstable_getMiniflareWorkerOptions(
makeConfig({
routes: ["https://subdomain.example.com/*"],
})
);

expect(workerOptions.zone).toBe("subdomain.example.com");
});
});
14 changes: 13 additions & 1 deletion packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getAssetsOptions } from "../../../assets";
import { readConfig } from "../../../config";
import { partitionDurableObjectBindings } from "../../../deployment-bundle/entry";
import { DEFAULT_MODULE_RULES } from "../../../deployment-bundle/rules";
import { getBindings } from "../../../dev";
import { getBindings, getInferredHost } from "../../../dev";
import { getDurableObjectClassNameToUseSQLiteMap } from "../../../dev/class-names-sqlite";
import {
buildAssetOptions,
Expand Down Expand Up @@ -349,6 +349,17 @@ export interface Unstable_MiniflareWorkerOptions {
externalWorkers: WorkerOptions[];
}

function getMiniflareZone(config: Config): string | undefined {
if (config.dev.host) {
return config.dev.host;
}

return getInferredHost(
(config.route && [config.route]) || config.routes,
config.configPath
);
}

export function unstable_getMiniflareWorkerOptions(
configPath: string,
env?: string,
Expand Down Expand Up @@ -461,6 +472,7 @@ export function unstable_getMiniflareWorkerOptions(
compatibilityDate: config.compatibility_date,
compatibilityFlags: config.compatibility_flags,
modulesRules,
zone: getMiniflareZone(config),
containerEngine: useContainers
? (config.dev.container_engine ?? resolveDockerHost(getDockerPath()))
: undefined,
Expand Down
Loading