naja-js / naja

Modern AJAX library for Nette Framework
https://naja.js.org
MIT License
109 stars 16 forks source link

Incorrect history entry in browser history when title is a snippet #406

Closed zipper closed 3 weeks ago

zipper commented 1 month ago

When title element is used as a snippet, the history in the browser (long click on back button) doesn't work properly in Chrome and Firefox (and probably in Safari as well).

  1. Create a page with a title "A" with ajax link to different page.
  2. Navigate using naja to a page with a title "B".
  3. The title is correctly set to "B", but in the history, the previous state is also titled "B" instead of "A".

I believe this is caused by changing the title (as any other snippet) before the pushState is called. When pushState is called, the browser uses the current title value in the history.

This was also an issue in history.nette.ajax.js and nette.ajax.js libraries (see https://github.com/vojtech-dobes/nette.ajax.js/issues/151). I have forked this library and solved the issue by changing the title only after the pushState call. Below are links to respective commmits into those repositories for possible inspiration:

There are also some related questions on SO:

jiripudil commented 1 month ago

Huh, TIL WebKit/Safari still uses the title argument in pushState and will continue to do so until the next version (https://bugs.webkit.org/show_bug.cgi?id=223190), so yep, it's broken in Safari as well, although differently 😅