Skip to content

Commit

Permalink
fix: normalised based path
Browse files Browse the repository at this point in the history
  • Loading branch information
mxdvl committed Apr 28, 2023
1 parent 467c261 commit e49803e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ await rebuild();

if (flags.dev) {
const watcher = Deno.watchFs(site_dir);
serve(create_handler({ base: flags.base, build_dir }), { port: 4507 });
serve(create_handler({ base_path, build_dir }), { port: 4507 });
let timeout;
for await (const { kind, paths: [path] } of watcher) {
if (path && (kind === "modify" || kind === "create")) {
Expand Down
15 changes: 10 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { Handler } from "https://deno.land/[email protected]/http/server.ts";
import { serveDir } from "https://deno.land/[email protected]/http/file_server.ts";
import { normalize as normalise } from "https://deno.land/[email protected]/path/posix.ts";

interface ServerOptions {
base?: string;
base_path: string;
build_dir: string;
}

export const create_handler = (
{ base = "", build_dir }: ServerOptions,
{ base_path, build_dir }: ServerOptions,
): Handler => ((req) => {
const url = new URL(req.url);

if (url.pathname.startsWith("/" + base)) {
return serveDir(req, { fsRoot: build_dir, urlRoot: base });
const normalised_base_path = normalise("/" + base_path + "/");

if (url.pathname.startsWith(normalised_base_path)) {
return serveDir(req, { fsRoot: build_dir, urlRoot: base_path });
} else {
return Response.redirect(new URL(base + url.pathname, url.origin));
return Response.redirect(
new URL(normalise(normalised_base_path + url.pathname), url.origin),
);
}
});

0 comments on commit e49803e

Please sign in to comment.