enonic / lib-router

Router Library for Enonic XP.
Apache License 2.0
0 stars 1 forks source link

Expand possibilities for URL patterns #37

Closed espen42 closed 3 years ago

espen42 commented 3 years ago

Lib-router can be set up with a catch-group pattern, for example libRouter.get('/asset/{path:.+}', req => .... This will respond to requests to urls below that, such as /asset/css/styles.css etc, and add .pathParams.path to the request object, with whatever value is after the slash ("css/styles.css").

But this pattern requires both the slash (after asset) and something more after that in the url. If there's no slash-separated sub-uri, this route won't kick in at all. lib-static has a usage scenario (index fallbacks) which requires a route to not only catch sub-uris this way, but also the root itself - with a trailing slash and without one (and to be able to separate between those scenarios in the resultingrequest object):

It seems impossible to define a catch-group pattern for this in lib-router?

This is possible with controller mappings, though: they are able to use a regex pattern like <pattern>/asset(/.*)?$</pattern>. It seems lib-router is just a bit more picky on the patterns it accepts.

From the point of view of lib-static, an ideal solution would be to allow a pattern to start without a slash before it so that the slash can be part of the group - for example libRouter.get('/asset{path: /.+}', req => ...

rymsha commented 3 years ago

Current implementation strips away trailing slashes - so it is impossible to defile a pattern that would match /asset/, but not /asset With a fix, developers can write pattern /asset/? - that would be (almost) like old behavior. /asset - won't match /asset/ anymore