From 80eef6dde17963cd896ff7aec80ce6bed2053527 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 4 Dec 2024 17:07:14 +0000 Subject: [PATCH] Enhance search block functionality by integrating canonical URL support and updating interactivity configuration. This change allows the search block to utilize the canonical URL when performing instant searches. --- packages/block-library/src/search/index.php | 3 +++ packages/block-library/src/search/view.js | 15 +++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index 91f193c78a1b98..2216a411065e7f 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -208,6 +208,9 @@ function render_block_core_search( $attributes, $content, $block ) { } if ( $enhanced_pagination && $instant_search_enabled && isset( $block->context['queryId'] ) ) { + + wp_interactivity_config( 'core/search', array( 'canonicalURL' => get_permalink() ) ); + $is_inherited = isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] && ! empty( $block->context['queryId'] ); $search = ''; diff --git a/packages/block-library/src/search/view.js b/packages/block-library/src/search/view.js index b3494c6f1a8364..bf732eec28bf9a 100644 --- a/packages/block-library/src/search/view.js +++ b/packages/block-library/src/search/view.js @@ -1,7 +1,12 @@ /** * WordPress dependencies */ -import { store, getContext, getElement } from '@wordpress/interactivity'; +import { + store, + getContext, + getElement, + getConfig, +} from '@wordpress/interactivity'; /** @type {( () => void ) | null} */ let supersedePreviousSearch = null; @@ -106,14 +111,16 @@ const { state, actions } = store( return; } - const url = new URL( window.location.href ); + let url = new URL( window.location.href ); if ( value ) { if ( ctx.isInherited ) { - url.searchParams.set( 'instant-search', value ); + // Get the canonical URL from the config + const { canonicalURL } = getConfig( 'core/search' ); // Make sure we reset the pagination. - url.searchParams.set( 'paged', '1' ); + url = new URL( canonicalURL ); + url.searchParams.set( 'instant-search', value ); } else { // Set the instant-search parameter using the query ID and search value const queryId = ctx.queryId;