chore(server): simplify plugin route creation (#1594)
This commit is contained in:
@@ -1289,10 +1289,14 @@ function getMiddlewareRoutesFromPlugins(
|
|||||||
return Object.values(mws);
|
return Object.values(mws);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatRoutePath(path: string) {
|
||||||
|
return path.startsWith("/") ? path : "/" + path;
|
||||||
|
}
|
||||||
|
|
||||||
function getRoutesFromPlugins(plugins: Plugin[]): [string, RouteModule][] {
|
function getRoutesFromPlugins(plugins: Plugin[]): [string, RouteModule][] {
|
||||||
return plugins.flatMap((plugin) => plugin.routes ?? [])
|
return plugins.flatMap((plugin) => plugin.routes ?? [])
|
||||||
.map((route) => {
|
.map((route) => {
|
||||||
return [`./routes${route.path}.ts`, {
|
return [`./routes${formatRoutePath(route.path)}.ts`, {
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
default: route.component as any,
|
default: route.component as any,
|
||||||
handler: route.handler,
|
handler: route.handler,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { MiddlewareHandlerContext, Plugin } from "$fresh/server.ts";
|
import { MiddlewareHandlerContext, Plugin } from "$fresh/server.ts";
|
||||||
import { handler as testMiddleware } from "./sample_routes/_middleware.ts";
|
import { handler as testMiddleware } from "./sample_routes/_middleware.ts";
|
||||||
import { AppBuilder } from "./sample_routes/AppBuilder.tsx";
|
import { AppBuilder } from "./sample_routes/AppBuilder.tsx";
|
||||||
|
import { SimpleRoute } from "./sample_routes/simple-route.tsx";
|
||||||
export type { Options };
|
export type { Options };
|
||||||
|
|
||||||
interface Options {
|
interface Options {
|
||||||
@@ -45,6 +46,6 @@ export default function routePlugin(
|
|||||||
routes: [{
|
routes: [{
|
||||||
path: "/_app",
|
path: "/_app",
|
||||||
component: AppBuilder(options),
|
component: AppBuilder(options),
|
||||||
}],
|
}, { path: "no-leading-slash-here", component: SimpleRoute }],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export function SimpleRoute() {
|
||||||
|
return <div>Hello</div>;
|
||||||
|
}
|
||||||
@@ -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,
|
||||||
|
`<div>Hello</div>`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "/with-island hydration",
|
name: "/with-island hydration",
|
||||||
async fn(t) {
|
async fn(t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user