10up / headstartwp

Build a headless website fast with WordPress, the world’s most popular CMS, and Next.js, the most popular React framework. A free and open source solution by the experts at 10up.
https://headstartwp.10up.com
160 stars 16 forks source link

Unable to fetch head metadata on search strategy when search keys has more than one word #537

Closed nicholasio closed 8 months ago

nicholasio commented 1 year ago

Describe the bug

There seem to be a few issues with the Yoast get_head endpoint that is not properly recognizing the requested page as a search page.

Steps to Reproduce

  1. Go To Search
  2. Search for two words
  3. Missing/wrong metatags or crash.

Screenshots, screen recording, code snippet

No response

Environment information

No response

WordPress information

No response

Code of Conduct

lucymtc commented 1 year ago

@nicholasio I have also noticed an issue with get_head endpoint when appending the url param with '${getWPUrl()}/?s=${params.search} If the WP url returned by getWPUrl() has a trailing slash, this will add double slash to //?s= where get_head endpoint returns page not found. I needed to do const wpUrl = getWPUrl().replace(/\/$/, ''); first to remove the trailing slash

nicholasio commented 1 year ago

Thanks @lucymtc Here are a few things I found:

I'm not sure if we can rely on on Yoast to generate the meta tags for the search page.

nicholasio commented 9 months ago

Alternative solution: Have the SearchStrategy manually build the yoast_seo object and yoast_html fields manually with basica meta tags.

lucymtc commented 8 months ago

@nicholasio for the issue:

I found what the problem is and opened an Issue and PR in Yoast

I tried to override the validate callback for the URL, the way to do this would be to register a get_head endpoint from our side as if $override is set to false in register_rest_route WP merges with newer overriding if duplicate keys exist. The problem is that you need to pass in the exact arguments that the original register_rest_route (the one in Yoast) is called with, and the callback function depends on an instance of\Yoast\WP\SEO\Routes\Indexables_Head_Route which has dependencies in other Yoast classes and doesn't make it straight forward to instantiate the class. Unless Yoast exposes an instance of the class but I haven't found anything so far. I tested other hooks to try override the validation callback without success.

lucymtc commented 8 months ago

Update on fix for _"Searching for multiple words crashes the gethead query and thus makes the framework default to the metadata of the first post returned." has been merged into Yoast via this PR https://github.com/Yoast/wordpress-seo/pull/21032

lucymtc commented 8 months ago

Opened PR https://github.com/10up/headstartwp/pull/663 for: Yoast is not properly detecting a search page via the get_head function. Yoast does not yoast_head to the WordPress search endpoint.

nicholasio commented 8 months ago

Update on fix for _"Searching for multiple words crashes the gethead query and thus makes the framework default to the metadata of the first post returned." has been merged into Yoast via this PR Yoast/wordpress-seo#21032

Looks like the PR merged will be released in Yoast 22.0.