vercel / serve

Static file serving and directory listing
https://npmjs.com/package/serve
MIT License
9.36k stars 691 forks source link

`.html` is stripped from URL #732

Open trusktr opened 2 years ago

trusktr commented 2 years ago

Description

When visiting localhost:3000/foo.html, serve strips the .html so it becomes localhost:3000/foo.

Library version

14.0.1

Node version

v16.13.2

mdtausifiqbal commented 1 year ago

Same Issue

When visiting localhost:3000/foo.html?query=bar serve strips the .html?query=bar

Library version is 14.0.1

mdtausifiqbal commented 1 year ago

@trusktr I found that this issue belongs to the core of serve that is serve-handler

We should create an Issue in that repository

trusktr commented 1 year ago

new issue: https://github.com/vercel/serve-handler/issues/178

thorsent commented 1 year ago

I think cleanUrls: false avoids this behavior.

Glidias commented 1 year ago

I tried to run this from serve.json file with the CLI command, but doesn't appear to have any effect.

{
    "cleanUrls": false
}

hmm, not sure why or issit just me...

bramus commented 1 year ago

@Glidias I got it working by with this command:

serve src --config ../.serve.json

(serve expects the JSON file in the src dir, but I’ve got it in the root of the project repo so that’s why I needed to prepend ../ to the path)

and this .serve.json

{
    "cleanUrls": false,
    "redirects": [{ "source": "/", "destination": "/index.html", "type": 301 }]
}

However, I’ve noticed thatserve is extremely unreliable with a config like this and would not serve some .html files, returning a 404 for them, even though if they do exist. Appending a querystring to the URLs would serve those files just fine, but that’s a hacky workaround.

In the end I turned out ditching serve in favor of http-server which does play nice with plain html files.

http-server src -p 3000 -d false --no-dotfiles
Glidias commented 1 year ago

@bramus I actually had the serve.json file at the root of the serving directory, so i thought it should had worked. However, I got it to work after changing the port setting and also another person had it worked fine. I confirmed this was likely because of browser cache due to cached 301 redirects.

ADTC commented 1 year ago

@bramus Thanks. I thought this serve utility is a simple HTTP server, but it has the hidden "feature" of rewriting paths to .html files to remove the extension. It also rewrites paths ending in / to not end in the slash. Honestly this should have been off by default, or at least easy to turn off via a command line configuration.

I'm ditching it in favor of http-server as well, although its default 404 handling is out of whack.

kungfooman commented 1 year ago

A config file in src seems also awkward... anyway, @bramus answer solved it for me, thank you!