feat: support overriding page routes (#57)
This commit is contained in:
Vendored
+1
-2
@@ -1,6 +1,5 @@
|
||||
{
|
||||
"deno.enable": true,
|
||||
"deno.lint": true,
|
||||
"deno.unstable": true,
|
||||
"deno.config": "./tsconfig.json"
|
||||
"deno.unstable": true
|
||||
}
|
||||
|
||||
@@ -32,4 +32,12 @@ export interface PageConfig {
|
||||
* the client.
|
||||
*/
|
||||
runtimeJS?: boolean;
|
||||
|
||||
/**
|
||||
* A route override for the page. This is useful for pages where the route
|
||||
* can not be expressed through the filesystem routing capabilities.
|
||||
*
|
||||
* The route override must be a path-to-regexp compatible route matcher.
|
||||
*/
|
||||
routeOverride?: string;
|
||||
}
|
||||
|
||||
@@ -41,10 +41,13 @@ export class ServerContext {
|
||||
}
|
||||
const path = url.substring(baseUrl.length).substring("pages".length);
|
||||
const baseRoute = path.substring(1, path.length - extname(path).length);
|
||||
const route = pathToRoute(baseRoute);
|
||||
const name = baseRoute.replace("/", "-");
|
||||
if (!path.startsWith("/_")) {
|
||||
const { default: component, config } = (module as PageModule);
|
||||
let route = pathToRoute(baseRoute);
|
||||
if (config?.routeOverride) {
|
||||
route = String(config.routeOverride);
|
||||
}
|
||||
let { handler } = (module as PageModule);
|
||||
handler ??= {};
|
||||
if (
|
||||
@@ -59,7 +62,7 @@ export class ServerContext {
|
||||
name,
|
||||
component,
|
||||
handler,
|
||||
runtimeJS: config?.runtimeJS ?? false,
|
||||
runtimeJS: Boolean(config?.runtimeJS ?? false),
|
||||
};
|
||||
pages.push(page);
|
||||
} else if (
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { h, PageConfig, PageProps } from "../../deps.ts";
|
||||
|
||||
export default function Page(props: PageProps) {
|
||||
return <div>Book {props.params.id}</div>;
|
||||
}
|
||||
|
||||
export const config: PageConfig = {
|
||||
routeOverride: "/books/:id(\\d+)",
|
||||
};
|
||||
@@ -5,18 +5,20 @@
|
||||
import * as $0 from "./pages/index.tsx";
|
||||
import * as $1 from "./pages/static.tsx";
|
||||
import * as $2 from "./pages/props/[id].tsx";
|
||||
import * as $3 from "./pages/api/get_only.ts";
|
||||
import * as $4 from "./pages/intercept.tsx";
|
||||
import * as $5 from "./pages/[name].tsx";
|
||||
import * as $3 from "./pages/books/[id].tsx";
|
||||
import * as $4 from "./pages/api/get_only.ts";
|
||||
import * as $5 from "./pages/intercept.tsx";
|
||||
import * as $6 from "./pages/[name].tsx";
|
||||
|
||||
const routes = {
|
||||
pages: {
|
||||
"./pages/index.tsx": $0,
|
||||
"./pages/static.tsx": $1,
|
||||
"./pages/props/[id].tsx": $2,
|
||||
"./pages/api/get_only.ts": $3,
|
||||
"./pages/intercept.tsx": $4,
|
||||
"./pages/[name].tsx": $5,
|
||||
"./pages/books/[id].tsx": $3,
|
||||
"./pages/api/get_only.ts": $4,
|
||||
"./pages/intercept.tsx": $5,
|
||||
"./pages/[name].tsx": $6,
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
@@ -123,3 +123,18 @@ Deno.test("/static page prerender", async () => {
|
||||
assertStringIncludes(body, "<p>This is a static page.</p>");
|
||||
assert(!body.includes("__FRSH_PROPS"));
|
||||
});
|
||||
|
||||
Deno.test("/books/:id page - /books/123", async () => {
|
||||
const resp = await router(new Request("https://fresh.deno.dev/books/123"));
|
||||
assert(resp);
|
||||
assertEquals(resp.status, 200);
|
||||
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
||||
const body = await resp.text();
|
||||
assertStringIncludes(body, "<div>Book 123</div>");
|
||||
});
|
||||
|
||||
Deno.test("/books/:id page - /books/abc", async () => {
|
||||
const resp = await router(new Request("https://fresh.deno.dev/books/abc"));
|
||||
assert(resp);
|
||||
assertEquals(resp.status, 404);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user