From 798c7c068cbe785308e15a64dcfbb7ec4193b057 Mon Sep 17 00:00:00 2001 From: Vahan Date: Tue, 26 Apr 2022 13:36:20 +0400 Subject: [PATCH] feat(history): plugin fixes and new option adding (#5665) * fixed issue with the history back navigation [ADD] Added option to keep the query string parameters when changing user when navigating [FIXED] History back via browser button not working * Added typing for History plugin options Added typing for History plugin options - enabled - keepQuery * keepQuery default value: false Changed the default value of the option keepQuery to false * Update history.js fixed param getting object name --- src/modules/history/history.js | 6 +++++- src/types/modules/history.d.ts | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/modules/history/history.js b/src/modules/history/history.js index b7c76f045..d2ec183e7 100644 --- a/src/modules/history/history.js +++ b/src/modules/history/history.js @@ -7,6 +7,7 @@ export default function History({ swiper, extendParams, on }) { root: '', replaceState: false, key: 'slides', + keepQuery: false, }, }); @@ -58,6 +59,9 @@ export default function History({ swiper, extendParams, on }) { } else if (!location.pathname.includes(key)) { value = `${key}/${value}`; } + if (swiper.params.history.keepQuery) { + value += location.search; + } const currentState = window.history.state; if (currentState && currentState.value === value) { return; @@ -86,7 +90,7 @@ export default function History({ swiper, extendParams, on }) { const setHistoryPopState = () => { paths = getPathValues(swiper.params.url); - scrollToSlide(swiper.params.speed, swiper.paths.value, false); + scrollToSlide(swiper.params.speed, paths.value, false); }; const init = () => { diff --git a/src/types/modules/history.d.ts b/src/types/modules/history.d.ts index 0003b08d7..a3cea81ed 100644 --- a/src/types/modules/history.d.ts +++ b/src/types/modules/history.d.ts @@ -3,6 +3,13 @@ export interface HistoryMethods {} export interface HistoryEvents {} export interface HistoryOptions { + /** + * Enables History Plugin. + * + * @default false + */ + enabled?: boolean; + /** * Swiper page root, useful to specify when you use Swiper history mode not on root website page. * For example can be `https://my-website.com/` or `https://my-website.com/subpage/` or `/subpage/` @@ -26,4 +33,11 @@ export interface HistoryOptions { * @default 'slides' */ key?: string; + + /** + * Keep query parameters when changing browser url. + * + * @default false + */ + keepQuery?: boolean; }