Skip to content

Commit

Permalink
fix(redirects): External Redirect support (#129)
Browse files Browse the repository at this point in the history
* fix(redirects): External Redirect support

Signed-off-by: Levente Kurusa <lkurusa@kernelstuff.org>

* FMT

Signed-off-by: Levente Kurusa <lkurusa@kernelstuff.org>

---------

Signed-off-by: Levente Kurusa <lkurusa@kernelstuff.org>
  • Loading branch information
levex committed Apr 23, 2023
1 parent 0a43328 commit b6cf193
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@ export function redirects(redirectMap: Record<string, string>): BlogMiddleware {
}

if (maybeRedirect) {
if (!maybeRedirect.startsWith("/")) {
if (
!maybeRedirect.startsWith("/") && !(maybeRedirect.startsWith("http"))
) {
maybeRedirect = "/" + maybeRedirect;
}

Expand Down
11 changes: 11 additions & 0 deletions blog_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const BLOG_SETTINGS = await configureBlog(BLOG_URL, false, {
redirects({
"/to_second": "second",
"/to_second_with_slash": "/second",
"/external_redirect": "https://example.com",
"second.html": "second",
}),
],
Expand Down Expand Up @@ -183,6 +184,16 @@ Deno.test("posts/ trailing slash redirects", async () => {
await resp.text();
});

Deno.test("external redirects", async () => {
const resp = await testHandler(
new Request("https://blog.deno.dev/external_redirect"),
);
assert(resp);
assertEquals(resp.status, 307);
assertEquals(resp.headers.get("location"), "https://example.com");
await resp.text();
});

Deno.test("redirect map", async () => {
{
const resp = await testHandler(
Expand Down

0 comments on commit b6cf193

Please sign in to comment.