Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instant Search: Account for site URL when appending query string #13846

Merged
merged 5 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/search/class.jetpack-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ public function load_assets() {
// This is probably a temporary filter for testing the prototype.
$options = array(
'enableLoadOnScroll' => false,
'homeUrl' => home_url(),
'locale' => str_replace( '_', '-', get_locale() ),
'postTypeFilters' => $widget_options['post_types'],
'postTypes' => $post_type_labels,
Expand Down
2 changes: 2 additions & 0 deletions modules/search/instant-search/components/search-app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ class SearchApp extends Component {
this.input.current.focus();
}

window.addEventListener( 'popstate', this.onChangeQueryString );
window.addEventListener( 'queryStringChange', this.onChangeQueryString );
}
componentWillUnmount() {
window.removeEventListener( 'popstate', this.onChangeQueryString );
window.removeEventListener( 'queryStringChange', this.onChangeQueryString );
}

Expand Down
14 changes: 8 additions & 6 deletions modules/search/instant-search/lib/query-string.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import 'url-polyfill';
import { decode, encode } from 'qss';
// NOTE: We only import the get package here for to reduced bundle size.
// Do not import the entire lodash library!
Expand All @@ -18,19 +19,20 @@ function getQuery() {
}

function pushQueryString( queryString ) {
// NOTE: This erases location.pathname
if ( history.pushState ) {
const newUrl = queryString
? `${ window.location.protocol }//${ window.location.host }?${ queryString }`
: `${ window.location.protocol }//${ window.location.host }${ window.location.pathname }`;
window.history.pushState( { path: newUrl }, '', newUrl );
const url = new window.URL( window.location.href );
if ( window[ SERVER_OBJECT_NAME ] && 'homeUrl' in window[ SERVER_OBJECT_NAME ] ) {
url.href = window[ SERVER_OBJECT_NAME ].homeUrl;
}
url.search = queryString;
window.history.pushState( null, null, url.toString() );
window.dispatchEvent( new Event( 'queryStringChange' ) );
}
}

export function restorePreviousHref( initialHref ) {
if ( history.pushState ) {
window.history.pushState( { href: initialHref }, '', initialHref );
window.history.pushState( null, null, initialHref );
window.dispatchEvent( new Event( 'queryStringChange' ) );
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"swiper": "4.5.1",
"uglify-save-license": "0.4.1",
"unfetch": "4.1.0",
"url-polyfill": "1.1.7",
"webpack": "4.41.0",
"webpack-cli": "3.3.9"
},
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13079,6 +13079,11 @@ [email protected]:
mime "^2.0.3"
schema-utils "^1.0.0"

[email protected]:
version "1.1.7"
resolved "https://registry.yarnpkg.com/url-polyfill/-/url-polyfill-1.1.7.tgz#402ee84360eb549bbeb585f4c7971e79a31de9e3"
integrity sha512-ZrAxYWCREjmMtL8gSbSiKKLZZticgihCvVBtrFbUVpyoETt8GQJeG2okMWA8XryDAaHMjJfhnc+rnhXRbI4DXA==

url-template@^2.0.8:
version "2.0.8"
resolved "https://registry.yarnpkg.com/url-template/-/url-template-2.0.8.tgz#fc565a3cccbff7730c775f5641f9555791439f21"
Expand Down