-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(scroll): use location.key for scroll behaviours (#12403)
## Description This is a simple fix to use `location.key` for saving scroll state, if it's available. Based on the suggestions found in this discussion: taion/scroll-behavior#135 (comment) ## Related Issues Fixes #12390.
- Loading branch information
1 parent
2133288
commit 853ceb9
Showing
6 changed files
with
129 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
e2e-tests/production-runtime/cypress/integration/scroll-behavior.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
describe(`Scroll behaviour`, () => { | ||
it(`should restore scroll position only when going back in history`, () => { | ||
cy.visit(`/`).waitForAPI(`onRouteUpdate`) | ||
|
||
cy.getTestElement(`long-page`) | ||
.click() | ||
.waitForAPI(`onRouteUpdate`) | ||
|
||
cy.scrollTo(`bottom`) | ||
|
||
// allow ScrollContext to update scroll position store | ||
// it uses requestAnimationFrame so wait a bit to allow | ||
// it to store scroll position | ||
cy.wait(500) | ||
|
||
cy.getTestElement(`below-the-fold`) | ||
.click() | ||
.waitForAPI(`onRouteUpdate`) | ||
|
||
// after going back we expect page will | ||
// be restore previous scroll position | ||
cy.go(`back`).waitForAPI(`onRouteUpdate`) | ||
|
||
cy.window().then(win => { | ||
expect(win.scrollY).not.to.eq(0, 0) | ||
}) | ||
|
||
cy.go(`forward`).waitForAPI(`onRouteUpdate`) | ||
|
||
// after clicking link we expect page will be scrolled to top | ||
cy.getTestElement(`long-page`) | ||
.click() | ||
.waitForAPI(`onRouteUpdate`) | ||
|
||
cy.window().then(win => { | ||
expect(win.scrollY).to.eq(0, 0) | ||
}) | ||
|
||
// reset to index page | ||
cy.getTestElement(`index-link`) | ||
.click() | ||
.waitForAPI(`onRouteUpdate`) | ||
}) | ||
|
||
it(`should keep track of location.key`, () => { | ||
cy.visit(`/`).waitForAPI(`onRouteUpdate`) | ||
|
||
cy.getTestElement(`long-page`) | ||
.click() | ||
.waitForAPI(`onRouteUpdate`) | ||
|
||
cy.getTestElement(`below-the-fold`) | ||
.scrollIntoView({ | ||
// this is weird hack - seems like Cypress in run mode doesn't update scroll correctly | ||
duration: 100, | ||
}) | ||
.wait(500) // allow ScrollContext to update scroll position store | ||
.storeScrollPosition(`middle-of-the-page`) | ||
.click() | ||
.waitForAPI(`onRouteUpdate`) | ||
|
||
cy.getTestElement(`long-page`) | ||
.click() | ||
.waitForAPI(`onRouteUpdate`) | ||
|
||
cy.getTestElement(`even-more-below-the-fold`) | ||
.scrollIntoView({ | ||
// this is weird hack - seems like Cypress in run mode doesn't update scroll correctly | ||
duration: 100, | ||
}) | ||
.wait(500) // allow ScrollContext to update scroll position store | ||
.storeScrollPosition(`bottom-of-the-page`) | ||
.click() | ||
.waitForAPI(`onRouteUpdate`) | ||
|
||
cy.go(`back`).waitForAPI(`onRouteUpdate`) | ||
|
||
cy.location(`pathname`) | ||
.should(`equal`, `/long-page/`) | ||
.wait(500) | ||
// we went back in hitsory 1 time, so we should end up at the bottom of the page | ||
.shouldMatchScrollPosition(`bottom-of-the-page`) | ||
.shouldNotMatchScrollPosition(`middle-of-the-page`) | ||
|
||
cy.go(`back`).waitForAPI(`onRouteUpdate`) | ||
cy.go(`back`).waitForAPI(`onRouteUpdate`) | ||
|
||
cy.location(`pathname`) | ||
.should(`equal`, `/long-page/`) | ||
.wait(500) | ||
// we went back in hitsory 2 more times, so we should end up in the middle of the page | ||
// instead of at the bottom | ||
.shouldMatchScrollPosition(`middle-of-the-page`) | ||
.shouldNotMatchScrollPosition(`bottom-of-the-page`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters