Skip to content

Commit

Permalink
Merge pull request #66 from thefrontside/fix-base-redirects
Browse files Browse the repository at this point in the history
Fix bug in base redirects
  • Loading branch information
cowboyd authored Nov 6, 2023
2 parents 5a84250 + 52b4b38 commit 40c72a3
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 85 deletions.
5 changes: 4 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

182 changes: 105 additions & 77 deletions www/deno.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions www/import_map.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"imports": {
"$fresh/": "https://deno.land/x/[email protected]/",
"preact": "https://esm.sh/preact@10.11.0",
"preact/": "https://esm.sh/preact@10.11.0/",
"preact-render-to-string": "https://esm.sh/*[email protected]",
"preact": "npm:preact@10.18.2",
"preact/": "npm:/preact@10.18.2/",
"preact-render-to-string": "npm:[email protected]",
"@preact/signals": "https://esm.sh/*@preact/[email protected]",
"@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
"twind": "https://esm.sh/[email protected]",
Expand Down
6 changes: 5 additions & 1 deletion www/routes/docs/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export const handler: Handlers<Data> = {
const fileContent = await Deno.readTextFile(url);
const { body, attrs } = frontMatter<Record<string, unknown>>(fileContent);
const page = { ...entry, markdown: body, data: attrs ?? {} };
const resp = ctx.render({ page, base: req.headers.get("x-base") });
let base = req.headers.get("x-base-url") ?? "/";
if (!base.endsWith("/")) {
base = base + "/"
}
const resp = ctx.render({ page, base });
return resp;
},
};
Expand Down
8 changes: 6 additions & 2 deletions www/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { join } from "https://deno.land/[email protected]/path/join.ts";
import { Handlers } from "$fresh/server.ts";

export const handler: Handlers = {
GET(req) {
let base = req.headers.get("x-base") ?? req.url;
return Response.redirect(new URL("./docs/introduction", base));
let base = req.headers.get("x-base-url") ?? req.url;
let url = new URL(base);
url.pathname = join(url.pathname, "docs/introduction");
let response = Response.redirect(url);
return response;
},
};
2 changes: 1 addition & 1 deletion www/test/www.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("www", () => {
it("sets a base element if the X-Base header is present", async () => {
let response = await fetch("http://localhost:9999/docs/introduction", {
headers: {
"X-Base": "http://example.com/path/to/subdir/",
"X-Base-Url": "http://example.com/path/to/subdir/",
},
});
let html = await response.text();
Expand Down

0 comments on commit 40c72a3

Please sign in to comment.