whatwg / urlpattern

URL Pattern Standard
https://urlpattern.spec.whatwg.org/
Other
160 stars 22 forks source link

Base URL and empty pathname in URLPatternInit #200

Open sisidovski opened 1 year ago

sisidovski commented 1 year ago

What is the issue with the URL Pattern Standard?

When we construct with URLPatternInit, containing an empty string pathname and the regular baseURL string, the result is a bit unintuitive. When the pathname is empty, it makes sense to me that the result is entirely inherited from the base URL, or keep it as the empty string. Also, it makes sense if the result is consistent with the case when URLPatternInit only has baseURL and doesn't have pathname field.

However, current behavior shows an inconsistent result like below.

// Empty pathname
new URLPattern({pathname: '', baseURL: 'https://example.com/a/b/c'}).pathname
=> '/a/b/'

// baseURL only
new URLPattern({baseURL: 'https://example.com/a/b/c'}).pathname
=> '/a/b/c'

Also, this behaves differently from the URL API.

new URL('', 'https://example.com/a/b/c').pathname
=> '/a/b/c'

This behavior was introduced in https://github.com/whatwg/urlpattern/pull/107, and written in the step 17 in 3.2. URLPatternInit processing. I guess that was added to address the case when the pathname is a relative url such as *.jpg.

// Empty pathname
new URLPattern({pathname: '*.jpg', baseURL: 'https://example.com/a/b/c'}).pathname
=> '/a/b/*.jpg'

I wonder if we should process the step 17 if the pathname is empty, just inheriting the entire string from baseURL is intuitive, at least for me. Also, it would be nice to have the same behavior with the URL API not to surprise developers. WDYT?

cc: @jeremyroman @domenic