Summary
When using the select plugin, relative apiUrl values throw an error
Steps to Reproduce
- Create a select element with a relative
apiUrl (e.g., /private/api/something).
- Initialize the select element.
When you do this, the initialization now throws an error on the first attempt to load data from the API:
TypeError: Failed to construct 'URL': Invalid URL
Demo Link
https://preline.co/plugins/advanced-select.html#js-data-options
Expected Behavior
I would expect that relative URLs should work.
Actual Behavior
It looks like the issue is that v3.1.0 of the plugin used the apiUrl as-is:
Beginning in v3.2.0 (and in all versions after), the plugin now creates a new URL object from the apiUrl value, which fails because the URL constructor does not accept relative URL paths:
|
const url = new URL(this.apiUrl); |
Given that this is running in the browser context, this could almost certainly be resolved by first using URL.parse to test whether the apiUrl value is parseable as-is, and if not, then trying to construct it with the current page's base URL:
let url;
if (URL.parse(this.apiUrl)) {
url = new URL(this.apiUrl);
} else {
url = new URL(this.apiUrl, window.location.origin);
}
Screenshots
No response
Summary
When using the select plugin, relative apiUrl values throw an error
Steps to Reproduce
apiUrl(e.g.,/private/api/something).When you do this, the initialization now throws an error on the first attempt to load data from the API:
TypeError: Failed to construct 'URL': Invalid URLDemo Link
https://preline.co/plugins/advanced-select.html#js-data-options
Expected Behavior
I would expect that relative URLs should work.
Actual Behavior
It looks like the issue is that v3.1.0 of the plugin used the
apiUrlas-is:preline/src/plugins/select/index.ts
Line 1158 in 069c821
Beginning in v3.2.0 (and in all versions after), the plugin now creates a new
URLobject from theapiUrlvalue, which fails because theURLconstructor does not accept relative URL paths:preline/src/plugins/select/core.ts
Line 1613 in 1cd9636
Given that this is running in the browser context, this could almost certainly be resolved by first using
URL.parseto test whether theapiUrlvalue is parseable as-is, and if not, then trying to construct it with the current page's base URL:Screenshots
No response