Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix browser back issue of redirects from getServerSideProps / getStaticProps #17741

Merged
merged 3 commits into from Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 1 addition & 6 deletions packages/next/next-server/lib/router/router.ts
Expand Up @@ -813,12 +813,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)
Copy link
Contributor Author

@TasukuUno TasukuUno Oct 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In previous code, this replaced history of the origin page not the one with redirect settings with new page. So, I think it is better to use "pushState" for client-side navigation and use "replaceState" in the only case of first navigation of SSG.

}
}

Expand Down
96 changes: 96 additions & 0 deletions test/integration/gssp-redirect/test/index.test.js
Expand Up @@ -123,6 +123,10 @@ const runTests = () => {
window.next.router.push('/gssp-blog/redirect-dest-_another')
})()`)
await browser.waitForElementByCss('#another')

const text = await browser.elementByCss('#another').text()

expect(text).toEqual('another Page')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In first commit, I added them to the existing test cases because there are no any "expect" functions.

})

it('should apply redirect when GSSP page is navigated to client-side (external)', async () => {
Expand All @@ -149,6 +153,10 @@ const runTests = () => {
window.next.router.push('/gsp-blog/redirect-dest-_another')
})()`)
await browser.waitForElementByCss('#another')

const text = await browser.elementByCss('#another').text()

expect(text).toEqual('another Page')
})

it('should apply redirect when GSP page is navigated to client-side (external)', async () => {
Expand All @@ -167,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