history state (due to pushState) issues

User avatar
axew3
w3all User
w3all User
Posts: 2677
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: history state (due to pushState) issues

Post by axew3 »

ok, i've got the problem.
in short, what happen is that history.pushState, push two times same page (you can see into history back browser button)

if you switch to

Code: Select all

history.replaceState
instead of using history.pushState
it lead to the correct result. But of course, the address bar is not updated then.
same was for the solution on landing, but there, it has been possible to wrap into load jQuery( window ).load(function() {
to resolve.
i will check how could never be possible to fix this-
the good question is: why it push two times the same?
User avatar
axew3
w3all User
w3all User
Posts: 2677
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: history state (due to pushState) issues

Post by axew3 »

so for what it worth my consideration on this, i just take a look into more deeply.

the resume is this to me (that may you expressed before and may i did not understood) of all the problems.

When you land, the first page is correctly pushed ONCE into history with the fix i applied.

but as soon you click into any forum link, and the history.pushState fire, it cause that the history push happen TWO TIMES, you can see it into the Back Button of the browser: there are two identical pushes, two on history: so, if you do not care to push on the address bar the url when you click into back button, the solution is this:

on the top javascript, so inside the function that output the javascript on page-forum, just add this:

Code: Select all

window.onpopstate = function(event) {
  window.history.go(-1);
};
with this, the back button action will cause the correct return to the right page, even if the address bar will not update to where you return back, then if you reload the page, you'll may be redirected to the url that is on the address bar.
This appear to me at moment the more clean solution, but it is not perfect.

But the fundamental question is and still remain by the way:
why this happen? how it has been possible to be resolved wrapping the code like this, the solution of when you land to the first page on forum:

Code: Select all

 jQuery( window ).load(function() { // wrapped here, or won't push correctly, duplicate happen
  var w3all_landed_url = '".$w3all_url_to_cms."';
  var w3all_landed_url_clean = w3allNormalize_phpBBUrl_onParent(w3all_landed_url);
  // PUSH phpBB URLs when wp page load first time //
  var w3all_passedurl = window.btoa(unescape(encodeURIComponent(w3all_landed_url_clean)));
  var w3all_passedurl_push = '".$w3allhomeurl."/".$wp_w3all_forum_folder_wp."/?".$w3all_iframe_custom_w3fancyurl."=' + w3all_passedurl;
  history.pushState({w3all_passedurl: w3all_passedurl_push}, \"\", w3all_passedurl_push);
 })
the unique thing i note, is that the code that works fine for the first loaded page,
is that this code is NOT inside the callback of the iframe resizer code.
is the iframe resizer code interacting some way with this?
inside it, happen two times
... we'll see
oferlaor
User w
User w
Posts: 10
Joined: Wed Jan 27, 2021 2:24 pm

Re: history state (due to pushState) issues

Post by oferlaor »

did you try the popstate solution?

I thought of it myself but it didn't fire the first back, only on the second...

I think it pushes it twice because you do not preventDefault() on the actual click, so you have one event by the original click and the second is the fake one created by the pushState.

is the go back solution implemented on this forum already?

BTW, found another neat hack - I moved the iframeresizer to the overall header and it really accelerated how fast the forum loads.
User avatar
axew3
w3all User
w3all User
Posts: 2677
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: history state (due to pushState) issues

Post by axew3 »

yes applied

ps
found another neat hack - I moved the iframeresizer to the overall header and it really accelerated how fast the forum loads.
i will come to see how you did if you do not provide here a ready example!
Provide one when ready if you can, so may it will be so published as default for all (with credits for you, of course)
oferlaor
User w
User w
Posts: 10
Joined: Wed Jan 27, 2021 2:24 pm

Re: history state (due to pushState) issues

Post by oferlaor »

OK, tried it.

But it doesn't quite work correctly (almost). The popstate event gets called the second back (so you lose the scroll position).

Is there a way to catch the first event and do the go back the first time?
User avatar
axew3
w3all User
w3all User
Posts: 2677
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: history state (due to pushState) issues

Post by axew3 »

will try to go more deep asap ... i go to check your solution about preloader, because of that still stop here, often not correctly removed, i have to reload the page.
Or i will rewrite if still in trouble with, with a different solution.

For what i see into firefox and chrome it return correctly to the scrolled position at moment.

While what i really do not understand is why the preloader do not disappear often ... it is very strange to me, exactly happen when i may click on back button to return to forum on the very first history stack
Post Reply