@@ -885,6 +885,28 @@ if (!history.state) {
885
885
history . replaceState ( state , document . title ) ;
886
886
}
887
887
888
+ function maybeUpdateHistory ( nextUrl : URL ) {
889
+ // Only add history entry when URL is new. Still apply
890
+ // the partials because sometimes users click a link to
891
+ // "refresh" the current page.
892
+ if ( nextUrl . href !== window . location . href ) {
893
+ const state : FreshHistoryState = {
894
+ index,
895
+ scrollX : window . scrollX ,
896
+ scrollY : window . scrollY ,
897
+ } ;
898
+
899
+ // Store current scroll position
900
+ history . replaceState ( { ...state } , "" , location . href ) ;
901
+
902
+ // Now store the new position
903
+ index ++ ;
904
+ state . scrollX = 0 ;
905
+ state . scrollY = 0 ;
906
+ history . pushState ( state , "" , nextUrl . href ) ;
907
+ }
908
+ }
909
+
888
910
document . addEventListener ( "click" , async ( e ) => {
889
911
let el = e . target ;
890
912
if ( el && el instanceof HTMLElement ) {
@@ -930,25 +952,7 @@ document.addEventListener("click", async (e) => {
930
952
931
953
const nextUrl = new URL ( el . href ) ;
932
954
try {
933
- // Only add history entry when URL is new. Still apply
934
- // the partials because sometimes users click a link to
935
- // "refresh" the current page.
936
- if ( el . href !== window . location . href ) {
937
- const state : FreshHistoryState = {
938
- index,
939
- scrollX : window . scrollX ,
940
- scrollY : window . scrollY ,
941
- } ;
942
-
943
- // Store current scroll position
944
- history . replaceState ( { ...state } , "" , location . href ) ;
945
-
946
- // Now store the new position
947
- index ++ ;
948
- state . scrollX = 0 ;
949
- state . scrollY = 0 ;
950
- history . pushState ( state , "" , nextUrl . href ) ;
951
- }
955
+ maybeUpdateHistory ( nextUrl ) ;
952
956
953
957
const partialUrl = new URL (
954
958
partial ? partial : nextUrl . href ,
@@ -1084,6 +1088,7 @@ document.addEventListener("submit", async (e) => {
1084
1088
init = { body : new FormData ( el ) , method : lowerMethod } ;
1085
1089
}
1086
1090
1091
+ maybeUpdateHistory ( url ) ;
1087
1092
await fetchPartials ( url , init ) ;
1088
1093
}
1089
1094
}
0 commit comments