Skip to content

Select plugin no longer allows relative apiUrl values #761

Description

@delfuego

Summary

When using the select plugin, relative apiUrl values throw an error

Steps to Reproduce

  1. Create a select element with a relative apiUrl (e.g., /private/api/something).
  2. 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:

let url = this.apiUrl;

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions