diff --git a/packages/gatsby-link/src/__tests__/index.js b/packages/gatsby-link/src/__tests__/index.js index e28c25808e812..0b1e0930e71f5 100644 --- a/packages/gatsby-link/src/__tests__/index.js +++ b/packages/gatsby-link/src/__tests__/index.js @@ -158,6 +158,53 @@ describe(``, () => { getReplace()(`/some-path`) expect(global.___replace).toHaveBeenCalledWith(`/some-path`) }) + + describe(`uses push or replace adequately`, () => { + it(`respects force disabling replace`, () => { + const to = `/` + getNavigate() + const { link } = setup({ linkProps: { to, replace: false } }) + link.click() + + expect( + global.___navigate + ).toHaveBeenCalledWith(`${global.__BASE_PATH__}${to}`, { replace: false }) + }) + + it(`respects force enabling replace`, () => { + const to = `/courses` + getNavigate() + const { link } = setup({ linkProps: { to, replace: true } }) + link.click() + + expect( + global.___navigate + ).toHaveBeenCalledWith(`${global.__BASE_PATH__}${to}`, { replace: true }) + }) + + it(`does not replace history when navigating away`, () => { + const to = `/courses` + getNavigate() + const { link } = setup({ linkProps: { to } }) + link.click() + + expect(global.___navigate).toHaveBeenCalledWith( + `${global.__BASE_PATH__}${to}`, + {} + ) + }) + + it(`does replace history when navigating on the same page`, () => { + const to = `/` + getNavigate() + const { link } = setup({ linkProps: { to } }) + link.click() + + expect( + global.___navigate + ).toHaveBeenCalledWith(`${global.__BASE_PATH__}${to}`, { replace: true }) + }) + }) }) describe(`withPrefix`, () => { @@ -274,8 +321,12 @@ describe(`state`, () => { const { link } = setup({ linkProps: { state } }) link.click() - expect( - global.___navigate - ).toHaveBeenCalledWith(`${global.__BASE_PATH__}${to}`, { state }) + expect(global.___navigate).toHaveBeenCalledWith( + `${global.__BASE_PATH__}${to}`, + { + replace: true, + state, + } + ) }) }) diff --git a/packages/gatsby-link/src/index.js b/packages/gatsby-link/src/index.js index 53f7147c7962b..6c9dd1fe1986a 100644 --- a/packages/gatsby-link/src/index.js +++ b/packages/gatsby-link/src/index.js @@ -169,9 +169,15 @@ class GatsbyLink extends React.Component { ) { e.preventDefault() + let shouldReplace = replace + const isCurrent = encodeURI(to) === window.location.pathname + if (typeof replace !== `boolean` && isCurrent) { + shouldReplace = true + } + // Make sure the necessary scripts and data are // loaded before continuing. - navigate(to, { state, replace }) + navigate(to, { state, replace: shouldReplace }) } return true