Skip to content

Commit

Permalink
fix: forward NavigateOptions in adaptForAppRouterInstance (#52498)
Browse files Browse the repository at this point in the history
### Fixing a bug

### What?

App-router now supports the
[`scroll`-option](https://github.com/vercel/next.js/pull/51869/files).
Thank you for that!
We're in the midst of migrating towards the app-router meaning that some
pages use the `adaptForAppRouterInstance`-adapter.

We noticed that the adaptForAppRouterInstance does not forward any
options given to either `push` or `replace`, while in theory `{ scroll?:
boolean }` perfectly overlaps and could be forwarded.

### Why?

When using `{ scroll: false }` and using `adaptForAppRouterInstance` the
options are not being forwarded.
Meaning that when calling either `push` or `replace`, the page is
scrolled to the top regardless.

### How?

By forwarding the options that perfectly overlap between
`NavigateOptions` (app-router) and `TransitionOptions` (next-router)

```
// Added NavigateOptions, and forward `{ scroll }`

push(href: string, { scroll }:NavigateOptions = {}): void {
      void router.push(href, undefined, { scroll }) // 
},
replace(href: string, { scroll }:NavigateOptions = {}): void {
      void router.replace(href, undefined, { scroll })
},
```

Please let me know if I need to change anything!

Co-authored-by: Jeffrey <jeffrey@jeffreyzutt.nl>
Co-authored-by: Jimmy Lai <laijimmy0@gmail.com>
  • Loading branch information
3 people committed Jul 17, 2023
1 parent dc6c22c commit 6b2aed1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
65 changes: 65 additions & 0 deletions packages/next/src/shared/lib/router/adapters.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { adaptForAppRouterInstance } from './adapters'
import { NextRouter } from './router'

describe('adaptForAppRouterInstance', () => {
beforeEach(() => jest.resetAllMocks())

const router = {
back: jest.fn(),
forward: jest.fn(),
reload: jest.fn(),
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
} as unknown as NextRouter

const adapter = adaptForAppRouterInstance(router)

it('should forward a call to `back()`', () => {
adapter.back()
expect(router.back).toHaveBeenCalled()
})

it('should forward a call to `forward()`', () => {
adapter.forward()
expect(router.forward).toHaveBeenCalled()
})

it('should forward a call to `reload()`', () => {
adapter.refresh()
expect(router.reload).toHaveBeenCalled()
})

it('should forward a call to `push()`', () => {
adapter.push('/foo')
expect(router.push).toHaveBeenCalledWith('/foo', undefined, {
scroll: undefined,
})
})

it('should forward a call to `push()` with options', () => {
adapter.push('/foo', { scroll: false })
expect(router.push).toHaveBeenCalledWith('/foo', undefined, {
scroll: false,
})
})

it('should forward a call to `replace()`', () => {
adapter.replace('/foo')
expect(router.replace).toHaveBeenCalledWith('/foo', undefined, {
scroll: undefined,
})
})

it('should forward a call to `replace()` with options', () => {
adapter.replace('/foo', { scroll: false })
expect(router.replace).toHaveBeenCalledWith('/foo', undefined, {
scroll: false,
})
})

it('should forward a call to `prefetch()`', () => {
adapter.prefetch('/foo')
expect(router.prefetch).toHaveBeenCalledWith('/foo')
})
})
10 changes: 5 additions & 5 deletions packages/next/src/shared/lib/router/adapters.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ParsedUrlQuery } from 'node:querystring'
import React, { useMemo, useRef } from 'react'
import type { AppRouterInstance } from '../app-router-context'
import type { AppRouterInstance, NavigateOptions } from '../app-router-context'
import { PathnameContext } from '../hooks-client-context'
import type { NextRouter } from './router'
import { isDynamicRoute } from './utils'
Expand All @@ -24,11 +24,11 @@ export function adaptForAppRouterInstance(
refresh(): void {
router.reload()
},
push(href: string): void {
void router.push(href)
push(href: string, { scroll }: NavigateOptions = {}): void {
void router.push(href, undefined, { scroll })
},
replace(href: string): void {
void router.replace(href)
replace(href: string, { scroll }: NavigateOptions = {}): void {
void router.replace(href, undefined, { scroll })
},
prefetch(href: string): void {
void router.prefetch(href)
Expand Down

0 comments on commit 6b2aed1

Please sign in to comment.