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

URL rewrite functionality #149

Open
pavel opened this issue Oct 14, 2016 · 10 comments
Open

URL rewrite functionality #149

pavel opened this issue Oct 14, 2016 · 10 comments

Comments

@pavel
Copy link
Contributor

pavel commented Oct 14, 2016

From #142, #128 and presence of --spa option there's a need for URL rewriting mechanism.

@pavel
Copy link
Contributor Author

pavel commented Oct 14, 2016

I have an implementation here: https://github.com/pavel/live-server/tree/rewrite. I'll create a PR after some testing and feedback gathering.
@jackfranklin Please, take a look, and let me know if it works for you in SPA case.

@jackfranklin
Copy link

@pavel to me this doesn't feel like a very "Node" solution. It just seems way less user friendly than having a function that is called with the URL and can act accordingly. I also can't immediately figure out how I would apply the rewrites I've done using these rules. If you can show me that it might help clear it up.

I do think it's a shame though that people will have to be familiar with / learn modrewrite rules in order to benefit from this feature.

@pavel
Copy link
Contributor Author

pavel commented Oct 14, 2016

@jackfranklin I'll try to keep it short :)

to me this doesn't feel like a very "Node" solution.

What I would like to avoid is feelings. Let me expand on this. It might not feel like a "Node" solution due to:

  • not being abstract enough to cover all of the cases of rewrite you can image;
  • not providing you with certain level of control.

But! It does cover some variety of cases allowing people to get things done. Think of NGINX, it does not provide you a way to specify functions or processing mechanisms for URL rewrite in its config. Though it covers majority of needs using string rules in its config.

It just seems way less user friendly than having a function that is called with the URL and can act accordingly.

If we go with functions, then how do you imagine specifying URL rewrite via CLI?
Also, please, note that live-server does provide you with a mechanism of extension through specifying your own middleware. See this for an example (last line of the params object).

I also can't immediately figure out how I would apply the rewrites I've done using these rules.

I don't quite get your message here. You mean it is not clear what rules to use? Are you confused by rules syntax?

Here's a CLI usage example for --spa replacement (with asset handling) in bash:

live-server \
  --rewrite="/.+\..+$ - [L]" \
  --rewrite="^/(.+)$ /#/\$1 [R,L]"
  1. Expanding on this rule: "/.+\..+$ - [L]"
    1. /.+\..+$ this regexp, matches any URL that has an extension in it (like ".css", ".js")
    2. - this dash tells connect-modrewrite to skip rewrite.
    3. [L] flag tells connect-modrewrite to stop processing any subsequent rules.
    4. What we've got from this one? Any URL that has an extension (which semantically means asset in this case), will not be changed and processed by connect-modrewrite.
  2. Expanding on this rule: /(.+)$ /#/\$1 [R,L]
    1. ^/(.+)$ this regexp, matches anything after first slash "/" (e.g. for "/test/hello", the regexp will match "test/hello")
    2. /#/\$1 this rewrite rule, says take all matched stuff and put it after "/#/" (making URL "/test/hello" to be rewritten to "/#/test/hello")
    3. [R,L] this flags tell connect-modrewrite to issue a redirect (302 by default), and stop processing any subsequent rules.
    4. What we've got from this one? Our rule transforms all URL that have no extension to SPA form.

Don't get me wrong. I don't say that this is a perfect solution. I do understand that this is kind of a tricky thing to figure out. And the complexity of rules is bound to your project's URL handling complexity. And at the point when complexity is too high, you might want to go with your own middleware in live-server.

This SPA rewrite rules I've provided are not generic, they might not work for some projects out there. You'll have to tweak it to your needs, depending on what you consider "assets" and what you consider "SPA URLs".

@jackfranklin
Copy link

@pavel I think we'll have to agree to disagree here ;)

My argument really is that rather than use a set of rules that some Node devs may or may not understand (I consider myself good at JS, but I don't know these rules), you could instead allow a custom function to be passed that is called with the URL. I think that's a pattern a lot of modules follow and I would expect more Node devs to be comfortable with that (I may be wrong of course).

I would then allow command line usage via a --require flag, so:

live-server --require rewrite-module

Which would require('rewrite-module')(url) for each URL (caching the require).

That's my 2c - but I can see both sides of the argument :)

@pavel
Copy link
Contributor Author

pavel commented Oct 17, 2016

@jackfranklin I guess I'm limited on this topic here.
What is rewrite-module? Is a custom rewrite solution? How is it different from custom middleware?

@jackfranklin
Copy link

Just a custom rewrite module people could build that would expected to export a function that will be called to determine if a URL should be rewritten or not.

@tapio
Copy link
Owner

tapio commented Oct 17, 2016

URL rewrite functionality is generally useful and commonly used feature so thanks for working on that. As for the spa use case, I wrote my thoughts to #142 (comment)

@pavel
Copy link
Contributor Author

pavel commented Oct 17, 2016

@tapio Should I continue working on this solution or wait till we can get the middleware solution for #150?

@tapio
Copy link
Owner

tapio commented Oct 18, 2016

We can refactor later if we want to move more stuff to separate files. From the looks of your branch, this functionality is already nicely tidy, mostly just installing a middleware and handling the cli.

@ghost
Copy link

ghost commented May 25, 2017

--spa doesn't work, still getting 404s. Also using --mount.

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

No branches or pull requests

3 participants