http-server icon indicating copy to clipboard operation
http-server copied to clipboard

Add --spa option

Open johannesloetzsch opened this issue 4 years ago • 5 comments

Allows serving of SPAs

  • with correct status code 200
  • without using the defekt Catch-all redirect
Relevant issues

fixes issue #757

Contributor checklist
  • [ ] Provide tests for the changes (unless documentation-only)
  • [x] Documented any new features, CLI switches, etc. (if applicable)
    • [x] Server --help output
    • [x] README.md
    • [ ] doc/http-server.1 (use the same format as other entries)
  • [x] The pull request is being made against the master branch
Maintainer checklist
  • [ ] Assign a version triage tag
  • [ ] Approve tests if applicable

johannesloetzsch avatar Dec 03 '21 15:12 johannesloetzsch

SPA support has been discussed numerous times over the years. See especially https://github.com/http-party/http-server/pull/369#issuecomment-327291011, but there's further discussion in #369, #405 and #194 (there are a few others, but they didn't add much to the discussion).

Although this PR is small, I'm concerned that adding an --spa option would open the door to needing to support all the rest of the functionality of an SPA.

Does this PR add anything other than an approach to solving #757?

thornjad avatar Dec 06 '21 21:12 thornjad

Thank you @thornjad for linking the related issues :)

My motivation was solving the trouble related to #757. I don't insist in this pull request to be the chosen solution. All options I commented would be fine for me.

My reasoning for implementing an --spa option is, that the currently in the documentation suggested workarounds (404.html and Catch-all redirect) are either not conform to the http standard or come with an unnecessary performance overhead. This pull request shows, that an correct implementation can be shorter (only counted the changes to lib/core/index.js, not the boilerplate for option parsing) than the description of how to apply the workarounds at README.md.

I would argue, the amount of attempts to implement such an option show, that already a minimal solution would be very appreciated. Unfortunately the inked alternatives are all unmaintained.

You wrote:

I'm concerned that adding an --spa option would open the door to needing to support all the rest of the functionality of an SPA.

Could you elaborate what other functionality for serving an SPA might be requested?

johannesloetzsch avatar Dec 07 '21 00:12 johannesloetzsch

I personally do not care what this function is called. If it's called page404 or otherwise, I'll be happy to use it too. This is more convenient than copying the index file and renaming it every time.

dartess avatar Mar 11 '22 13:03 dartess

I personally do not care what this function is called. If it's called page404 or otherwise, I'll be happy to use it too. This is more convenient than copying the index file and renaming it every time.

There is another major advantage of using the solution from this pull request, compared to the 404.html workaround: The --spa option returns the correct HTTP status code. This is relevant for CI, Monitoring and other interaction via scripts.

johannesloetzsch avatar Mar 11 '22 14:03 johannesloetzsch

+1 could greatly use an SPA-functionality.

Need http-server as opposed to 'serve' because http-server supports Range Requests in bytes, which are needed for loading a static sqlite3 db on-demand using wa-sqlite.

Tails avatar Apr 29 '22 19:04 Tails