Skip to content

Commit

Permalink
Merge branch 'canary' into fix-placeholder-quality
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Jun 8, 2021
2 parents 56b7ec9 + 535a83d commit 0ee3045
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/next/client/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ function linkClicked(
e.preventDefault()

// avoid scroll for urls with anchor refs
if (scroll == null) {
scroll = as.indexOf('#') < 0
if (scroll == null && as.indexOf('#') >= 0) {
scroll = false
}

// replace state instead of push if prop is present
Expand Down
16 changes: 6 additions & 10 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,11 +816,6 @@ export default class Router implements BaseRouter {
this.isReady = true
}

// Default to scroll reset behavior unless explicitly specified to be
// `false`! This makes the behavior between using `Router#push` and a
// `<Link />` consistent.
options.scroll = !!(options.scroll ?? true)

let localeChange = options.locale !== this.locale

if (process.env.__NEXT_I18N_SUPPORT) {
Expand Down Expand Up @@ -1165,9 +1160,6 @@ export default class Router implements BaseRouter {
!(routeInfo.Component as any).getInitialProps
}

// shallow routing is only allowed for same page URL changes.
const isValidShallowRoute = options.shallow && this.route === route

if (
(options as any)._h &&
pathname === '/_error' &&
Expand All @@ -1179,14 +1171,18 @@ export default class Router implements BaseRouter {
props.pageProps.statusCode = 500
}

// shallow routing is only allowed for same page URL changes.
const isValidShallowRoute = options.shallow && this.route === route

const shouldScroll = options.scroll ?? !isValidShallowRoute
const resetScroll = shouldScroll ? { x: 0, y: 0 } : null
await this.set(
route,
pathname!,
query,
cleanedAs,
routeInfo,
forcedScroll ||
(isValidShallowRoute || !options.scroll ? null : { x: 0, y: 0 })
forcedScroll ?? resetScroll
).catch((e) => {
if (e.cancelled) error = error || e
else throw e
Expand Down
1 change: 0 additions & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"@next/polyfill-module": "10.2.4-canary.7",
"@next/react-dev-overlay": "10.2.4-canary.7",
"@next/react-refresh-utils": "10.2.4-canary.7",
"@opentelemetry/api": "0.14.0",
"assert": "2.0.0",
"ast-types": "0.13.2",
"browserify-zlib": "0.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export default withRouter(
return router.query.counter ? parseInt(router.query.counter) : 0
}

increase() {
increase(scroll) {
const counter = this.getCurrentCounter()
const href = `/nav/shallow-routing?counter=${counter + 1}`
Router.push(href, href, { shallow: true })
Router.push(href, href, { shallow: true, scroll })
}

increaseNonShallow() {
Expand Down Expand Up @@ -56,6 +56,9 @@ export default withRouter(
<button id="increase" onClick={() => this.increase()}>
Increase
</button>
<button id="increaseWithScroll" onClick={() => this.increase(true)}>
Increase with scroll
</button>
<button id="increase2" onClick={() => this.increaseNonShallow()}>
Increase Non-Shallow
</button>
Expand Down
16 changes: 16 additions & 0 deletions test/integration/client-navigation/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,22 @@ describe('Client Navigation', () => {
})
})

it('should scroll to top when the scroll option is set to true', async () => {
const browser = await webdriver(context.appPort, '/nav/shallow-routing')
await browser.eval(() =>
document.querySelector('#increaseWithScroll').scrollIntoView()
)
const scrollPosition = await browser.eval('window.pageYOffset')

expect(scrollPosition).toBeGreaterThan(3000)

await browser.elementByCss('#increaseWithScroll').click()
await check(async () => {
const newScrollPosition = await browser.eval('window.pageYOffset')
return newScrollPosition === 0 ? 'success' : 'fail'
}, 'success')
})

describe('with URL objects', () => {
it('should work with <Link/>', async () => {
const browser = await webdriver(context.appPort, '/nav')
Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2595,18 +2595,6 @@
"@octokit/openapi-types" "^4.0.2"
"@types/node" ">= 8"

"@opentelemetry/[email protected]":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-0.14.0.tgz#4e17d8d2f1da72b19374efa7b6526aa001267cae"
integrity sha512-L7RMuZr5LzMmZiQSQDy9O1jo0q+DaLy6XpYJfIGfYSfoJA5qzYwUP3sP1uMIQ549DvxAgM3ng85EaPTM/hUHwQ==
dependencies:
"@opentelemetry/context-base" "^0.14.0"

"@opentelemetry/context-base@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/context-base/-/context-base-0.14.0.tgz#c67fc20a4d891447ca1a855d7d70fa79a3533001"
integrity sha512-sDOAZcYwynHFTbLo6n8kIbLiVF3a3BLkrmehJUyEbT9F+Smbi47kLGS2gG2g0fjBLR/Lr1InPD7kXL7FaTqEkw==

"@polka/url@^1.0.0-next.9":
version "1.0.0-next.11"
resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.11.tgz#aeb16f50649a91af79dbe36574b66d0f9e4d9f71"
Expand Down

0 comments on commit 0ee3045

Please sign in to comment.