Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal(http): add cleanUrls option to serveDir #6232

Open
lowlighter opened this issue Dec 4, 2024 · 1 comment
Open

proposal(http): add cleanUrls option to serveDir #6232

lowlighter opened this issue Dec 4, 2024 · 1 comment

Comments

@lowlighter
Copy link
Contributor

Is your feature request related to a problem? Please describe.

Nowadays it's pretty common to trim html page extensions from urls to make them nice and rememberable.
(e.g. deno.com/blog, github.com/denoland/std, ..., some hosters such as Vercel also propose a similar feature)

The reason for why I'd like it to be natively in the std function is:

  • it avoids to implementing routing for simple use-cases (SSG websites)
    • serveDir is often also used as a fallback for route(), but it makes it kind of ugly to use it as-is if you want the clean urls behavior (see workaround below)
  • workaround exists but not practical
    • change your file structure (can cause interoperability issues with other modules) to use directories instead, rename all your file as index.html and use the index option
    • serveDir once, check if response status is 404, then rewrite the request (but kind of troublesome since Request are immutable, and also may already have been consumed) and retry with the extension. while it works, feels hacky and probably poorly memory efficient as you need to create multiple up to 4 instances of objects to serve a single file

The patch is pretty small to implement, as you only need to add a second resolution check if the option is enabled, so the footprint would be pretty low

I feel like the use-case is common enough, and makes it easier to deploy smaller-scales app / docs website / SSG without having to code anything, as you'd be able to directly deno run -A jsr:@std/http/file_server , which will be a nice QOL

Describe the solution you'd like

If this get agreed on, #6231 can be reopened, here's the reference:

-  const fsPath = join(target, normalizedPath);
+  // Resolve path
+  // If cleanUrls is enabled, automatically append ".html" if not present
+  // and it does not shadow another existing file or directory
+  let fsPath = join(target, normalizedPath);
+  if (
+    cleanUrls && !fsPath.endsWith(".html") &&
+    !(await Deno.stat(fsPath).then(() => true).catch(() => false))
+  ) {
+    fsPath += ".html";
+  }
  const fileInfo = await Deno.stat(fsPath);

Describe alternatives you've considered

One of the workaround listed above

@pomdtr
Copy link

pomdtr commented Dec 11, 2024

I'm interested in this to. I use @std/http/file-server in smallweb, and multiple users have been asking for this exact feature.

Example frameworks using cleanurls by default:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants