Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-eagles-wear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

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.
19 changes: 16 additions & 3 deletions packages/start/src/router/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,27 @@ function defineRoutes(fileRoutes: Route[]) {
export function matchAPIRoute(path: string, method: string) {
const match = router.lookup(path);
if (match && match.route) {
const handler =
method === "HEAD" ? match.route["$HEAD"] || match.route["$GET"] : match.route[`$${method}`];
const route = match.route;

// Find the appropriate handler for the HTTP method
const handler = method === "HEAD"
? route.$HEAD || route.$GET
: route[`$${method}`];

if (handler === undefined) return;

// Check if this is a page route
const isPage = route.page === true && route.$component !== undefined;

// Return comprehensive route information
return {
handler,
params: match.params
params: match.params,
isPage
};
}

return undefined;
}

function containsHTTP(route: Route) {
Expand Down
1 change: 1 addition & 0 deletions packages/start/src/server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function createBaseHandler(
`API handler for ${event.request.method} "${event.request.url}" did not return a response.`
);
}
if (!match.isPage) return;
}

// render
Expand Down
6 changes: 6 additions & 0 deletions packages/tests/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export default function App() {
<li>
<a href="/generator-server-function">generator server function</a>
</li>
<li>
<a href="/not-found">Not Found</a>
</li>
<li>
<a href="/text-plain-response">Text Plain Response</a>
</li>
<li>
<a href="/referencing-multiple-export-named-functions-in-the-same-file">referencing multiple export named functions in the same file</a>
</li>
Expand Down
21 changes: 21 additions & 0 deletions packages/tests/src/routes/[...404].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Title } from "@solidjs/meta";
import { HttpStatusCode } from "@solidjs/start";
import type { APIEvent } from "@solidjs/start/server";

export const GET = (event: APIEvent) => {
if (event.request.headers.get("accept") !== "application/json") return;
return { notFound: "API" };
};

export default function NotFound() {
return (
<main>
<Title>Not Found</Title>
<HttpStatusCode code={404} />
<h1>Page Not Found</h1>
<p>
{"Your page cannot be found... >_<"}
</p>
</main>
);
}
3 changes: 3 additions & 0 deletions packages/tests/src/routes/api/text-plain.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function GET(e: { nativeEvent: { respondWith: (arg0: Response) => void; }; }) {
e.nativeEvent.respondWith(new Response("test"));
}
15 changes: 15 additions & 0 deletions packages/tests/src/routes/text-plain-response.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default function App() {
const handleClick = (e: Event) => {
// e.preventDefault();

window.location.href = "/api/text-plain";
};

return (
<main>
<a href="/api/text-plain" onClick={handleClick}>
Text Plain Response
</a>
</main>
);
};
Loading