Skip to content

Commit d92ab55

Browse files
Add/link replace (#1419)
* Using developit/unfetch as the Fetch API polyfill * Added the replace prop into the Link component * Added integration test for replace prop on Link component
1 parent d5bd8c7 commit d92ab55

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

lib/link.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ export default class Link extends Component {
5656
scroll = as.indexOf('#') < 0
5757
}
5858

59+
// replace state instead of push if prop is present
60+
const { replace } = this.props
61+
const changeMethod = replace ? 'replace' : 'push'
62+
5963
// straight up redirect
60-
Router.push(href, as)
64+
Router[changeMethod](href, as)
6165
.then((success) => {
6266
if (!success) return
6367
if (scroll) window.scrollTo(0, 0)

readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ export default () => (
292292

293293
That will generate the URL string `/about?name=Zeit`, you can use every property as defined in the [Node.js URL module documentation](https://nodejs.org/api/url.html#url_url_strings_and_url_objects).
294294

295+
The default behaviour for the `<Link>` component is to `push` a new url into the stack. You can use the `replace` prop to prevent adding a new entry.
296+
297+
```jsx
298+
// pages/index.js
299+
import Link from 'next/link'
300+
export default () => (
301+
<div>Click <Link href='/about' replace><a>here</a></Link> to read more</div>
302+
)
303+
```
304+
295305
#### Imperatively
296306

297307
<p><details>

test/integration/basic/pages/nav/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default class extends Component {
3333
>
3434
<a id='query-string-link' style={linkStyle}>QueryString</a>
3535
</Link>
36+
<Link href='/nav/about' replace><a id='about-replace-link' style={linkStyle}>Replace state</a></Link>
3637
<button
3738
onClick={() => this.visitQueryStringPage()}
3839
style={linkStyle}

test/integration/basic/test/client-navigation.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,40 @@ export default (context, render) => {
263263
.toBe(`http://localhost:${context.appPort}/nav/querystring/10#10`)
264264
browser.close()
265265
})
266+
267+
it('should work with the "replace" prop', async () => {
268+
const browser = await webdriver(context.appPort, '/nav')
269+
270+
let stackLength = await browser
271+
.eval('window.history.length')
272+
273+
expect(stackLength).toBe(2)
274+
275+
// Navigation to /about using a replace link should maintain the url stack length
276+
const text = await browser
277+
.elementByCss('#about-replace-link').click()
278+
.waitForElementByCss('.nav-about')
279+
.elementByCss('p').text()
280+
281+
expect(text).toBe('This is the about page.')
282+
283+
stackLength = await browser
284+
.eval('window.history.length')
285+
286+
expect(stackLength).toBe(2)
287+
288+
// Going back to the home with a regular link will augment the history count
289+
await browser
290+
.elementByCss('#home-link').click()
291+
.waitForElementByCss('.nav-home')
292+
293+
stackLength = await browser
294+
.eval('window.history.length')
295+
296+
expect(stackLength).toBe(3)
297+
298+
browser.close()
299+
})
266300
})
267301
})
268302
}

0 commit comments

Comments
 (0)