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

pattern.match does not match wildcard paths appropriately if missing trailing slash #24

Closed
niftylettuce opened this issue Apr 20, 2016 · 3 comments

Comments

@niftylettuce
Copy link

If we have a route, such as /snd/url-pattern and we do not have a trailing forward slash (in other words, if the route is not /snd/url-pattern/ with the trailing /), then it does not match the wildcard path of /snd/url-pattern/* as it should. Here is a code snippet:

var UrlPattern = require('url-pattern');
var pattern = new UrlPattern('/snd/url-pattern/*');
console.log(pattern.match('/snd/url-pattern')); // returns `null`

The only possible idea I have right now to fix this is to use this middleware in order to automatically 301 redirect to trailing slashes https://github.com/avinoamr/connect-slashes.

Do you think you can fix this in your package, or is there a more appropriate way for me to do this?

@snd
Copy link
Owner

snd commented Apr 20, 2016

hey nifty,

the behaviour of your code snippet is intended! that match should return null!
your pattern /snd/url-pattern/* expresses the following: match EXACTLY the string /snd/url-pattern/ followed by anything. it follows that pattern.match('/snd/url-pattern') returns null since it doesn't match the string /snd/url-pattern/ EXACTLY.

there is no special behaviour for wildcards next to trailing slashes. this is on purpose.

you can however make the slash and the wildcard optional by wrapping them in parentheses. this should do what you want:

var UrlPattern = require('url-pattern');
var pattern = new UrlPattern('/snd/url-pattern(/*)');
console.log(pattern.match('/snd/url-pattern')); // returns `{}`
console.log(pattern.match('/snd/url-pattern/')); // returns `{_: ''}`
console.log(pattern.match('/snd/url-pattern/abc')); // returns `{_: 'abc'}`

let me know if this solves your issue.

@sh
Copy link

sh commented Apr 20, 2016

Hey nifty,

If you do care about SEO your 301 redirect approach is definitely the best.
Having the same content served by 2 different urls, eg /foo and /foo/will result in duplicate content.

See this article Trailing slashes and SEO for a short explanation on why this is bad.

@niftylettuce
Copy link
Author

@sh good to know, thank you 👍

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

3 participants