Skip to content

Commit 1ac3847

Browse files
committed
fix: Path comparison should ignore last "/"
1 parent cda8664 commit 1ac3847

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/core.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ export function getPathInParent() {
6767
export function replaceStateOnWindow(win: Window, path: string) {
6868

6969
try {
70-
win.history.replaceState({}, '', path);
71-
win.dispatchEvent(new CustomEvent(EVENT_HISTORY_STATE_CHANGED));
70+
const currentPath = getFullPath(win);
71+
if (!isSamePath(currentPath, path)) {
72+
console.log('Replacing state on window: %s > %s', currentPath, path);
73+
// Only replace if the path has changed
74+
win.history.replaceState({}, '', path);
75+
win.dispatchEvent(new CustomEvent(EVENT_HISTORY_STATE_CHANGED));
76+
}
7277
} catch (e) {
7378
//ignore
7479
return false;
@@ -122,6 +127,17 @@ function syncLocationToChildFrames() {
122127
* Syncs the location of the current frame with the parent frame.
123128
*/
124129

130+
function normalizePath(path: string) {
131+
if (path.endsWith('/')) {
132+
return path.substring(0, path.length - 1);
133+
}
134+
return path;
135+
}
136+
137+
function isSamePath(path1: string, path2: string) {
138+
return normalizePath(path1) === normalizePath(path2);
139+
}
140+
125141
function syncLocationToParent() {
126142
if (window === window.parent) {
127143
// We are the top level window - no parent to sync to
@@ -135,7 +151,7 @@ function syncLocationToParent() {
135151
const localPath = removePathPrefix(fullPath, basePath);
136152
const topLocalPath = joinPaths(topPath, localPath);
137153
const parentFullPath = getFullPath(window.parent);
138-
if (parentFullPath !== topLocalPath) {
154+
if (!isSamePath(parentFullPath, topLocalPath)) {
139155
replaceStateOnWindow(window.parent, topLocalPath);
140156
}
141157
}
@@ -193,6 +209,10 @@ export function getFullPath(win?: Window) {
193209
* Gets everything after host and port.
194210
*/
195211
export function toFullPath(urlString: string) {
212+
if (urlString.startsWith('about:')) {
213+
return '';
214+
}
215+
196216
if (urlString.startsWith('/')) {
197217
return urlString;
198218
}

0 commit comments

Comments
 (0)