Skip to content

Commit d83ec89

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 79b79ff commit d83ec89

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ function render_block_core_search( $attributes, $content, $block ) {
210210
}
211211

212212
if ( $enhanced_pagination && $instant_search_enabled && isset( $block->context['queryId'] ) ) {
213+
214+
wp_interactivity_config( 'core/search', array( 'canonicalURL' => get_permalink() ) );
215+
213216
$is_inherited = isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] && ! empty( $block->context['queryId'] );
214217
$search = '';
215218

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

+28-10
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;
@@ -110,18 +115,31 @@ const { state, actions } = store(
110115
return;
111116
}
112117

113-
const url = new URL( window.location.href );
118+
let url = new URL( window.location.href );
114119

115120
if ( value ) {
116-
// Set the instant-search parameter using the query ID and search value
117-
const queryId = ctx.queryId;
118-
url.searchParams.set(
119-
`instant-search-${ queryId }`,
120-
value
121-
);
121+
if ( ctx.isInherited ) {
122+
// Get the canonical URL from the config
123+
const { canonicalURL } = getConfig( 'core/search' );
124+
125+
// Make sure we reset the pagination.
126+
url = new URL( canonicalURL );
127+
url.searchParams.set( 'instant-search', value );
128+
} else {
129+
// Set the instant-search parameter using the query ID and search value
130+
const queryId = ctx.queryId;
131+
url.searchParams.set(
132+
`instant-search-${ queryId }`,
133+
value
134+
);
122135

123-
// Make sure we reset the pagination.
124-
url.searchParams.set( `query-${ queryId }-page`, '1' );
136+
// Make sure we reset the pagination.
137+
url.searchParams.set( `query-${ queryId }-page`, '1' );
138+
}
139+
} else if ( ctx.isInherited ) {
140+
// Reset global search for inherited queries
141+
url.searchParams.delete( 'instant-search' );
142+
url.searchParams.delete( 'paged' );
125143
} else {
126144
// Reset specific search for non-inherited queries
127145
url.searchParams.delete(

0 commit comments

Comments
 (0)