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

Backreferences in rewrites? #27

Closed
bard opened this issue Jun 13, 2018 · 7 comments
Closed

Backreferences in rewrites? #27

bard opened this issue Jun 13, 2018 · 7 comments

Comments

@bard
Copy link

bard commented Jun 13, 2018

I'm trying to rewrite everything under /admin, e.g.

  • /admin => /
  • /admin/index.html => /index.html
  • /admin/static/style.css => /static/style.css
  • /admin/static/main.js => /static/main.js

...and so on.

Can this be accomplished using rewrites in serve.json without specificying every possible path?

I tried the following but no luck:

{
  "rewrites": [{
    "source": "/admin/(.*)",
    "destination": "/$1"
  }]
}
@leo
Copy link
Contributor

leo commented Jun 13, 2018

Please try path segments, as described in the documentation:

{
  "rewrites": [{
    "source": "/admin/:suffix",
    "destination": "/:suffix"
  }]
}

@bard
Copy link
Author

bard commented Jun 13, 2018

Thanks, I had seen that, but I assumed it would work as in Express routes in that it matches only one path component (i.e. would match /foo/:bar matches /foo/index.js but not /foo/static/index.js) and indeed it looks like that's the case:

/tmp/test$ ls -R public/
public/:
index.js  static

public/static:
style.css

/tmp/test$ cat serve.json
{
  "public": "public",
  "rewrites": [
    {
      "source": "/admin/:rest", "destination": "/:rest"
    }
  ]
}

/tmp/test$ serve &
[1] 17823
INFO: Discovered configuration in `serve.json`
INFO: Accepting connections at http://localhost:3000

/tmp/test$ curl http://localhost:3000/index.js
HTTP/1.1 200 OK
[...]
// this is /index.js

/tmp/test$ curl http://localhost:3000/admin/index.js
HTTP/1.1 200 OK
[...]
// this is /index.js
 
/tmp/test$ curl http://localhost:3000/static/style.css
HTTP/1.1 200 OK
[...]
/* this is /static/style.css */

/tmp/test$ curl http://localhost:3000/admin/static/style.css
HTTP/1.1 404 Not Found
[...]
<!DOCTYPE html><head> <meta name="viewport" content="width=device-width, [...]

@leo
Copy link
Contributor

leo commented Jun 15, 2018

Yes, we implemented it a little differently because it was important for us to allow rewriting the entire ending of a path to a different one. If you want to restrict it, please create more rewrites afterwards that point the other paths to the right one.

For every request, all rewrites that match the source are applied - so that should be easy. 👍

@erfangc
Copy link

erfangc commented Sep 6, 2018

this does not work if you are running serve -s as for some reason before the rewrites can happen serve-handler would actually end up serving index.html instead

user:~/environment/serve $ ls -lR
.:
total 8
drwxrwxr-x 3 ec2-user ec2-user 4096 Sep  6 15:52 public
-rw-rw-r-- 1 ec2-user ec2-user  114 Sep  6 18:23 serve.json

./public:
total 12
-rw-rw-r-- 1 ec2-user ec2-user  182 Sep  6 16:57 index.html
-rw-rw-r-- 1 ec2-user ec2-user   23 Sep  6 15:44 index.js
drwxrwxr-x 2 ec2-user ec2-user 4096 Sep  6 15:44 static

./public/static:
total 4
-rw-rw-r-- 1 ec2-user ec2-user 24 Sep  6 18:20 style.css
serve -s
curl localhost:8080/admin/index.js 
# Content of index.html instead

@AndyOGo
Copy link

AndyOGo commented Apr 30, 2020

path-to-regexp supports repeated segments

const toPathRepeated = compile("/:segment+");

toPathRepeated({ segment: "foo" }); //=> "/foo"
toPathRepeated({ segment: ["a", "b", "c"] }); //=> "/a/b/c"

I think that should be the solution 🤔

{
  "rewrites": [{
    "source": "/admin/:rest+",
    "destination": "/:rest"
  }]
}

@AaronBeaudoin
Copy link

@AndyOGo's solution looks like it wasn't tested, because with it I still get the same issue.

Here is my abridged serve.json:

{
  ...
  "rewrites": [
    {
      "source": "/some/path/:path+",
      "destination": "/:path"
    }
  ]
}

And here is my abridged curl output after running npx serve:

user@Machine project-root % curl -I http://localhost:3000/favicon.png                           
HTTP/1.1 200 OK
...

user@Machine project-root % curl -I http://localhost:3000/some/path/favicon.png      
HTTP/1.1 200 OK
...

user@Machine project-root % curl -I http://localhost:3000/assets/vendor.822f96e6.js                           
HTTP/1.1 200 OK
...

user@Machine project-root % curl -I http://localhost:3000/some/path/assets/vendor.822f96e6.js
HTTP/1.1 404 Not Found
...

@whydna
Copy link

whydna commented Jan 14, 2022

So is there no way to do this?

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

No branches or pull requests

6 participants