diff --git a/.changeset/slow-eagles-wear.md b/.changeset/slow-eagles-wear.md new file mode 100644 index 000000000..0ca32b23b --- /dev/null +++ b/.changeset/slow-eagles-wear.md @@ -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. diff --git a/packages/start/src/router/routes.ts b/packages/start/src/router/routes.ts index de383ac34..fe569b31a 100644 --- a/packages/start/src/router/routes.ts +++ b/packages/start/src/router/routes.ts @@ -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) { diff --git a/packages/start/src/server/handler.ts b/packages/start/src/server/handler.ts index effd00735..01714c1ac 100644 --- a/packages/start/src/server/handler.ts +++ b/packages/start/src/server/handler.ts @@ -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 diff --git a/packages/tests/src/app.tsx b/packages/tests/src/app.tsx index 3be335393..fc0916758 100644 --- a/packages/tests/src/app.tsx +++ b/packages/tests/src/app.tsx @@ -50,6 +50,12 @@ export default function App() {
  • generator server function
  • +
  • + Not Found +
  • +
  • + Text Plain Response +
  • referencing multiple export named functions in the same file
  • diff --git a/packages/tests/src/routes/[...404].tsx b/packages/tests/src/routes/[...404].tsx new file mode 100644 index 000000000..7d71e47bc --- /dev/null +++ b/packages/tests/src/routes/[...404].tsx @@ -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 ( +
    + Not Found + +

    Page Not Found

    +

    + {"Your page cannot be found... >_<"} +

    +
    + ); +} diff --git a/packages/tests/src/routes/api/text-plain.tsx b/packages/tests/src/routes/api/text-plain.tsx new file mode 100644 index 000000000..89d9ae907 --- /dev/null +++ b/packages/tests/src/routes/api/text-plain.tsx @@ -0,0 +1,3 @@ +export function GET(e: { nativeEvent: { respondWith: (arg0: Response) => void; }; }) { + e.nativeEvent.respondWith(new Response("test")); +} diff --git a/packages/tests/src/routes/text-plain-response.tsx b/packages/tests/src/routes/text-plain-response.tsx new file mode 100644 index 000000000..5fc11d61d --- /dev/null +++ b/packages/tests/src/routes/text-plain-response.tsx @@ -0,0 +1,15 @@ +export default function App() { + const handleClick = (e: Event) => { + // e.preventDefault(); + + window.location.href = "/api/text-plain"; + }; + + return ( +
    + + Text Plain Response + +
    + ); +};