-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Back button not working after url changed by swap or query added (…
…#10903) To reproduce: Go to swap Change tokens that will change the url Go to another page using menu Use back button to go back (not working) <!-- Before opening a pull request, please read the [contributing guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md) first --> <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on refactoring the `replaceBrowserHistory` functionality to use `replaceBrowserHistoryMultiple`, allowing for multiple URL parameter updates in a single call. This enhances code readability and performance by consolidating history state updates. ### Detailed summary - Introduced `replaceBrowserHistoryMultiple` in `replaceBrowserHistoryMultiple.ts`. - Updated `replaceBrowserHistory` to handle undefined or null values using `isUndefinedOrNull`. - Replaced instances of `replaceBrowserHistory` with `replaceBrowserHistoryMultiple` across multiple components. - Enhanced URL state management in various files, including `FlipButton.tsx`, `FormMain.tsx`, and `Twap.tsx`, to handle multiple parameters efficiently. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
- Loading branch information
Showing
10 changed files
with
84 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import isUndefinedOrNull from './isUndefinedOrNull' | ||
|
||
const replaceBrowserHistory = (key: string, value?: string | number | null) => { | ||
const url = new URL(window.location.href) | ||
if (!value) { | ||
if (isUndefinedOrNull(value)) { | ||
url.searchParams.delete(key) | ||
} else { | ||
url.searchParams.set(key, String(value)) | ||
} | ||
window.history.replaceState({}, '', url) | ||
const urlString = url.toString() | ||
window.history.replaceState({ ...window.history.state, as: urlString, url: urlString }, '', url) | ||
} | ||
|
||
export default replaceBrowserHistory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import isUndefinedOrNull from './isUndefinedOrNull' | ||
|
||
const replaceBrowserHistoryMultiple = (params: { [key: string]: string | number | null | undefined }) => { | ||
const url = new URL(window.location.href) | ||
|
||
Object.entries(params).forEach(([key, value]) => { | ||
if (isUndefinedOrNull(value)) { | ||
url.searchParams.delete(key) | ||
} else { | ||
url.searchParams.set(key, String(value)) | ||
} | ||
}) | ||
|
||
const urlString = url.toString() | ||
window.history.replaceState({ ...window.history.state, as: urlString, url: urlString }, '', url) | ||
} | ||
|
||
export default replaceBrowserHistoryMultiple |