Skip to content

Commit 222c8fc

Browse files
authored
fix: Enhance route matching to return isPage flag and add tests for… (#1927)
1 parent 12f3a3d commit 222c8fc

File tree

7 files changed

+67
-3
lines changed

7 files changed

+67
-3
lines changed

.changeset/slow-eagles-wear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/start": patch
3+
---
4+
5+
Enhance route matching to return `isPage` flag and add tests for plain text API response and Missing page handling that has both api/file routes.

packages/start/src/router/routes.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,27 @@ function defineRoutes(fileRoutes: Route[]) {
6060
export function matchAPIRoute(path: string, method: string) {
6161
const match = router.lookup(path);
6262
if (match && match.route) {
63-
const handler =
64-
method === "HEAD" ? match.route["$HEAD"] || match.route["$GET"] : match.route[`$${method}`];
63+
const route = match.route;
64+
65+
// Find the appropriate handler for the HTTP method
66+
const handler = method === "HEAD"
67+
? route.$HEAD || route.$GET
68+
: route[`$${method}`];
69+
6570
if (handler === undefined) return;
71+
72+
// Check if this is a page route
73+
const isPage = route.page === true && route.$component !== undefined;
74+
75+
// Return comprehensive route information
6676
return {
6777
handler,
68-
params: match.params
78+
params: match.params,
79+
isPage
6980
};
7081
}
82+
83+
return undefined;
7184
}
7285

7386
function containsHTTP(route: Route) {

packages/start/src/server/handler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export function createBaseHandler(
5959
`API handler for ${event.request.method} "${event.request.url}" did not return a response.`
6060
);
6161
}
62+
if (!match.isPage) return;
6263
}
6364

6465
// render

packages/tests/src/app.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export default function App() {
5050
<li>
5151
<a href="/generator-server-function">generator server function</a>
5252
</li>
53+
<li>
54+
<a href="/not-found">Not Found</a>
55+
</li>
56+
<li>
57+
<a href="/text-plain-response">Text Plain Response</a>
58+
</li>
5359
<li>
5460
<a href="/referencing-multiple-export-named-functions-in-the-same-file">referencing multiple export named functions in the same file</a>
5561
</li>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Title } from "@solidjs/meta";
2+
import { HttpStatusCode } from "@solidjs/start";
3+
import type { APIEvent } from "@solidjs/start/server";
4+
5+
export const GET = (event: APIEvent) => {
6+
if (event.request.headers.get("accept") !== "application/json") return;
7+
return { notFound: "API" };
8+
};
9+
10+
export default function NotFound() {
11+
return (
12+
<main>
13+
<Title>Not Found</Title>
14+
<HttpStatusCode code={404} />
15+
<h1>Page Not Found</h1>
16+
<p>
17+
{"Your page cannot be found... >_<"}
18+
</p>
19+
</main>
20+
);
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function GET(e: { nativeEvent: { respondWith: (arg0: Response) => void; }; }) {
2+
e.nativeEvent.respondWith(new Response("test"));
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function App() {
2+
const handleClick = (e: Event) => {
3+
// e.preventDefault();
4+
5+
window.location.href = "/api/text-plain";
6+
};
7+
8+
return (
9+
<main>
10+
<a href="/api/text-plain" onClick={handleClick}>
11+
Text Plain Response
12+
</a>
13+
</main>
14+
);
15+
};

0 commit comments

Comments
 (0)