Skip to content

Search does nothing after a filter has been used #76

Open

Description

@Retrofitterdk

Entering text in a search field does nothing after a filter has been used (Invalid URL in updateURL(), regression from #74)

Description

On a page with a Search block and a Taxonomy Filter in the same Query Loop, searching works until a filter has been used. After a filter navigates, the next search does nothing, and the console shows:

TypeError: Failed to construct 'URL': Invalid URL
    at updateURL (build/taxonomy/view.js)

Search first and filter second works fine.

Steps to reproduce

  1. Add a Query Loop (own query, inherit: false) containing a Search block and a Taxonomy Filter (select).
  2. Load the page and choose a term in the filter. The loop updates as expected.
  3. Type in the search field or submit it.

Expected: the results are filtered by both the term and the search.
Actual: nothing happens, and the console shows the error above.

Cause

The filter options' values are relative URLs (built from remove_query_arg() on the request URI), e.g. /members/?query-0-post_tag=foo. actions.navigate passes e.target.value to the router as-is, so core/router's state.url holds that relative URL afterwards.

Since #74, updateURL() builds the search URL from that state with no base:

const url = new URL( routerState?.url || window.location.href );

new URL() throws on a relative URL without a base. On a fresh page load state.url is not set yet, so it falls back to the absolute window.location.href. That is why the first search works and the #74 tests pass. Before #74 the code used form.action, which the DOM always gives as an absolute URL.

Suggested fix

Resolve against the current location:

const url = new URL( routerState?.url || window.location.href, window.location.href );

An e2e test that changes a filter and then searches would cover it.

Environment

  • Query Filter 0.6.1 (the same code is on main)
  • WordPress 7.1.4
  • Reproduced in Chrome. It happens with the Taxonomy Filter and with any block that navigates via actions.navigate with a relative URL.

Workaround

Making the filters' option values absolute, with a render_block filter on query-filter/taxonomy and query-filter/post-type, keeps state.url absolute and avoids the error.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions