Skip to content

Commit 0a34883

Browse files
committedApr 10, 2024
preserve original path on optional segments
1 parent 2a6608a commit 0a34883

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed
 

‎.changeset/cuddly-falcons-obey.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
preserve original path on optional segments

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules/
22
dist/
33
.vscode/
44
coverage/
5-
solid-router-*.tgz
5+
solid-router-*.tgz
6+
.DS_Store

‎src/routing.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ export function createRoutes(routeDef: RouteDefinition, base: string = ""): Rout
132132
info
133133
};
134134

135-
return asArray(routeDef.path).reduce<Route[]>((acc, path) => {
136-
for (const originalPath of expandOptionals(path)) {
137-
const path = joinPaths(base, originalPath);
135+
return asArray(routeDef.path).reduce<Route[]>((acc, originalPath) => {
136+
for (const expandedPath of expandOptionals(originalPath)) {
137+
const path = joinPaths(base, expandedPath);
138138
let pattern = isLeaf ? path : path.split("/*", 1)[0];
139139
pattern = pattern
140140
.split("/")

‎test/route.spec.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ describe("createRoutes should", () => {
158158
id: "someTHING",
159159
all: "BaR/sOlId"
160160
});
161+
expect(route.originalPath).toBe("foo/:id/*all");
161162
});
162163

163164
test(`validate path using match filters`, () => {
@@ -182,19 +183,25 @@ describe("createRoutes should", () => {
182183
path: "foo/:id?"
183184
};
184185
const routes = createRoutes(routeDef);
185-
expect(routes[0].originalPath).toBe("foo");
186-
expect(routes[1].originalPath).toBe("foo/:id");
186+
expect(routes[0].pattern).toBe("/foo");
187+
expect(routes[0].originalPath).toBe("foo/:id?");
188+
expect(routes[1].pattern).toBe("/foo/:id");
189+
expect(routes[1].originalPath).toBe("foo/:id?");
187190
});
188191

189192
test(`with a multiple paths`, () => {
190193
const routeDef = {
191194
path: ["foo/:id?", "bar/:name?"]
192195
};
193196
const routes = createRoutes(routeDef);
194-
expect(routes[0].originalPath).toBe("foo");
195-
expect(routes[1].originalPath).toBe("foo/:id");
196-
expect(routes[2].originalPath).toBe("bar");
197-
expect(routes[3].originalPath).toBe("bar/:name");
197+
expect(routes[0].pattern).toBe("/foo");
198+
expect(routes[0].originalPath).toBe("foo/:id?");
199+
expect(routes[1].pattern).toBe("/foo/:id");
200+
expect(routes[1].originalPath).toBe("foo/:id?");
201+
expect(routes[2].pattern).toBe("/bar");
202+
expect(routes[2].originalPath).toBe("bar/:name?");
203+
expect(routes[3].pattern).toBe("/bar/:name");
204+
expect(routes[3].originalPath).toBe("bar/:name?");
198205
});
199206
});
200207
});

0 commit comments

Comments
 (0)
Please sign in to comment.