lwsjs / local-web-server

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

How can I replace URLs in body? #153

Closed simplenotezy closed 3 years ago

simplenotezy commented 3 years ago

Really cool repo! Might just be what I am looking for.

I am in need of this repo, since I'd like cypress.io to test the static site after it has been built/compiled, but before publishing to production. The issue is that the /dist/ folder contains absolute URLs to the production site, and I'd like to rewrite these URLs from "https://myproductionsite.com" to "https://localhost:8000".

I have tried this with the --rewrite option, but not sure if that's what it is meant for.

This doesn't seem to work:

yarn ws --spa dist/index.html --rewrite '/https:\/\/jwr\.test/(.*) -> http://localhost:8000/$1'

(jwr.test is representing the production URL in this test case)

75lb commented 3 years ago

Yes, the --rewrite option is for URL rewriting.. So if a client requests index.html you could instead, with a rewrite rule, return new-index.html (for example).

There is a useful debugging tool to help build rewrite expressions: https://github.com/lwsjs/local-web-server/wiki/How-to-rewrite-URLs-to-local-or-remote-destinations#debugging

75lb commented 3 years ago

however, I don't think URL rewriting is what you're trying to achieve here - sounds like you want ws to edit the body of any HTML file containing a particular text string (in your case a URL).. ordinarily, a website would have a configuration file (or similar) which you could edit to "point" the web app at a different target servers.. I'm assuming you don't have this config file so yes, you would need a custom middleware to scan and edit the HTML files returned..

I will respond to your seperate issue on this topic..

simplenotezy commented 3 years ago

@75lb OK. I see. Yes that's exactly what I need, I need to rewrite the body raw HTML, as the build process takes ~30 minutes, and before running my deploy script, I'd like to run some tests. But I don't want to run the tests against the "production" URLs, so I need to rewrite this.

Looking forward to your reply, as I am more than eager to proceed with this 😊