Skip to content

Commit 3f22490

Browse files
authored
Fix browser back issue of redirects from getServerSideProps / getStaticProps (#17741)
* Fix test without expect for redirects from getStaticProps/getServerSideProps * Fix browser back issue of redirects from getServerSideProps / getStaticProps
1 parent d2e7f3c commit 3f22490

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

packages/next/next-server/lib/router/router.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -813,12 +813,7 @@ export default class Router implements BaseRouter {
813813
this._resolveHref(parsedHref, pages)
814814

815815
if (pages.includes(parsedHref.pathname)) {
816-
return this.change(
817-
'replaceState',
818-
destination,
819-
destination,
820-
options
821-
)
816+
return this.change(method, destination, destination, options)
822817
}
823818
}
824819

test/integration/gssp-redirect/test/index.test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ const runTests = () => {
123123
window.next.router.push('/gssp-blog/redirect-dest-_another')
124124
})()`)
125125
await browser.waitForElementByCss('#another')
126+
127+
const text = await browser.elementByCss('#another').text()
128+
129+
expect(text).toEqual('another Page')
126130
})
127131

128132
it('should apply redirect when GSSP page is navigated to client-side (external)', async () => {
@@ -149,6 +153,10 @@ const runTests = () => {
149153
window.next.router.push('/gsp-blog/redirect-dest-_another')
150154
})()`)
151155
await browser.waitForElementByCss('#another')
156+
157+
const text = await browser.elementByCss('#another').text()
158+
159+
expect(text).toEqual('another Page')
152160
})
153161

154162
it('should apply redirect when GSP page is navigated to client-side (external)', async () => {
@@ -167,6 +175,94 @@ const runTests = () => {
167175
},
168176
})
169177
})
178+
179+
it('should not replace history of the origin page when GSSP page is navigated to client-side (internal normal)', async () => {
180+
const browser = await webdriver(appPort, '/another?mark_as=root')
181+
182+
await browser.eval(`(function () {
183+
window.location.href = '/'
184+
})()`)
185+
await browser.waitForElementByCss('#index')
186+
187+
await browser.eval(`(function () {
188+
window.next.router.push('/gssp-blog/redirect-dest-_another')
189+
})()`)
190+
await browser.waitForElementByCss('#another')
191+
192+
await browser.eval(`(function () {
193+
window.history.back()
194+
})()`)
195+
196+
const curUrl = await browser.url()
197+
const { path } = url.parse(curUrl)
198+
expect(path).toEqual('/')
199+
})
200+
201+
it('should not replace history of the origin page when GSSP page is navigated to client-side (external)', async () => {
202+
const browser = await webdriver(appPort, '/another?mark_as=root')
203+
204+
await browser.eval(`(function () {
205+
window.location.href = '/'
206+
})()`)
207+
await browser.waitForElementByCss('#index')
208+
209+
await browser.eval(`(function () {
210+
window.next.router.push('/gssp-blog/redirect-dest-_gssp-blog_first')
211+
})()`)
212+
await browser.waitForElementByCss('#gssp')
213+
214+
await browser.eval(`(function () {
215+
window.history.back()
216+
})()`)
217+
218+
const curUrl = await browser.url()
219+
const { path } = url.parse(curUrl)
220+
expect(path).toEqual('/')
221+
})
222+
223+
it('should not replace history of the origin page when GSP page is navigated to client-side (internal)', async () => {
224+
const browser = await webdriver(appPort, '/another?mark_as=root')
225+
226+
await browser.eval(`(function () {
227+
window.location.href = '/'
228+
})()`)
229+
await browser.waitForElementByCss('#index')
230+
231+
await browser.eval(`(function () {
232+
window.next.router.push('/gsp-blog/redirect-dest-_another')
233+
})()`)
234+
await browser.waitForElementByCss('#another')
235+
236+
await browser.eval(`(function () {
237+
window.history.back()
238+
})()`)
239+
240+
const curUrl = await browser.url()
241+
const { path } = url.parse(curUrl)
242+
expect(path).toEqual('/')
243+
})
244+
245+
it('should not replace history of the origin page when GSP page is navigated to client-side (external)', async () => {
246+
const browser = await webdriver(appPort, '/another?mark_as=root')
247+
248+
await browser.eval(`(function () {
249+
window.location.href = '/'
250+
})()`)
251+
await browser.waitForElementByCss('#index')
252+
253+
await browser.eval(`(function () {
254+
window.next.router.push('/gsp-blog/redirect-dest-_gsp-blog_first')
255+
})()`)
256+
await browser.waitForElementByCss('#gsp')
257+
258+
await browser.eval(`(function () {
259+
window.history.back()
260+
})()`)
261+
262+
const curUrl = await browser.url()
263+
const { path } = url.parse(curUrl)
264+
expect(path).toEqual('/')
265+
})
170266
}
171267

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

0 commit comments

Comments
 (0)