Skip to content

Commit

Permalink
fix: pass routes to dev session (#1213)
Browse files Browse the repository at this point in the history
We can pass routes when creating a `dev` session. The effect of this is when you visit a path that _doesn't_ match the given routes, then it instead does a fetch from the deployed worker on that path (if any). We were previously passing `*/*`, i.e, matching _all_ routes in dev; this fix now passes configured routes instead.
  • Loading branch information
threepointone committed Jun 28, 2022
1 parent ee6b413 commit 1bab3f6
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/curly-bulldogs-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: pass `routes` to `dev` session

We can pass routes when creating a `dev` session. The effect of this is when you visit a path that _doesn't_ match the given routes, then it instead does a fetch from the deployed worker on that path (if any). We were previously passing `*/*`, i.e, matching _all_ routes in dev; this fix now passes configured routes instead.
21 changes: 21 additions & 0 deletions packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@ describe("wrangler dev", () => {
});
});

describe("routes", () => {
it("should pass routes to <Dev/>", async () => {
fs.writeFileSync("index.js", `export default {};`);

// config.routes
mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
writeWranglerToml({
main: "index.js",
routes: ["http://5.some-host.com/some/path/*"],
});
await runWrangler("dev");
expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
expect.objectContaining({
host: "5.some-host.com",
zone: "some-zone-id-5",
routes: ["http://5.some-host.com/some/path/*"],
})
);
});
});

describe("host", () => {
it("should resolve a host to its zone", async () => {
writeWranglerToml({
Expand Down
16 changes: 15 additions & 1 deletion packages/wrangler/src/create-worker-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,21 @@ async function createPreviewToken(
: `/accounts/${accountId}/workers/scripts/${scriptId}/edge-preview`;

const mode: CfPreviewMode = ctx.zone
? { routes: ["*/*"] } // TODO: should we support routes here? how?
? {
routes: ctx.routes
? // extract all the route patterns
ctx.routes.map((route) => {
if (typeof route === "string") {
return route;
}
if (route.custom_domain) {
return `${route.pattern}/*`;
}
return route.pattern;
})
: // if there aren't any patterns, then just match on all routes
["*/*"],
}
: { workers_dev: true };

const formData = createWorkerUploadForm(worker);
Expand Down
10 changes: 6 additions & 4 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from "./index";

import type { Config } from "./config";
import type { Route } from "./config/environment";
import type { Argv, ArgumentsCamelCase } from "yargs";

interface DevArgs {
Expand Down Expand Up @@ -296,24 +297,24 @@ export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
// Compute zone info from the `host` and `route` args and config;
let host = args.host || config.dev.host;
let zoneId: string | undefined;
const routes: Route[] | undefined =
args.routes || (config.route && [config.route]) || config.routes;

if (!args.local) {
if (host) {
zoneId = await getZoneIdFromHost(host);
}
const routes = args.routes || config.route || config.routes;
if (!zoneId && routes) {
const firstRoute = Array.isArray(routes) ? routes[0] : routes;
const firstRoute = routes[0];
const zone = await getZoneForRoute(firstRoute);
if (zone) {
zoneId = zone.id;
host = zone.host;
}
}
} else if (!host) {
const routes = args.routes || config.route || config.routes;
if (routes) {
const firstRoute = Array.isArray(routes) ? routes[0] : routes;
const firstRoute = routes[0];
host = getHostFromRoute(firstRoute);
}
}
Expand Down Expand Up @@ -415,6 +416,7 @@ export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
env={args.env}
zone={zoneId}
host={host}
routes={routes}
rules={getRules(config)}
legacyEnv={isLegacyEnv(config)}
minify={args.minify ?? config.minify}
Expand Down
3 changes: 3 additions & 0 deletions packages/wrangler/src/dev/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Local } from "./local";
import { Remote } from "./remote";
import { useEsbuild } from "./use-esbuild";
import type { Config } from "../config";
import type { Route } from "../config/environment";
import type { Entry } from "../entry";
import type { AssetPaths } from "../sites";
import type { CfWorkerInit } from "../worker";
Expand Down Expand Up @@ -52,6 +53,7 @@ export type DevProps = {
legacyEnv: boolean;
zone: string | undefined;
host: string | undefined;
routes: Route[] | undefined;
};

export function DevImplementation(props: DevProps): JSX.Element {
Expand Down Expand Up @@ -188,6 +190,7 @@ function DevSession(props: DevSessionProps) {
legacyEnv={props.legacyEnv}
zone={props.zone}
host={props.host}
routes={props.routes}
/>
);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/wrangler/src/dev/remote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { logger } from "../logger";
import { usePreviewServer } from "../proxy";
import { syncAssets } from "../sites";
import { ChooseAccount, getAccountChoices, requireApiToken } from "../user";
import type { Route } from "../config/environment";
import type { CfPreviewToken } from "../create-worker-preview";
import type { AssetPaths } from "../sites";
import type { ChooseAccountItem } from "../user";
Expand All @@ -34,6 +35,7 @@ export function Remote(props: {
legacyEnv: boolean | undefined;
zone: string | undefined;
host: string | undefined;
routes: Route[] | undefined;
}) {
const [accountId, setAccountId] = useState(props.accountId);
const accountChoicesRef = useRef<Promise<ChooseAccountItem[]>>();
Expand All @@ -56,6 +58,7 @@ export function Remote(props: {
legacyEnv: props.legacyEnv,
zone: props.zone,
host: props.host,
routes: props.routes,
});

usePreviewServer({
Expand Down Expand Up @@ -125,6 +128,7 @@ export function useWorker(props: {
legacyEnv: boolean | undefined;
zone: string | undefined;
host: string | undefined;
routes: Route[] | undefined;
}): CfPreviewToken | undefined {
const {
name,
Expand Down Expand Up @@ -230,6 +234,7 @@ export function useWorker(props: {
legacyEnv: props.legacyEnv,
zone: props.zone,
host: props.host,
routes: props.routes,
},
abortController.signal
)
Expand Down Expand Up @@ -277,6 +282,7 @@ export function useWorker(props: {
props.legacyEnv,
props.zone,
props.host,
props.routes,
]);
return token;
}
1 change: 1 addition & 0 deletions packages/wrangler/src/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export async function previewHandler(args: ArgumentsCamelCase<PreviewArgs>) {
env={args.env}
zone={undefined}
host={undefined}
routes={undefined}
legacyEnv={isLegacyEnv(config)}
build={config.build || {}}
define={config.define}
Expand Down
6 changes: 4 additions & 2 deletions packages/wrangler/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Route } from "./config/environment";
import type { ApiCredentials } from "./user";

/**
* A Cloudflare account.
*/
import type { ApiCredentials } from "./user";

export interface CfAccount {
/**
* An API token.
Expand Down Expand Up @@ -180,4 +181,5 @@ export interface CfWorkerContext {
legacyEnv: boolean | undefined;
zone: string | undefined;
host: string | undefined;
routes: Route[] | undefined;
}

0 comments on commit 1bab3f6

Please sign in to comment.