feat(server): plugins can add middlewares and routes (#1455)
Co-authored-by: RoeHH <git@roeh.ch> Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
co-authored by
RoeHH
Marvin Hagemeister
parent
7dee138bee
commit
50db296fb7
@@ -3,13 +3,15 @@
|
||||
// This file is automatically updated during development when running `dev.ts`.
|
||||
|
||||
import * as $0 from "./routes/static.tsx";
|
||||
import * as $1 from "./routes/with-island.tsx";
|
||||
import * as $1 from "./routes/test.tsx";
|
||||
import * as $2 from "./routes/with-island.tsx";
|
||||
import * as $$0 from "./islands/Island.tsx";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
"./routes/static.tsx": $0,
|
||||
"./routes/with-island.tsx": $1,
|
||||
"./routes/test.tsx": $1,
|
||||
"./routes/with-island.tsx": $2,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Island.tsx": $$0,
|
||||
|
||||
@@ -2,11 +2,13 @@ import { FreshOptions } from "$fresh/server.ts";
|
||||
import cssInjectPlugin from "./utils/css-inject-plugin.ts";
|
||||
import jsInjectPlugin from "./utils/js-inject-plugin.ts";
|
||||
import cssInjectPluginAsync from "./utils/css-inject-plugin-async.ts";
|
||||
import routePlugin from "./utils/route-plugin.ts";
|
||||
|
||||
export default {
|
||||
plugins: [
|
||||
cssInjectPlugin,
|
||||
jsInjectPlugin,
|
||||
cssInjectPluginAsync,
|
||||
routePlugin({ title: "Title Set From Plugin Config" }),
|
||||
],
|
||||
} as FreshOptions;
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Handlers, PageProps } from "../../../server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(_req, ctx) {
|
||||
const resp = await ctx.render();
|
||||
return resp;
|
||||
},
|
||||
};
|
||||
|
||||
export default function Home(props: PageProps) {
|
||||
const value = props.state.test as string;
|
||||
return (
|
||||
<div>
|
||||
<h1>{value}</h1>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Plugin } from "$fresh/server.ts";
|
||||
import { handler as testMiddleware } from "./sample_routes/_middleware.ts";
|
||||
import { AppBuilder } from "./sample_routes/AppBuilder.tsx";
|
||||
export type { Options };
|
||||
|
||||
interface Options {
|
||||
title: string;
|
||||
}
|
||||
|
||||
export default function routePlugin(options: Options): Plugin {
|
||||
return {
|
||||
name: "routePlugin",
|
||||
middlewares: [{
|
||||
middleware: { handler: testMiddleware },
|
||||
path: "/",
|
||||
}],
|
||||
routes: [{
|
||||
path: "/_app",
|
||||
component: AppBuilder(options),
|
||||
}],
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { AppProps } from "$fresh/server.ts";
|
||||
import { Head } from "../../../../runtime.ts";
|
||||
import { Options } from "../route-plugin.ts";
|
||||
|
||||
export function AppBuilder(options: Options) {
|
||||
return ({ Component }: AppProps) => {
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
<title>{options.title}</title>
|
||||
</Head>
|
||||
<main class="max-w-screen-md px-4 pt-16 mx-auto">
|
||||
<Component />
|
||||
</main>
|
||||
</>
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
ctx.state.test = "look, i'm set from a plugin!";
|
||||
const resp = await ctx.next();
|
||||
return resp;
|
||||
}
|
||||
Reference in New Issue
Block a user