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 pushUnsafe / replaceUnsafe #46

Merged
merged 2 commits into from
Mar 12, 2024
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
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swan-io/chicane",
"version": "2.0.0-rc.0",
"version": "2.0.0-rc.1",
"license": "MIT",
"description": "A simple and safe router for React and TypeScript",
"author": "Mathieu Acthernoene <[email protected]>",
Expand Down Expand Up @@ -42,7 +42,6 @@
"not dead"
],
"prettier": {
"trailingComma": "all",
"plugins": [
"prettier-plugin-organize-imports"
]
Expand Down
26 changes: 11 additions & 15 deletions src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ export const createBrowserHistory = () => {
globalLocation.pathname + globalLocation.search,
);

const maybeUpdateLocation = (nextLocation: Location) => {
const maybeUpdateLocation = () => {
const nextLocation = decodeLocation(
globalLocation.pathname + globalLocation.search,
);

if (nextLocation.toString() === currentLocation.toString()) {
return; // As the `encodeSearch` function guarantees a stable sorting, we can rely on a simple URL comparison
}
Expand Down Expand Up @@ -132,9 +136,7 @@ export const createBrowserHistory = () => {
};

window.addEventListener("popstate", () => {
maybeUpdateLocation(
decodeLocation(globalLocation.pathname + globalLocation.search),
);
maybeUpdateLocation();
});

return {
Expand All @@ -154,29 +156,23 @@ export const createBrowserHistory = () => {
const blocker = last(blockers);

if (blocker == null || window.confirm(blocker.message)) {
const nextLocation = decodeLocation(url);
const nextUrl = nextLocation.toString();

try {
// iOS has a limit of 100 pushState calls / 30 secs
globalHistory.pushState(null, "", nextUrl);
globalHistory.pushState(null, "", url);
} catch {
globalLocation.assign(nextUrl);
globalLocation.assign(url);
}

maybeUpdateLocation(nextLocation);
maybeUpdateLocation();
}
},

replace: (url: string): void => {
const blocker = last(blockers);

if (blocker == null || window.confirm(blocker.message)) {
const nextLocation = decodeLocation(url);
const nextUrl = nextLocation.toString();

globalHistory.replaceState(null, "", nextUrl);
maybeUpdateLocation(nextLocation);
globalHistory.replaceState(null, "", url);
maybeUpdateLocation();
}
},

Expand Down