From c32f0cf9ce1b26c5b19ecb4d3148b49cf1bf53d8 Mon Sep 17 00:00:00 2001 From: Lino Le Van <11367844+lino-levan@users.noreply.github.com> Date: Tue, 11 Jul 2023 05:42:29 -0700 Subject: [PATCH] fix: trailingSlash option (#1426) --- src/server/context.ts | 11 ++++++++++- tests/fixture_router/static/foo.txt | 1 + tests/trailing_slash_test.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/fixture_router/static/foo.txt diff --git a/src/server/context.ts b/src/server/context.ts index a9142419..937c0d0a 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -403,7 +403,16 @@ export class ServerContext { headers: { location }, }); } else if (trailingSlashEnabled && !url.pathname.endsWith("/")) { - return Response.redirect(url.href + "/", Status.PermanentRedirect); + // If the last element of the path has a "." it's a file + const isFile = url.pathname.split("/").at(-1)?.includes("."); + + // If the path uses the internal prefix, don't redirect it + const isInternal = url.pathname.startsWith(INTERNAL_PREFIX); + + if (!isFile && !isInternal) { + url.pathname += "/"; + return Response.redirect(url, Status.PermanentRedirect); + } } return await withMiddlewares(req, connInfo, inner); diff --git a/tests/fixture_router/static/foo.txt b/tests/fixture_router/static/foo.txt new file mode 100644 index 00000000..ba0e162e --- /dev/null +++ b/tests/fixture_router/static/foo.txt @@ -0,0 +1 @@ +bar \ No newline at end of file diff --git a/tests/trailing_slash_test.ts b/tests/trailing_slash_test.ts index 63fa5231..1df29a1f 100644 --- a/tests/trailing_slash_test.ts +++ b/tests/trailing_slash_test.ts @@ -1,3 +1,4 @@ +import { INTERNAL_PREFIX } from "../runtime.ts"; import { ServerContext, Status } from "../server.ts"; import { assert, assertEquals } from "./deps.ts"; import manifest from "./fixture_router/fresh.gen.ts"; @@ -30,3 +31,30 @@ Deno.test("forwards slash placed at the end of url", async () => { // forwarded location should be with trailing slash assertEquals(resp.headers.get("location"), targetUrl + "/"); }); + +Deno.test("forwards slash placed at the end of url with hash and query string", async () => { + const targetUrl = "https://fresh.deno.dev/about"; + const queryAndHash = "?demo=test#what"; + const resp = await router(new Request(targetUrl + queryAndHash)); + assert(resp); + assertEquals(resp.status, Status.PermanentRedirect); + // forwarded location should be with trailing slash + assertEquals(resp.headers.get("location"), targetUrl + "/" + queryAndHash); +}); + +Deno.test("forwards slash not placed at the end of url with prefix", async () => { + const targetUrl = `https://fresh.deno.dev${INTERNAL_PREFIX}/no_redirect`; + const resp = await router(new Request(targetUrl)); + assert(resp); + // we should get a 404 and not a redirect + assertEquals(resp.status, Status.NotFound); +}); + +Deno.test("forwards slash not placed at the end of url for static file", async () => { + const targetUrl = `https://fresh.deno.dev/foo.txt`; + const resp = await router(new Request(targetUrl)); + assert(resp); + // we should not be getting a redirect + assertEquals(resp.status, Status.OK); + assertEquals(await resp.text(), "bar"); +});