Skip to content

Commit 91d8994

Browse files
colecrouterpetebacondarwin
authored andcommitted
fix: do not merge routes with different methods when computing pages routes
Fixes #92
1 parent 747c65a commit 91d8994

File tree

3 files changed

+113
-47
lines changed

3 files changed

+113
-47
lines changed

.changeset/hungry-pugs-relate.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: do not merge routes with different methods when computing pages routes
6+
7+
Fixes #92

packages/wrangler/pages/functions/filepath-routing.test.ts

+103-46
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,117 @@
1+
import { writeFileSync } from "fs";
2+
import { runInTempDir } from "../../src/__tests__/helpers/run-in-tmp";
13
import { toUrlPath } from "../../src/paths";
2-
import { compareRoutes } from "./filepath-routing";
4+
import { compareRoutes, generateConfigFromFileTree } from "./filepath-routing";
5+
import type { UrlPath } from "../../src/paths";
36
import type { HTTPMethod, RouteConfig } from "./routes";
47

5-
describe("compareRoutes()", () => {
6-
test("routes / last", () => {
7-
expect(
8-
compareRoutes(routeConfig("/"), routeConfig("/foo"))
9-
).toBeGreaterThanOrEqual(1);
10-
expect(
11-
compareRoutes(routeConfig("/"), routeConfig("/:foo"))
12-
).toBeGreaterThanOrEqual(1);
13-
expect(
14-
compareRoutes(routeConfig("/"), routeConfig("/:foo*"))
15-
).toBeGreaterThanOrEqual(1);
16-
});
8+
describe("filepath-routing", () => {
9+
describe("compareRoutes()", () => {
10+
test("routes / last", () => {
11+
expect(
12+
compareRoutes(routeConfig("/"), routeConfig("/foo"))
13+
).toBeGreaterThanOrEqual(1);
14+
expect(
15+
compareRoutes(routeConfig("/"), routeConfig("/:foo"))
16+
).toBeGreaterThanOrEqual(1);
17+
expect(
18+
compareRoutes(routeConfig("/"), routeConfig("/:foo*"))
19+
).toBeGreaterThanOrEqual(1);
20+
});
1721

18-
test("routes with fewer segments come after those with more segments", () => {
19-
expect(
20-
compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar"))
21-
).toBeGreaterThanOrEqual(1);
22-
expect(
23-
compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar/cat"))
24-
).toBeGreaterThanOrEqual(1);
25-
});
22+
test("routes with fewer segments come after those with more segments", () => {
23+
expect(
24+
compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar"))
25+
).toBeGreaterThanOrEqual(1);
26+
expect(
27+
compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar/cat"))
28+
).toBeGreaterThanOrEqual(1);
29+
});
2630

27-
test("routes with wildcard segments come after those without", () => {
28-
expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/foo"))).toBe(1);
29-
expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/:foo"))).toBe(1);
30-
});
31+
test("routes with wildcard segments come after those without", () => {
32+
expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/foo"))).toBe(1);
33+
expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/:foo"))).toBe(
34+
1
35+
);
36+
});
3137

32-
test("routes with dynamic segments come after those without", () => {
33-
expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1);
34-
});
38+
test("routes with dynamic segments come after those without", () => {
39+
expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1);
40+
});
3541

36-
test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => {
37-
expect(
38-
compareRoutes(routeConfig("/foo/:id/bar"), routeConfig("/foo/bar/:id"))
39-
).toBe(1);
40-
});
42+
test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => {
43+
expect(
44+
compareRoutes(routeConfig("/foo/:id/bar"), routeConfig("/foo/bar/:id"))
45+
).toBe(1);
46+
});
4147

42-
test("routes with no HTTP method come after those specifying a method", () => {
43-
expect(compareRoutes(routeConfig("/foo"), routeConfig("/foo", "GET"))).toBe(
44-
1
45-
);
46-
});
48+
test("routes with no HTTP method come after those specifying a method", () => {
49+
expect(
50+
compareRoutes(routeConfig("/foo"), routeConfig("/foo", "GET"))
51+
).toBe(1);
52+
});
53+
54+
test("two equal routes are sorted according to their original position in the list", () => {
55+
expect(
56+
compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo", "GET"))
57+
).toBe(0);
58+
});
4759

48-
test("two equal routes are sorted according to their original position in the list", () => {
49-
expect(
50-
compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo", "GET"))
51-
).toBe(0);
60+
test("it returns -1 if the first argument should appear first in the list", () => {
61+
expect(
62+
compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo"))
63+
).toBe(-1);
64+
});
5265
});
5366

54-
test("it returns -1 if the first argument should appear first in the list", () => {
55-
expect(compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo"))).toBe(
56-
-1
57-
);
67+
describe("generateConfigFromFileTree", () => {
68+
runInTempDir();
69+
70+
it("should generate a route entry for each file in the tree", async () => {
71+
writeFileSync(
72+
"foo.ts",
73+
`
74+
export function onRequestGet() {}
75+
export function onRequestPost() {}
76+
`
77+
);
78+
writeFileSync(
79+
"bar.ts",
80+
`
81+
export function onRequestPut() {}
82+
export function onRequestDelete() {}
83+
`
84+
);
85+
86+
const entries = await generateConfigFromFileTree({
87+
baseDir: ".",
88+
baseURL: "/base" as UrlPath,
89+
});
90+
expect(entries).toEqual({
91+
routes: [
92+
{
93+
method: "PUT",
94+
module: ["bar.ts:onRequestPut"],
95+
routePath: "/base/bar",
96+
},
97+
{
98+
method: "DELETE",
99+
module: ["bar.ts:onRequestDelete"],
100+
routePath: "/base/bar",
101+
},
102+
{
103+
method: "GET",
104+
module: ["foo.ts:onRequestGet"],
105+
routePath: "/base/foo",
106+
},
107+
{
108+
method: "POST",
109+
module: ["foo.ts:onRequestPost"],
110+
routePath: "/base/foo",
111+
},
112+
],
113+
});
114+
});
58115
});
59116
});
60117

packages/wrangler/pages/functions/filepath-routing.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ export async function generateConfigFromFileTree({
127127
routeEntries = routeEntries.reduce(
128128
(acc: typeof routeEntries, { routePath, ...rest }) => {
129129
const existingRouteEntry = acc.find(
130-
(routeEntry) => routeEntry.routePath === routePath
130+
(routeEntry) =>
131+
routeEntry.routePath === routePath &&
132+
routeEntry.method === rest.method
131133
);
132134
if (existingRouteEntry !== undefined) {
133135
Object.assign(existingRouteEntry, rest);

0 commit comments

Comments
 (0)