0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00

Fixes page titles in the browser's drop-down for back / forward navigation when using view transitions (#9586)

* Fixes titles in the browser's dropdown for back / forwards traversals through the browser history

* Improve names of constants

* Reword the changset description
This commit is contained in:
Martin Trapp 2024-01-04 12:15:35 +01:00 committed by GitHub
parent 00fcf82eb7
commit 82bad5d620
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes page titles in the browser's drop-down for back / forward navigation when using view transitions

View file

@ -170,9 +170,18 @@ function runScripts() {
// Add a new entry to the browser history. This also sets the new page in the browser addressbar.
// Sets the scroll position according to the hash fragment of the new location.
const moveToLocation = (to: URL, from: URL, options: Options, historyState?: State) => {
const moveToLocation = (
to: URL,
from: URL,
options: Options,
pageTitleForBrowserHistory: string,
historyState?: State
) => {
const intraPage = samePage(from, to);
const targetPageTitle = document.title;
document.title = pageTitleForBrowserHistory;
let scrolledToTop = false;
if (to.href !== location.href && !historyState) {
if (options.history === 'replace') {
@ -223,6 +232,7 @@ const moveToLocation = (to: URL, from: URL, options: Options, historyState?: Sta
}
history.scrollRestoration = 'manual';
}
document.title = targetPageTitle;
};
function preloadStyleLinks(newDocument: Document) {
@ -409,8 +419,9 @@ async function updateDOM(
throw new DOMException('Transition was skipped');
}
const pageTitleForBrowserHistory = document.title; // document.title will be overridden by swap()
const swapEvent = await doSwap(preparationEvent, viewTransition!, defaultSwap);
moveToLocation(swapEvent.to, swapEvent.from, options, historyState);
moveToLocation(swapEvent.to, swapEvent.from, options, pageTitleForBrowserHistory, historyState);
triggerEvent(TRANSITION_AFTER_SWAP);
if (fallback === 'animate' && !skipTransition) {
@ -441,7 +452,7 @@ async function transition(
updateScrollPosition({ scrollX, scrollY });
}
if (samePage(from, to) && !!to.hash) {
moveToLocation(to, from, options, historyState);
moveToLocation(to, from, options, document.title, historyState);
return;
}