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

Preserve existing URL parameters in history. #2777

Merged
merged 1 commit into from
May 19, 2022
Merged
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
20 changes: 17 additions & 3 deletions assets/js/instant-results/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,25 @@ const App = () => {
}

const state = JSON.stringify({ ...args, isOpen });
const params = getUrlParamsFromArgs(args, argsSchema, paramPrefix).toString();
const url = isOpen ? `?${params}` : window.location.origin + window.location.pathname;

if (history.state) {
history.pushState(state, document.title, url);
const params = getUrlParamsFromArgs(args, argsSchema, paramPrefix);
const url = new URL(window.location.href);
const keys = Array.from(url.searchParams.keys());

for (const key of keys) {
if (key.startsWith(paramPrefix)) {
url.searchParams.delete(key);
}
}

if (isOpen) {
params.forEach((value, key) => {
url.searchParams.set(key, value);
});
}

history.pushState(state, document.title, url.toString());
} else {
history.replaceState(state, document.title, window.location.href);
}
Expand Down