-
Notifications
You must be signed in to change notification settings - Fork 23
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
should URLPattern match a trailing "/" by default like path-to-regexp? #14
Comments
To clarify, in
I think generally the web expects 1 somewhat, but 2 is likely a historical mistake to support. However, the issue with 1 and the current API proposal is that it makes explicit matching impossible so it may be worth using only "strict" mode. It could be possible to force people to be explicit with the trailing slash so they can mirror their server behavior (maybe with |
In #22 @blakeembrey suggests a possible alternative to the magic optional prefix behavior. Instead of writing something like:
You would instead use a new grouping mechanism to scope what the modifier applies to:
Since the I'm not sure this would allow us to specify a valid trailing "/", though. @jakearchibald would this make any of your use cases easier? |
Updating the issue title to reflect that this is mainly about the path-to-regexp non-strict default mode. I think for service worker scopes we definitely need strict mode. (We don't accept an optional trailing delimiter if it wasn't explicitly included in the pattern.) For URLPattern js use I'm unsure what to do.
In either case it seems we probably need an option to URLPattern to override the default. I think @domenic was in favor of aligning with the platform here. |
Just an observation. In regards to the original post in this issue, this example: new URLPattern({ pathname: '/foo/:bar?' }); Only doesn't match
If we used a custom regexp to not require at least one character in new URLPattern({ pathname: '/foo/:bar([^\/]*)?' }); Then it does match |
FWIW, it seems reasonable to make a trailing slash optional and match either way, but requiring a trailing slash and requiring it not to be there should also be possible. |
Yea. I think its easier, though, to default to strict mode then since you can add an optional trailing slash in your pattern directly. In the opposite case where strict is disabled by default, though, there is no way to write a pattern that says "disable optional trailing slash behavior". Instead it would require a "enable strict mode" option outside of the pattern. |
(I don't think I'm a good help with the exact syntax, regular expressions always confuse me.) I wonder if we should write down some scenarios to see if the syntax can handle them. E.g., you might want to match
but not
(It might also be interesting to think about this from the perspective of the model of a URL, where path is a list of strings. So you could think about how many items you would want to have in the list, regardless of whether they are the empty string or not.) |
Do accomplish the above in strict mode you would have to use a custom regexp group like:
You could make this a named group with the custom regexp like:
I don't think you can accomplish the above example in non-strict mode since it will always match a trailing slash which is one of your excluded cases. You could meet all the other constraints in non-strict mode with I think at this point I am planning to make a go at using strict mode as the default without an option to disable it. If we get feedback that its necessary we can add the option (or something else more extreme if the feedback is very loud). |
I also realized if a developer wants non-strict behavior they can easily achieve it by appending |
The only tricky thing would be if many developers want to use a trailing |
Note, I opened a github "discussion" to collect community feedback on this issue. See #38. |
I think we've decided to go with strict mode. |
This has been codified in the spec. |
During the call in w3c/ServiceWorker#1535 we discussed the optional prefix behavior. For example:
Would match:
But would not match:
The main point of the feature is to avoid matching the
/foobar
here, but not matching/foo/
may be a problem for sites that normalize URLs with a trailing slash.This discrepancy could be because the proposal is currently based on "strict" path-to-regexp mode instead of "default" mode. In "default" mode a trailing "/" is always permitted.
This issue is to figure out how to handle this. Should we switch the proposal to use default mode which is what most developers use? Should we do something different for optional prefix behavior?
The text was updated successfully, but these errors were encountered: