fix: PluginRoute supports AsyncRoute and AsyncLayout (#2139)

This commit is contained in:
Reed von Redwitz
2023-12-04 16:56:00 +01:00
committed by GitHub
parent 993f1020b6
commit c8844ad98c
6 changed files with 93 additions and 16 deletions
+5 -1
View File
@@ -572,7 +572,11 @@ export interface PluginRoute {
/** A path in the format of a filename path without filetype */ /** A path in the format of a filename path without filetype */
path: string; path: string;
component?: ComponentType<PageProps> | ComponentType<AppProps>; component?:
| ComponentType<PageProps>
| ComponentType<AppProps>
| AsyncRoute
| AsyncLayout;
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
handler?: Handler<any, any> | Handlers<any, any>; handler?: Handler<any, any> | Handlers<any, any>;
+1 -1
View File
@@ -12,7 +12,7 @@ export default {
jsInjectPlugin, jsInjectPlugin,
cssInjectPluginAsync, cssInjectPluginAsync,
linkInjectPlugin, linkInjectPlugin,
routePlugin({ title: "Title Set From Plugin Config" }), routePlugin({ title: "Title Set From Plugin Config", async: false }),
secondMiddlewarePlugin(), secondMiddlewarePlugin(),
], ],
} as FreshConfig; } as FreshConfig;
@@ -3,10 +3,12 @@ import { handler as testMiddleware } from "./sample_routes/_middleware.ts";
import { AppBuilder } from "./sample_routes/AppBuilder.tsx"; import { AppBuilder } from "./sample_routes/AppBuilder.tsx";
import IslandPluginComponent from "./sample_routes/PluginRouteWithIsland.tsx"; import IslandPluginComponent from "./sample_routes/PluginRouteWithIsland.tsx";
import { SimpleRoute } from "./sample_routes/simple-route.tsx"; import { SimpleRoute } from "./sample_routes/simple-route.tsx";
import AsyncRoute from "./sample_routes/async-route.tsx";
export type { Options }; export type { Options };
interface Options { interface Options {
title: string; title: string;
async: boolean;
} }
export type PluginMiddlewareState = { export type PluginMiddlewareState = {
num: number; num: number;
@@ -45,6 +47,7 @@ export default function routePlugin(
path: "lots-of-middleware", path: "lots-of-middleware",
}], }],
routes: [ routes: [
{ path: "/async-route", component: AsyncRoute },
{ {
path: "/_app", path: "/_app",
component: AppBuilder(options), component: AppBuilder(options),
@@ -1,18 +1,32 @@
import { PageProps } from "$fresh/server.ts"; import { defineApp, PageProps } from "$fresh/server.ts";
import { Head } from "../../../../runtime.ts"; import { Head } from "../../../../runtime.ts";
import { Options } from "../route-plugin.ts"; import { Options } from "../route-plugin.ts";
export function AppBuilder(options: Options) { export function AppBuilder(options: Options) {
return ({ Component }: PageProps) => { return options.async
return ( ? defineApp((_req, ctx) => {
<> return (
<Head> <>
<title>{options.title}</title> <Head>
</Head> <title>{options.title}</title>
<main class="max-w-screen-md px-4 pt-16 mx-auto"> </Head>
<Component /> <main class="max-w-screen-md px-4 pt-16 mx-auto">
</main> foo
</> <ctx.Component />
); </main>
}; </>
);
})
: ({ Component }: PageProps) => {
return (
<>
<Head>
<title>{options.title}</title>
</Head>
<main class="max-w-screen-md px-4 pt-16 mx-auto">
<Component />
</main>
</>
);
};
} }
@@ -0,0 +1,9 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute((_req, _ctx) => {
return (
<div>
this is an async route!
</div>
);
});
+48 -1
View File
@@ -1,4 +1,4 @@
import { ServerContext, STATUS_CODE } from "../server.ts"; import { type FreshConfig, ServerContext, STATUS_CODE } from "../server.ts";
import { import {
assert, assert,
assertEquals, assertEquals,
@@ -17,6 +17,8 @@ import {
withFakeServe, withFakeServe,
withPageName, withPageName,
} from "./test_utils.ts"; } from "./test_utils.ts";
import routePlugin from "./fixture_plugin/utils/route-plugin.ts";
import secondMiddlewarePlugin from "./fixture_plugin/utils/second-middleware-plugin.ts";
const ctx = await ServerContext.fromManifest(manifest, config); const ctx = await ServerContext.fromManifest(manifest, config);
const handler = ctx.handler(); const handler = ctx.handler();
@@ -81,6 +83,38 @@ Deno.test("plugin routes and middleware", async () => {
); );
}); });
Deno.test("plugin routes and middleware -- async _app", async () => {
const ctx = await ServerContext.fromManifest(manifest, {
plugins: [
routePlugin({ title: "Title Set From Plugin Config", async: true }),
secondMiddlewarePlugin(),
],
} as FreshConfig);
const handler = ctx.handler();
const router = (req: Request) => {
return handler(req, {
remoteAddr: {
transport: "tcp",
hostname: "127.0.0.1",
port: 80,
},
});
};
const resp = await router(new Request("https://fresh.deno.dev/test"));
assert(resp);
assertEquals(resp.status, STATUS_CODE.OK);
const body = await resp.text();
assertStringIncludes(
body,
`<h1>look, i'm set from a plugin!</h1>`,
);
assertStringIncludes(
body,
`<title>Title Set From Plugin Config</title>`,
);
});
Deno.test("plugin middleware multiple handlers", async () => { Deno.test("plugin middleware multiple handlers", async () => {
const resp = await router( const resp = await router(
new Request("https://fresh.deno.dev/lots-of-middleware"), new Request("https://fresh.deno.dev/lots-of-middleware"),
@@ -107,6 +141,19 @@ Deno.test("plugin route no leading slash", async () => {
); );
}); });
Deno.test("plugin async route", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/async-route"),
);
assert(resp);
assertEquals(resp.status, STATUS_CODE.OK);
const body = await resp.text();
assertStringIncludes(
body,
`<div>this is an async route!</div>`,
);
});
Deno.test({ Deno.test({
name: "plugin supports islands", name: "plugin supports islands",
async fn(t) { async fn(t) {