未验证 提交 c8b51b5c 编写于 作者: J Joe Haddad 提交者: GitHub

Revert "Add scroll restoration handling after render is done" (#14075)

上级 3d6d033a
......@@ -94,9 +94,6 @@ type ComponentLoadCancel = (() => void) | null
type HistoryMethod = 'replaceState' | 'pushState'
const manualScrollRestoration =
typeof window !== 'undefined' && 'scrollRestoration' in window.history
function fetchNextData(
pathname: string,
query: ParsedUrlQuery | null,
......@@ -259,12 +256,6 @@ export default class Router implements BaseRouter {
}
window.addEventListener('popstate', this.onPopState)
// enable custom scroll restoration handling when available
// otherwise fallback to browser's default handling
if (manualScrollRestoration) {
window.history.scrollRestoration = 'manual'
}
}
}
......@@ -502,21 +493,6 @@ export default class Router implements BaseRouter {
}
Router.events.emit('beforeHistoryChange', as)
if (manualScrollRestoration && history.state) {
const {
url: curUrl,
as: curAs,
options: curOptions,
} = history.state
this.changeState('replaceState', curUrl, curAs, {
...curOptions,
_N_X: window.scrollX,
_N_Y: window.scrollY,
})
}
this.changeState(method, url, as, options)
if (process.env.NODE_ENV !== 'production') {
......@@ -534,10 +510,6 @@ export default class Router implements BaseRouter {
}
Router.events.emit('routeChangeComplete', as)
if (manualScrollRestoration && '_N_X' in options) {
window.scrollTo(options._N_X, options._N_Y)
}
return resolve(true)
}
)
......
export default () => <p id="another">hi from another</p>
import Link from 'next/link'
const Page = ({ loaded }) => {
const link = (
<Link href="/another">
<a
id="to-another"
style={{
marginLeft: 5000,
width: 95000,
display: 'block',
}}
>
to another
</a>
</Link>
)
if (typeof window !== 'undefined') {
window.didHydrate = true
}
if (loaded) {
return (
<>
<div
style={{
width: 10000,
height: 10000,
background: 'orange',
}}
/>
{link}
<div id="end-el">the end</div>
</>
)
}
return link
}
export default Page
export const getServerSideProps = () => {
return {
props: {
loaded: true,
},
}
}
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import webdriver from 'next-webdriver'
import {
killApp,
findPort,
launchApp,
nextStart,
nextBuild,
check,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
const runTests = () => {
it('should restore the scroll position on navigating back', async () => {
const browser = await webdriver(appPort, '/')
await browser.eval(() =>
document.querySelector('#to-another').scrollIntoView()
)
const scrollRestoration = await browser.eval(
() => window.history.scrollRestoration
)
expect(scrollRestoration).toBe('manual')
const scrollX = Math.floor(await browser.eval(() => window.scrollX))
const scrollY = Math.floor(await browser.eval(() => window.scrollY))
expect(scrollX).not.toBe(0)
expect(scrollY).not.toBe(0)
await browser.eval(() => window.next.router.push('/another'))
await check(
() => browser.eval(() => document.documentElement.innerHTML),
/hi from another/
)
await browser.eval(() => (window.didHydrate = false))
await browser.eval(() => window.history.back())
await check(() => browser.eval(() => window.didHydrate), {
test(content) {
return content
},
})
const newScrollX = Math.floor(await browser.eval(() => window.scrollX))
const newScrollY = Math.floor(await browser.eval(() => window.scrollY))
expect(scrollX).toBe(newScrollX)
expect(scrollY).toBe(newScrollY)
})
}
describe('Scroll Restoration Support', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('server mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`
module.exports = {
target: 'experimental-serverless-trace'
}
`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.remove(nextConfig)
await killApp(app)
})
runTests()
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册