Skip to content

Commit 91369f4

Browse files
committed
fix: after redirect, should replace history instead of pushing it
1 parent 944d4d3 commit 91369f4

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

packages/fresh/src/runtime/client/partials.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ if (!history.state) {
8383
history.replaceState(state, document.title);
8484
}
8585

86-
function maybeUpdateHistory(nextUrl: URL) {
86+
function maybePushHistory(nextUrl: URL) {
8787
// Only add history entry when URL is new. Still apply
8888
// the partials because sometimes users click a link to
8989
// "refresh" the current page.
@@ -106,6 +106,12 @@ function maybeUpdateHistory(nextUrl: URL) {
106106
}
107107
}
108108

109+
function maybeReplaceHistory(nextUrl: URL) {
110+
if (nextUrl.href !== globalThis.location.href) {
111+
history.replaceState(history.state, "", nextUrl.href);
112+
}
113+
}
114+
109115
document.addEventListener("click", async (e) => {
110116
let el = e.target;
111117
if (el && (el instanceof HTMLElement || el instanceof SVGElement)) {
@@ -154,7 +160,7 @@ document.addEventListener("click", async (e) => {
154160

155161
const nextUrl = new URL(el.href);
156162
try {
157-
maybeUpdateHistory(nextUrl);
163+
maybePushHistory(nextUrl);
158164

159165
const partialUrl = new URL(
160166
partial ? partial : nextUrl.href,
@@ -376,7 +382,7 @@ async function fetchPartials(
376382
}
377383

378384
if (shouldNavigate) {
379-
maybeUpdateHistory(actualUrl);
385+
maybeReplaceHistory(actualUrl);
380386
}
381387
}
382388

0 commit comments

Comments
 (0)