feat: Support setting <html> in _app template (#1617)
This commit is contained in:
@@ -458,3 +458,59 @@ Deno.test("fresh-update", async function fn(t) {
|
||||
assertEquals(code, 0);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("fresh-update add _app.tsx if not present", async function fn(t) {
|
||||
// Preparation
|
||||
const tmpDirName = await Deno.makeTempDir();
|
||||
|
||||
const cliProcess = new Deno.Command(Deno.execPath(), {
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
path.join(Deno.cwd(), "init.ts"),
|
||||
".",
|
||||
],
|
||||
cwd: tmpDirName,
|
||||
stdin: "null",
|
||||
stdout: "null",
|
||||
});
|
||||
|
||||
await cliProcess.output();
|
||||
|
||||
const appTsx = path.join(tmpDirName, "routes", "_app.tsx");
|
||||
await Deno.remove(appTsx);
|
||||
|
||||
await t.step("execute update command", async () => {
|
||||
await updateAndVerify(
|
||||
/The manifest has been generated for \d+ routes and \d+ islands./,
|
||||
);
|
||||
});
|
||||
|
||||
await t.step("add _app.tsx", async () => {
|
||||
const raw = await Deno.readTextFile(appTsx);
|
||||
assert(raw.includes("<html>"), `<html>-tag not found in _app.tsx`);
|
||||
});
|
||||
|
||||
async function updateAndVerify(expected: RegExp) {
|
||||
const cliProcess = new Deno.Command(Deno.execPath(), {
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
path.join(Deno.cwd(), "update.ts"),
|
||||
".",
|
||||
],
|
||||
cwd: tmpDirName,
|
||||
stdin: "null",
|
||||
stdout: "piped",
|
||||
});
|
||||
|
||||
const { code, stdout } = await cliProcess.output();
|
||||
const output = new TextDecoder().decode(stdout);
|
||||
|
||||
assertMatch(
|
||||
output,
|
||||
expected,
|
||||
);
|
||||
assertEquals(code, 0);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import {
|
||||
assertNotSelector,
|
||||
assertSelector,
|
||||
assertTextMany,
|
||||
fetchHtml,
|
||||
withFresh,
|
||||
} from "$fresh/tests/test_utils.ts";
|
||||
|
||||
Deno.test("doesn't apply internal app template", async () => {
|
||||
await withFresh(
|
||||
"./tests/fixture_explicit_app/main.ts",
|
||||
async (address) => {
|
||||
const doc = await fetchHtml(`${address}`);
|
||||
|
||||
// Doesn't render internal app template
|
||||
assertNotSelector(doc, "body body");
|
||||
|
||||
assertSelector(doc, "html > head");
|
||||
assertSelector(doc, "html > body");
|
||||
assertSelector(doc, `meta[charset="utf-8"]`);
|
||||
assertSelector(
|
||||
doc,
|
||||
`meta[name="viewport"][content="width=device-width, initial-scale=1.0"]`,
|
||||
);
|
||||
assertTextMany(doc, "title", ["fresh title"]);
|
||||
|
||||
// Still renders page
|
||||
assertSelector(doc, "body > .inner-body > .page");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("user _app works with <Head>", async () => {
|
||||
await withFresh(
|
||||
"./tests/fixture_explicit_app/main.ts",
|
||||
async (address) => {
|
||||
const doc = await fetchHtml(`${address}/head`);
|
||||
|
||||
// Doesn't render internal app template
|
||||
assertNotSelector(doc, "body body");
|
||||
|
||||
assertSelector(doc, "html > head");
|
||||
assertSelector(doc, "html > body");
|
||||
assertSelector(doc, `meta[charset="utf-8"]`);
|
||||
assertSelector(
|
||||
doc,
|
||||
`meta[name="viewport"][content="width=device-width, initial-scale=1.0"]`,
|
||||
);
|
||||
assertSelector(
|
||||
doc,
|
||||
`meta[name="fresh"][content="test"]`,
|
||||
);
|
||||
|
||||
// Still renders page
|
||||
assertSelector(doc, "body > .inner-body > .page");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("don't duplicate <title>", async () => {
|
||||
await withFresh(
|
||||
"./tests/fixture_explicit_app/main.ts",
|
||||
async (address) => {
|
||||
const doc = await fetchHtml(`${address}/title`);
|
||||
assertTextMany(doc, "title", ["foo bar"]);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -4,7 +4,7 @@
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1",
|
||||
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.5",
|
||||
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.3.1"
|
||||
},
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0"
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0"
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env -S deno run -A --watch=static/,routes/
|
||||
|
||||
import dev from "$fresh/dev.ts";
|
||||
|
||||
await dev(import.meta.url, "./main.ts");
|
||||
@@ -0,0 +1,21 @@
|
||||
// DO NOT EDIT. This file is generated by Fresh.
|
||||
// This file SHOULD be checked into source version control.
|
||||
// This file is automatically updated during development when running `dev.ts`.
|
||||
|
||||
import * as $0 from "./routes/_app.tsx";
|
||||
import * as $1 from "./routes/head.tsx";
|
||||
import * as $2 from "./routes/index.tsx";
|
||||
import * as $3 from "./routes/title.tsx";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
"./routes/_app.tsx": $0,
|
||||
"./routes/head.tsx": $1,
|
||||
"./routes/index.tsx": $2,
|
||||
"./routes/title.tsx": $3,
|
||||
},
|
||||
islands: {},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
export default manifest;
|
||||
@@ -0,0 +1,10 @@
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.asynciterable" />
|
||||
/// <reference lib="deno.ns" />
|
||||
/// <reference lib="deno.unstable" />
|
||||
|
||||
import { start } from "$fresh/server.ts";
|
||||
import manifest from "./fresh.gen.ts";
|
||||
|
||||
await start(manifest);
|
||||
@@ -0,0 +1,25 @@
|
||||
import { AppProps, Handler } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handler = (_req, ctx) => {
|
||||
ctx.state.lang = "de";
|
||||
return ctx.render();
|
||||
};
|
||||
|
||||
export default function App(
|
||||
{ Component, state }: AppProps<unknown, { lang: string }>,
|
||||
) {
|
||||
return (
|
||||
<html lang={state.lang}>
|
||||
<head>
|
||||
<meta charSet="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>fresh title</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="inner-body">
|
||||
<Component />
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Head } from "$fresh/runtime.ts";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div class="page">
|
||||
<Head>
|
||||
<meta name="fresh" content="test" />
|
||||
</Head>
|
||||
/head
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export default function Home() {
|
||||
return (
|
||||
<div class="page">
|
||||
Home
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Head } from "$fresh/runtime.ts";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div class="page">
|
||||
<Head>
|
||||
<title>foo bar</title>
|
||||
</Head>
|
||||
/title
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0"
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.16.0",
|
||||
"preact/": "https://esm.sh/preact@10.16.0/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1",
|
||||
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
|
||||
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
|
||||
},
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1",
|
||||
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
|
||||
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
|
||||
},
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0"
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0"
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.11.0",
|
||||
"preact/": "https://esm.sh/preact@10.11.0/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1",
|
||||
"@preact/signals": "https://esm.sh/*@preact/signals@1.0.3",
|
||||
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.0.1"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user