diff --git a/src/server/context.ts b/src/server/context.ts index 25db510a..d81be08d 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -1289,10 +1289,14 @@ function getMiddlewareRoutesFromPlugins( return Object.values(mws); } +function formatRoutePath(path: string) { + return path.startsWith("/") ? path : "/" + path; +} + function getRoutesFromPlugins(plugins: Plugin[]): [string, RouteModule][] { return plugins.flatMap((plugin) => plugin.routes ?? []) .map((route) => { - return [`./routes${route.path}.ts`, { + return [`./routes${formatRoutePath(route.path)}.ts`, { // deno-lint-ignore no-explicit-any default: route.component as any, handler: route.handler, diff --git a/tests/fixture_plugin/utils/route-plugin.ts b/tests/fixture_plugin/utils/route-plugin.ts index 789eadaa..aacd350c 100644 --- a/tests/fixture_plugin/utils/route-plugin.ts +++ b/tests/fixture_plugin/utils/route-plugin.ts @@ -1,6 +1,7 @@ import { MiddlewareHandlerContext, Plugin } from "$fresh/server.ts"; import { handler as testMiddleware } from "./sample_routes/_middleware.ts"; import { AppBuilder } from "./sample_routes/AppBuilder.tsx"; +import { SimpleRoute } from "./sample_routes/simple-route.tsx"; export type { Options }; interface Options { @@ -45,6 +46,6 @@ export default function routePlugin( routes: [{ path: "/_app", component: AppBuilder(options), - }], + }, { path: "no-leading-slash-here", component: SimpleRoute }], }; } diff --git a/tests/fixture_plugin/utils/sample_routes/simple-route.tsx b/tests/fixture_plugin/utils/sample_routes/simple-route.tsx new file mode 100644 index 00000000..8c8de652 --- /dev/null +++ b/tests/fixture_plugin/utils/sample_routes/simple-route.tsx @@ -0,0 +1,3 @@ +export function SimpleRoute() { + return
Hello
; +} diff --git a/tests/plugin_test.ts b/tests/plugin_test.ts index eee4c043..999215bf 100644 --- a/tests/plugin_test.ts +++ b/tests/plugin_test.ts @@ -81,6 +81,19 @@ Deno.test("plugin middleware multiple handlers", async () => { ); }); +Deno.test("plugin route no leading slash", async () => { + const resp = await router( + new Request("https://fresh.deno.dev/no-leading-slash-here"), + ); + assert(resp); + assertEquals(resp.status, Status.OK); + const body = await resp.text(); + assertStringIncludes( + body, + `
Hello
`, + ); +}); + Deno.test({ name: "/with-island hydration", async fn(t) {