Skip to content

Commit

Permalink
Merge pull request #69 from dpikt/custom-index
Browse files Browse the repository at this point in the history
Allow custom index filename
  • Loading branch information
zachleat authored Apr 19, 2024
2 parents fc4c3cb + 3e89d12 commit e16ddc9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
8 changes: 5 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const DEFAULT_OPTIONS = {
pathPrefix: "/", // May be overridden by Eleventy, adds a virtual base directory to your project
watch: [], // Globs to pass to separate dev server chokidar for watching
aliases: {}, // Aliasing feature
indexFileName: "index.html", // Allow custom index file name

onRequest: {}, // Maps URLPatterns to dynamic callback functions that run on a request from a client.

// Example:
Expand Down Expand Up @@ -271,7 +273,7 @@ class EleventyDevServer {
};
}

let indexHtmlPath = this.getOutputDirFilePath(url, "index.html");
let indexHtmlPath = this.getOutputDirFilePath(url, this.options.indexFileName);
let indexHtmlExists = fs.existsSync(indexHtmlPath);

let htmlPath = this.getOutputDirFilePath(url, ".html");
Expand Down Expand Up @@ -795,8 +797,8 @@ class EleventyDevServer {
let urls = [];
urls.push(path);

if(path.endsWith("/index.html")) {
urls.push(path.slice(0, -1 * "index.html".length));
if(path.endsWith(`/${this.options.indexFileName}`)) {
urls.push(path.slice(0, -1 * this.options.indexFileName.length));
} else if(path.endsWith(".html")) {
urls.push(path.slice(0, -1 * ".html".length));
}
Expand Down
12 changes: 12 additions & 0 deletions test/stubs/custom-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title></title>
</head>
<body>
<code>test/stubs/custom-index.html</code>
</body>
</html>
Empty file.
29 changes: 28 additions & 1 deletion test/testServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,31 @@ test("pathPrefix without leading or trailing slash", async (t) => {
});

server.close();
});
});

test("indexFileName option: serve custom index when provided", async (t) => {
let server = new EleventyDevServer("test-server", "./test/stubs/", { indexFileName: 'custom-index.html' });

t.deepEqual(server.mapUrlToFilePath("/"), {
statusCode: 200,
filepath: testNormalizeFilePath("test/stubs/custom-index.html"),
});


t.deepEqual(server.mapUrlToFilePath("/route1/"), {
statusCode: 200,
filepath: testNormalizeFilePath("test/stubs/route1/custom-index.html"),
});

server.close();
});

test("indexFileName option: return 404 when custom index file doesn't exist", async (t) => {
let server = new EleventyDevServer("test-server", "./test/stubs/", { indexFileName: 'does-not-exist.html' });

t.deepEqual(server.mapUrlToFilePath("/"), {
statusCode: 404,
});

server.close();
});

0 comments on commit e16ddc9

Please sign in to comment.