feat: expose config in context + simplify page props (#2102)
This commit is contained in:
@@ -40,6 +40,7 @@ export {
|
||||
fromFileUrl,
|
||||
join,
|
||||
relative,
|
||||
SEP,
|
||||
toFileUrl,
|
||||
} from "https://deno.land/std@0.208.0/path/mod.ts";
|
||||
export * as JSONC from "https://deno.land/std@0.208.0/jsonc/mod.ts";
|
||||
|
||||
@@ -18,6 +18,8 @@ import * as $api_head_override from "./routes/api/head_override.ts";
|
||||
import * as $assetsCaching_index from "./routes/assetsCaching/index.tsx";
|
||||
import * as $books_id_ from "./routes/books/[id].tsx";
|
||||
import * as $connInfo from "./routes/connInfo.ts";
|
||||
import * as $ctx_config from "./routes/ctx_config.tsx";
|
||||
import * as $ctx_config_props from "./routes/ctx_config_props.tsx";
|
||||
import * as $error_boundary from "./routes/error_boundary.tsx";
|
||||
import * as $event_handler_string from "./routes/event_handler_string.tsx";
|
||||
import * as $event_handler_string_island from "./routes/event_handler_string_island.tsx";
|
||||
@@ -117,6 +119,8 @@ const manifest = {
|
||||
"./routes/assetsCaching/index.tsx": $assetsCaching_index,
|
||||
"./routes/books/[id].tsx": $books_id_,
|
||||
"./routes/connInfo.ts": $connInfo,
|
||||
"./routes/ctx_config.tsx": $ctx_config,
|
||||
"./routes/ctx_config_props.tsx": $ctx_config_props,
|
||||
"./routes/error_boundary.tsx": $error_boundary,
|
||||
"./routes/event_handler_string.tsx": $event_handler_string,
|
||||
"./routes/event_handler_string_island.tsx": $event_handler_string_island,
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { defineRoute } from "$fresh/server.ts";
|
||||
import { relative, SEP } from "$fresh/tests/deps.ts";
|
||||
|
||||
export default defineRoute((_req, ctx) => {
|
||||
const value = JSON.stringify(ctx, (key, value) => {
|
||||
if (key === "outDir" || key == "staticDir") {
|
||||
return relative(Deno.cwd(), value).split(SEP).join("/");
|
||||
}
|
||||
if (typeof value === "function") return value.constructor.name;
|
||||
if (value === undefined) return "<undefined>";
|
||||
return value;
|
||||
}, 2);
|
||||
|
||||
return (
|
||||
<pre>
|
||||
{value}
|
||||
</pre>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
import { relative, SEP } from "../../deps.ts";
|
||||
|
||||
export default function Page(props: PageProps) {
|
||||
const value = JSON.stringify(props, (key, value) => {
|
||||
if (key === "outDir" || key == "staticDir") {
|
||||
return relative(Deno.cwd(), value).split(SEP).join("/");
|
||||
}
|
||||
if (typeof value === "function") return value.constructor.name;
|
||||
if (value === undefined) return "<undefined>";
|
||||
return value;
|
||||
}, 2);
|
||||
|
||||
return <pre>{value}</pre>;
|
||||
}
|
||||
@@ -1,10 +1,16 @@
|
||||
import { RouteContext } from "../../../../server.ts";
|
||||
import { delay } from "../../../deps.ts";
|
||||
import { delay, relative, SEP } from "../../../deps.ts";
|
||||
|
||||
export default async function Foo(_req: Request, context: RouteContext) {
|
||||
await delay(1);
|
||||
const value = JSON.stringify(context, (_key, value) => {
|
||||
const value = JSON.stringify(context, (key, value) => {
|
||||
if (key === "outDir" || key == "staticDir") {
|
||||
return relative(Deno.cwd(), value).split(SEP).join("/");
|
||||
} else if (key === "entrypoints") {
|
||||
return {};
|
||||
}
|
||||
if (typeof value === "function") return value.constructor.name;
|
||||
if (value === undefined) return "<undefined>";
|
||||
return value;
|
||||
}, 2);
|
||||
|
||||
|
||||
+95
-17
@@ -52,23 +52,6 @@ Deno.test("/ page prerender", async () => {
|
||||
assertStringIncludes(body, `<link rel="modulepreload"`);
|
||||
});
|
||||
|
||||
Deno.test("/props/123 page prerender", async () => {
|
||||
const resp = await handler(new Request("https://fresh.deno.dev/props/123"));
|
||||
assert(resp);
|
||||
assertEquals(resp.status, STATUS_CODE.OK);
|
||||
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
||||
const body = await resp.text();
|
||||
const doc = parseHtml(body);
|
||||
const data = JSON.parse(doc.querySelector("body > div")!.textContent!);
|
||||
|
||||
assertEquals(data, {
|
||||
"params": { "id": "123" },
|
||||
"url": "https://fresh.deno.dev/props/123",
|
||||
"route": "/props/:id",
|
||||
"state": { "root": "root_mw" },
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("/greet/[name] page prerender", async () => {
|
||||
const resp = await handler(new Request("https://fresh.deno.dev/bar"));
|
||||
assert(resp);
|
||||
@@ -1127,3 +1110,98 @@ Deno.test("pass options in config dev.ts", async (t) => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("Expose config in ctx", async () => {
|
||||
await withFakeServe("./tests/fixture/main.ts", async (server) => {
|
||||
const doc = await server.getHtml(`/ctx_config`);
|
||||
const data = JSON.parse(doc.querySelector("pre")?.textContent!);
|
||||
assertEquals(data, {
|
||||
config: {
|
||||
basePath: "",
|
||||
build: {
|
||||
outDir: "tests/fixture/_fresh",
|
||||
target: [
|
||||
"chrome99",
|
||||
"firefox99",
|
||||
"safari15",
|
||||
],
|
||||
},
|
||||
dev: false,
|
||||
plugins: [],
|
||||
render: "Function",
|
||||
router: "<undefined>",
|
||||
server: {},
|
||||
staticDir: "tests/fixture/static",
|
||||
},
|
||||
Component: "Function",
|
||||
params: {},
|
||||
route: "/ctx_config",
|
||||
state: {
|
||||
root: "root_mw",
|
||||
},
|
||||
url: "https://127.0.0.1/ctx_config",
|
||||
basePath: "",
|
||||
codeFrame: "<undefined>",
|
||||
destination: "route",
|
||||
error: "<undefined>",
|
||||
isPartial: false,
|
||||
remoteAddr: {
|
||||
hostname: "127.0.0.1",
|
||||
port: 80,
|
||||
transport: "tcp",
|
||||
},
|
||||
next: "Function",
|
||||
render: "AsyncFunction",
|
||||
renderNotFound: "AsyncFunction",
|
||||
localAddr: "<undefined>",
|
||||
pattern: "/ctx_config",
|
||||
data: "<undefined>",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("Expose config in ctx", async () => {
|
||||
await withFakeServe("./tests/fixture/main.ts", async (server) => {
|
||||
const doc = await server.getHtml(`/ctx_config_props`);
|
||||
const data = JSON.parse(doc.querySelector("pre")?.textContent!);
|
||||
assertEquals(data, {
|
||||
config: {
|
||||
basePath: "",
|
||||
build: {
|
||||
outDir: "tests/fixture/_fresh",
|
||||
target: [
|
||||
"chrome99",
|
||||
"firefox99",
|
||||
"safari15",
|
||||
],
|
||||
},
|
||||
dev: false,
|
||||
plugins: [],
|
||||
render: "Function",
|
||||
router: "<undefined>",
|
||||
server: {},
|
||||
staticDir: "tests/fixture/static",
|
||||
},
|
||||
Component: "Function",
|
||||
params: {},
|
||||
route: "/ctx_config_props",
|
||||
state: {
|
||||
root: "root_mw",
|
||||
},
|
||||
url: "https://127.0.0.1/ctx_config_props",
|
||||
basePath: "",
|
||||
codeFrame: "<undefined>",
|
||||
destination: "route",
|
||||
error: "<undefined>",
|
||||
isPartial: false,
|
||||
remoteAddr: {
|
||||
hostname: "127.0.0.1",
|
||||
port: 80,
|
||||
transport: "tcp",
|
||||
},
|
||||
localAddr: "<undefined>",
|
||||
pattern: "/ctx_config_props",
|
||||
data: "<undefined>",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -83,6 +83,29 @@ Deno.test("passes context to server component", async () => {
|
||||
port: 8000,
|
||||
transport: "tcp",
|
||||
},
|
||||
config: {
|
||||
basePath: "",
|
||||
build: {
|
||||
outDir: "tests/fixture_server_components/_fresh",
|
||||
target: [
|
||||
"chrome99",
|
||||
"firefox99",
|
||||
"safari15",
|
||||
],
|
||||
},
|
||||
dev: false,
|
||||
plugins: [
|
||||
{ entrypoints: {}, name: "twind", renderAsync: "AsyncFunction" },
|
||||
],
|
||||
render: "Function",
|
||||
router: "<undefined>",
|
||||
server: {},
|
||||
staticDir: "tests/fixture_server_components/static",
|
||||
},
|
||||
data: "<undefined>",
|
||||
error: "<undefined>",
|
||||
codeFrame: "<undefined>",
|
||||
pattern: "/context/:id",
|
||||
render: "AsyncFunction",
|
||||
Component: "Function",
|
||||
destination: "route",
|
||||
|
||||
Reference in New Issue
Block a user