Skip to content

Commit 80eef6d

Browse files
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.
1 parent c24d01d commit 80eef6d

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

packages/block-library/src/search/index.php

+3
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ function render_block_core_search( $attributes, $content, $block ) {
208208
}
209209

210210
if ( $enhanced_pagination && $instant_search_enabled && isset( $block->context['queryId'] ) ) {
211+
212+
wp_interactivity_config( 'core/search', array( 'canonicalURL' => get_permalink() ) );
213+
211214
$is_inherited = isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] && ! empty( $block->context['queryId'] );
212215
$search = '';
213216

packages/block-library/src/search/view.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/**
22
* WordPress dependencies
33
*/
4-
import { store, getContext, getElement } from '@wordpress/interactivity';
4+
import {
5+
store,
6+
getContext,
7+
getElement,
8+
getConfig,
9+
} from '@wordpress/interactivity';
510

611
/** @type {( () => void ) | null} */
712
let supersedePreviousSearch = null;
@@ -106,14 +111,16 @@ const { state, actions } = store(
106111
return;
107112
}
108113

109-
const url = new URL( window.location.href );
114+
let url = new URL( window.location.href );
110115

111116
if ( value ) {
112117
if ( ctx.isInherited ) {
113-
url.searchParams.set( 'instant-search', value );
118+
// Get the canonical URL from the config
119+
const { canonicalURL } = getConfig( 'core/search' );
114120

115121
// Make sure we reset the pagination.
116-
url.searchParams.set( 'paged', '1' );
122+
url = new URL( canonicalURL );
123+
url.searchParams.set( 'instant-search', value );
117124
} else {
118125
// Set the instant-search parameter using the query ID and search value
119126
const queryId = ctx.queryId;

0 commit comments

Comments
 (0)