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;