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

Fix to make browser back button navigate pagination properly when faceted search is enabled #1606

Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Draft
- Add jquery-migrate to Modal test [#1599](https://github.com/bigcommerce/cornerstone/pull/1599)
- Upgrade core-js to version 3 [#1598](https://github.com/bigcommerce/cornerstone/pull/1598)
- Fix to make browser back button navigate pagination properly when faceted search is enabled [#1606](https://github.com/bigcommerce/cornerstone/pull/1606)

## 4.3.0 (2019-11-12)
- Fixes body text color not taking effect for cart item headings on mobile / tablet [#1586](https://github.com/bigcommerce/cornerstone/pull/1586)
Expand Down
15 changes: 15 additions & 0 deletions assets/js/theme/common/faceted-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class FacetedSearch {

// DOM events
$(window).on('statechange', this.onStateChange);
$(window).on('popstate', this.onPopState);
$(document).on('click', this.options.showMoreToggleSelector, this.onToggleClick);
$(document).on('toggle.collapsible', this.options.accordionToggleSelector, this.onAccordionToggle);
$(document).on('keyup', this.options.facetedSearchFilterItems, this.filterFacetItems);
Expand All @@ -312,6 +313,7 @@ class FacetedSearch {
unbindEvents() {
// DOM events
$(window).off('statechange', this.onStateChange);
$(window).off('popstate', this.onPopState);
$(document).off('click', this.options.showMoreToggleSelector, this.onToggleClick);
$(document).off('toggle.collapsible', this.options.accordionToggleSelector, this.onAccordionToggle);
$(document).off('keyup', this.options.facetedSearchFilterItems, this.filterFacetItems);
Expand Down Expand Up @@ -408,6 +410,19 @@ class FacetedSearch {
this.collapsedFacets = _.without(this.collapsedFacets, id);
}
}

onPopState() {
const currentUrl = window.location.href;
const searchParams = new URLSearchParams(currentUrl);
// If searchParams does not contain a page value then modify url query string to have page=1
if (!searchParams.has('page')) {
const linkUrl = $('.pagination-link').attr('href');
const re = /page=[0-9]+/i;
const updatedLinkUrl = linkUrl.replace(re, 'page=1');
window.history.replaceState({}, document.title, updatedLinkUrl);
}
$(window).trigger('statechange');
}
}

export default FacetedSearch;