feat(vite): serve static index.html files (#3358)
Fixes https://github.com/denoland/fresh/issues/3356
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>ok</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -52,6 +52,21 @@ export function devServer(): Plugin[] {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
// Check if it's a static/index.html file
|
||||
const staticFilePathIndex = path.join(
|
||||
publicDir,
|
||||
url.pathname.slice(1),
|
||||
"index.html",
|
||||
);
|
||||
try {
|
||||
const content = await Deno.readTextFile(staticFilePathIndex);
|
||||
nodeRes.setHeader("Content-Type", "text/html; charset=utf-8");
|
||||
nodeRes.end(content);
|
||||
return;
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
const mod = await server.ssrLoadModule("fresh:server_entry");
|
||||
|
||||
try {
|
||||
|
||||
@@ -308,6 +308,19 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
hash: null,
|
||||
pathname: relative,
|
||||
});
|
||||
|
||||
if (path.basename(relative) === "index.html") {
|
||||
const htmlRelative = path.relative(
|
||||
publicDir,
|
||||
path.dirname(entry.path),
|
||||
);
|
||||
|
||||
staticFiles.push({
|
||||
filePath,
|
||||
hash: null,
|
||||
pathname: htmlRelative,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -431,3 +431,19 @@ Deno.test({
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite build - static index.html",
|
||||
fn: async () => {
|
||||
await launchProd(
|
||||
{ cwd: viteResult.tmp },
|
||||
async (address) => {
|
||||
const res = await fetch(`${address}/test_static/foo`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("<h1>ok</h1>");
|
||||
},
|
||||
);
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
@@ -392,3 +392,14 @@ Deno.test({
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite dev - static index.html",
|
||||
fn: async () => {
|
||||
const res = await fetch(`${demoServer.address()}/test_static/foo`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("<h1>ok</h1>");
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user