lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.21k stars 85 forks source link

Configuring Single Page App mode #36

Closed tpluscode closed 7 years ago

tpluscode commented 8 years ago

Hi. I would like to use SPA mode to test my app. I my case the paths are entire URIs, for example

http://localhost:8000/http://some.uri.in.path

When I start the web server with ws -s index.html, such requests are not rewritten. ws logs a message saying

Not rewriting GET /http://some.uri.in.path because the path includes a dot (.) character

I understand it works under the assumption that dot signifies static content?

I tried --rewrite /* -> index.html but this causes any request, even when static files which exist, to respond with index.html.

75lb commented 8 years ago

I understand it works under the assumption that dot signifies static content

yes, true.. this rule prevents requests for static files like app.js returning index.html.

In my case the paths are entire URIs

i must admit, yours is a rare use case.. i've seen paths sent in the querystring (e.g. http://localhost:8000/dashboard?path=http://some.uri.in.path) or POST data, but never as in your case.

How would you like to see this resolved?

75lb commented 8 years ago

i suppose we need the ability to define the pattern which identifies whether a path is for an asset (app.js) or not (/user/detail/1).. at the moment, the pattern is "does the path contain a full-stop".. you would require a different pattern..

tpluscode commented 8 years ago

How would you like to see this resolved?

I expected this to be very simple. For a given path /x/y/z.html

  1. If file /x/y/z.html exists return it
  2. If not, return the -spa parameter value

And actually this could be achieved explicitly with the greedy --rewrite /* -> index.html. You would only need to check that the rewritten path isn't actually a physical file.

How about that?

75lb commented 8 years ago

What you describe was the original behaviour until another user highlighted that a missing asset, e.g. an image or script should fail and return 404. But with the behaviour you described, a request for an image that's currently missing would return the SPA document. Not ideal.

I will find a middle ground to resolve this.

tpluscode commented 8 years ago

I see how either way can cause trouble. It is true that my current requirement is unusual. I did try to explicitly narrow down the rewrite so that it only matches an URL in path: --rewrite '/http://* -> index.html'. This however acts just like the greedy pattern. Maybe making it work would make everyone happy for now?

web-padawan commented 8 years ago

AFAIK koa-connect-history-api-fallback has an option for rewrites. Not sure if it can help. I'd rather pass redirect url as a query parameter (actually I do it this way in my current project using SPA). The url from given example is not a valid url itself, so making it work is unlikely to be a good solution.

75lb commented 8 years ago

this is fixed in v2 of the tool, will let you know when it's ready.

75lb commented 7 years ago

this is resolved by the --spa.asset-test option in v2 which is in progress and available for preview.