Skip to content

Commit 59094aa

Browse files
committed
move flat routing replacement to fs router
1 parent 939a036 commit 59094aa

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

packages/start/config/fs-router.js

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
import { analyzeModule, BaseFileSystemRouter, cleanPath } from "vinxi/fs-router";
22

3+
function toPathBase(src, config) {
4+
const routePath = cleanPath(src, config)
5+
// remove the initial slash
6+
.slice(1)
7+
.replace(/index$/, "")
8+
// replace . with / for flat routes - e.g. foo.bar -> foo/bar
9+
.replace(/\./g, "/")
10+
// converts any splat route ... that got replaced back from ///
11+
// this could be avoided with a lookbehind regex but safar has only supported them since mid 2023
12+
.replace(/\/\/\//g, "...")
13+
.replace(/\[([^\/]+)\]/g, (_, m) => {
14+
if (m.length > 3 && m.startsWith("...")) {
15+
return `*${m.slice(3)}`;
16+
}
17+
if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) {
18+
return `:${m.slice(1, -1)}?`;
19+
}
20+
return `:${m}`;
21+
});
22+
23+
return routePath?.length > 0 ? `/${routePath}` : "/";
24+
}
25+
326
export class SolidStartClientFileRouter extends BaseFileSystemRouter {
427
toPath(src) {
5-
const routePath = cleanPath(src, this.config)
6-
// remove the initial slash
7-
.slice(1)
8-
.replace(/index$/, "")
9-
.replace(/\[([^\/]+)\]/g, (_, m) => {
10-
if (m.length > 3 && m.startsWith("...")) {
11-
return `*${m.slice(3)}`;
12-
}
13-
if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) {
14-
return `:${m.slice(1, -1)}?`;
15-
}
16-
return `:${m}`;
17-
});
18-
19-
return routePath?.length > 0 ? `/${routePath}` : "/";
28+
return toPathBase(src, this.config);
2029
}
2130

2231
toRoute(src) {
@@ -72,21 +81,7 @@ function createHTTPHandlers(src, exports) {
7281

7382
export class SolidStartServerFileRouter extends BaseFileSystemRouter {
7483
toPath(src) {
75-
const routePath = cleanPath(src, this.config)
76-
// remove the initial slash
77-
.slice(1)
78-
.replace(/index$/, "")
79-
.replace(/\[([^\/]+)\]/g, (_, m) => {
80-
if (m.length > 3 && m.startsWith("...")) {
81-
return `*${m.slice(3)}`;
82-
}
83-
if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) {
84-
return `:${m.slice(1, -1)}?`;
85-
}
86-
return `:${m}`;
87-
});
88-
89-
return routePath?.length > 0 ? `/${routePath}` : "/";
84+
return toPathBase(src, this.config);
9085
}
9186

9287
toRoute(src) {

packages/start/src/router/routes.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ function defineRoutes(fileRoutes: Route[]) {
3636
const path = id
3737
// strip out escape group for escaping nested routes - e.g. foo(bar) -> foo
3838
.replace(/\/\([^)/]+\)/g, "")
39-
.replace(/\([^)/]+\)/g, "")
40-
// replace . with / for flat routes - e.g. foo.bar -> foo/bar
41-
.replace(/\./g, "/")
42-
// converts any splat route ... that got replaced back from ///
43-
// this could be avoided with a lookbehind regex but safar has only supported them since mid 2023
44-
.replace("///", "...");
39+
.replace(/\([^)/]+\)/g, "");
4540

4641
routes.push({ ...route, id, path });
4742
return routes;

0 commit comments

Comments
 (0)