From 2ca66c1fabb1389576a5ddbc435478ad7c1bdc32 Mon Sep 17 00:00:00 2001 From: TasukuUno Date: Fri, 9 Oct 2020 13:40:44 +0900 Subject: [PATCH 1/2] Fix test without expect for redirects from getStaticProps/getServerSideProps --- test/integration/gssp-redirect/test/index.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/integration/gssp-redirect/test/index.test.js b/test/integration/gssp-redirect/test/index.test.js index bdbb842bd51c..0a467296a8ac 100644 --- a/test/integration/gssp-redirect/test/index.test.js +++ b/test/integration/gssp-redirect/test/index.test.js @@ -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') }) it('should apply redirect when GSSP page is navigated to client-side (external)', async () => { @@ -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 () => { From 54f4af96b4a34c7d5c18d293121e240c60eb936f Mon Sep 17 00:00:00 2001 From: TasukuUno Date: Fri, 9 Oct 2020 18:40:06 +0900 Subject: [PATCH 2/2] Fix browser back issue of redirects from getServerSideProps / getStaticProps --- .../next/next-server/lib/router/router.ts | 7 +- .../gssp-redirect/test/index.test.js | 88 +++++++++++++++++++ 2 files changed, 89 insertions(+), 6 deletions(-) diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index c6d0916aedd2..bf39b7b96ce9 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -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) } } diff --git a/test/integration/gssp-redirect/test/index.test.js b/test/integration/gssp-redirect/test/index.test.js index 0a467296a8ac..5121bdc0f85d 100644 --- a/test/integration/gssp-redirect/test/index.test.js +++ b/test/integration/gssp-redirect/test/index.test.js @@ -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', () => {