Skip to content

Commit

Permalink
Fix browser back issue of redirects from getServerSideProps / getStat…
Browse files Browse the repository at this point in the history
…icProps
  • Loading branch information
TasukuUno committed Oct 9, 2020
1 parent 2ca66c1 commit 54f4af9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
7 changes: 1 addition & 6 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,7 @@ export default class Router implements BaseRouter {
this._resolveHref(parsedHref, pages)

if (pages.includes(parsedHref.pathname)) {
return this.change(
'replaceState',
destination,
destination,
options
)
return this.change(method, destination, destination, options)
}
}

Expand Down
88 changes: 88 additions & 0 deletions test/integration/gssp-redirect/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,94 @@ const runTests = () => {
},
})
})

it('should not replace history of the origin page when GSSP page is navigated to client-side (internal normal)', async () => {
const browser = await webdriver(appPort, '/another?mark_as=root')

await browser.eval(`(function () {
window.location.href = '/'
})()`)
await browser.waitForElementByCss('#index')

await browser.eval(`(function () {
window.next.router.push('/gssp-blog/redirect-dest-_another')
})()`)
await browser.waitForElementByCss('#another')

await browser.eval(`(function () {
window.history.back()
})()`)

const curUrl = await browser.url()
const { path } = url.parse(curUrl)
expect(path).toEqual('/')
})

it('should not replace history of the origin page when GSSP page is navigated to client-side (external)', async () => {
const browser = await webdriver(appPort, '/another?mark_as=root')

await browser.eval(`(function () {
window.location.href = '/'
})()`)
await browser.waitForElementByCss('#index')

await browser.eval(`(function () {
window.next.router.push('/gssp-blog/redirect-dest-_gssp-blog_first')
})()`)
await browser.waitForElementByCss('#gssp')

await browser.eval(`(function () {
window.history.back()
})()`)

const curUrl = await browser.url()
const { path } = url.parse(curUrl)
expect(path).toEqual('/')
})

it('should not replace history of the origin page when GSP page is navigated to client-side (internal)', async () => {
const browser = await webdriver(appPort, '/another?mark_as=root')

await browser.eval(`(function () {
window.location.href = '/'
})()`)
await browser.waitForElementByCss('#index')

await browser.eval(`(function () {
window.next.router.push('/gsp-blog/redirect-dest-_another')
})()`)
await browser.waitForElementByCss('#another')

await browser.eval(`(function () {
window.history.back()
})()`)

const curUrl = await browser.url()
const { path } = url.parse(curUrl)
expect(path).toEqual('/')
})

it('should not replace history of the origin page when GSP page is navigated to client-side (external)', async () => {
const browser = await webdriver(appPort, '/another?mark_as=root')

await browser.eval(`(function () {
window.location.href = '/'
})()`)
await browser.waitForElementByCss('#index')

await browser.eval(`(function () {
window.next.router.push('/gsp-blog/redirect-dest-_gsp-blog_first')
})()`)
await browser.waitForElementByCss('#gsp')

await browser.eval(`(function () {
window.history.back()
})()`)

const curUrl = await browser.url()
const { path } = url.parse(curUrl)
expect(path).toEqual('/')
})
}

describe('GS(S)P Redirect Support', () => {
Expand Down

0 comments on commit 54f4af9

Please sign in to comment.