Skip to content

Commit

Permalink
Fixes page titles in the browser's drop-down for back / forward navig…
Browse files Browse the repository at this point in the history
…ation 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
  • Loading branch information
martrapp committed Jan 4, 2024
1 parent 00fcf82 commit 82bad5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-rabbits-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes page titles in the browser's drop-down for back / forward navigation when using view transitions
17 changes: 14 additions & 3 deletions packages/astro/src/transitions/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -223,6 +232,7 @@ const moveToLocation = (to: URL, from: URL, options: Options, historyState?: Sta
}
history.scrollRestoration = 'manual';
}
document.title = targetPageTitle;
};

function preloadStyleLinks(newDocument: Document) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 82bad5d

Please sign in to comment.