diff --git a/.changeset/wild-singers-turn.md b/.changeset/wild-singers-turn.md new file mode 100644 index 000000000000..0b1de3bc22f3 --- /dev/null +++ b/.changeset/wild-singers-turn.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Increases compatibility with standard browser behavior by changing where view transitions occur on browser back navigation. diff --git a/packages/astro/e2e/fixtures/view-transitions/src/pages/long-page.astro b/packages/astro/e2e/fixtures/view-transitions/src/pages/long-page.astro index aa7c8b5e59c7..132c29c09229 100644 --- a/packages/astro/e2e/fixtures/view-transitions/src/pages/long-page.astro +++ b/packages/astro/e2e/fixtures/view-transitions/src/pages/long-page.astro @@ -4,6 +4,7 @@ import Layout from '../components/Layout.astro';
go to 1
+
go to self
go further down
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Dictum varius duis at consectetur lorem donec massa sapien faucibus. Amet mauris commodo quis imperdiet massa. Sed pulvinar proin gravida hendrerit lectus a. Magna ac placerat vestibulum lectus. Blandit cursus risus at ultrices mi tempus. Luctus venenatis lectus magna fringilla urna porttitor. Auctor eu augue ut lectus arcu bibendum at varius vel. Tristique senectus et netus et. In fermentum et sollicitudin ac orci phasellus egestas tellus rutrum. Eget lorem dolor sed viverra ipsum nunc aliquet. Amet consectetur adipiscing elit ut aliquam purus. Accumsan lacus vel facilisis volutpat est velit egestas. Felis imperdiet proin fermentum leo vel. Ut tellus elementum sagittis vitae et leo duis ut diam. Nisl pretium fusce id velit. Lorem donec massa sapien faucibus et. Nibh sed pulvinar proin gravida hendrerit lectus a. In est ante in nibh mauris cursus mattis molestie. diff --git a/packages/astro/e2e/view-transitions.test.js b/packages/astro/e2e/view-transitions.test.js index af1f89f3e457..a9626c09e918 100644 --- a/packages/astro/e2e/view-transitions.test.js +++ b/packages/astro/e2e/view-transitions.test.js @@ -413,10 +413,7 @@ test.describe('View Transitions', () => { await expect(locator).toBeInViewport(); // Scroll back to top - // back returns immediately, but we need to wait for navigate() to complete - const waitForReady = page.waitForEvent('console'); await page.goBack(); - await waitForReady; locator = page.locator('#longpage'); await expect(locator).toBeInViewport(); @@ -426,6 +423,85 @@ test.describe('View Transitions', () => { await expect(locator).toBeInViewport(); }); + test('View Transitions Rule', async ({ page, astro }) => { + let consoleCount = 0; + page.on('console', (msg) => { + // This count is used for transition events + if (msg.text() === 'ready') consoleCount++; + }); + // Don't test back and forward '' to '', because They are not stored in the history. + // click '' to '' (transition) + await page.goto(astro.resolveUrl('/long-page')); + let locator = page.locator('#longpage'); + await expect(locator).toBeInViewport(); + let consolePromise = page.waitForEvent('console'); + await page.click('#click-self'); + await consolePromise; + locator = page.locator('#longpage'); + await expect(locator).toBeInViewport(); + expect(consoleCount).toEqual(1); + + // click '' to 'hash' (no transition) + await page.click('#click-scroll-down'); + locator = page.locator('#click-one-again'); + await expect(locator).toBeInViewport(); + expect(consoleCount).toEqual(1); + + // back 'hash' to '' (no transition) + await page.goBack(); + locator = page.locator('#longpage'); + await expect(locator).toBeInViewport(); + expect(consoleCount).toEqual(1); + + // forward '' to 'hash' (no transition) + await page.goForward(); + locator = page.locator('#click-one-again'); + await expect(locator).toBeInViewport(); + expect(consoleCount).toEqual(1); + + // click 'hash' to 'hash' (no transition) + await page.click('#click-scroll-up'); + locator = page.locator('#longpage'); + await expect(locator).toBeInViewport(); + expect(consoleCount).toEqual(1); + + // back 'hash' to 'hash' (no transition) + await page.goBack(); + locator = page.locator('#click-one-again'); + await expect(locator).toBeInViewport(); + expect(consoleCount).toEqual(1); + + // forward 'hash' to 'hash' (no transition) + await page.goForward(); + locator = page.locator('#longpage'); + await expect(locator).toBeInViewport(); + expect(consoleCount).toEqual(1); + + // click 'hash' to '' (transition) + consolePromise = page.waitForEvent('console'); + await page.click('#click-self'); + await consolePromise; + locator = page.locator('#longpage'); + await expect(locator).toBeInViewport(); + expect(consoleCount).toEqual(2); + + // back '' to 'hash' (transition) + consolePromise = page.waitForEvent('console'); + await page.goBack(); + await consolePromise; + locator = page.locator('#longpage'); + await expect(locator).toBeInViewport(); + expect(consoleCount).toEqual(3); + + // forward 'hash' to '' (transition) + consolePromise = page.waitForEvent('console'); + await page.goForward(); + await consolePromise; + locator = page.locator('#longpage'); + await expect(locator).toBeInViewport(); + expect(consoleCount).toEqual(4); + }); + test(' component forwards transitions to the ', async ({ page, astro }) => { // Go to page 1 await page.goto(astro.resolveUrl('/image-one')); diff --git a/packages/astro/src/transitions/router.ts b/packages/astro/src/transitions/router.ts index 4e4f562a3c12..6ca3f666a871 100644 --- a/packages/astro/src/transitions/router.ts +++ b/packages/astro/src/transitions/router.ts @@ -453,9 +453,11 @@ async function transition( if (navigationType !== 'traverse') { updateScrollPosition({ scrollX, scrollY }); } - if (samePage(from, to) && !!to.hash) { - moveToLocation(to, from, options, document.title, historyState); - return; + if (samePage(from, to)) { + if ((direction !== 'back' && to.hash) || (direction === 'back' && from.hash)) { + moveToLocation(to, from, options, document.title, historyState); + return; + } } const prepEvent = await doPreparation(