File tree Expand file tree Collapse file tree 4 files changed +44
-1
lines changed Expand file tree Collapse file tree 4 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ export default class extends Component {
27
27
< Link href = '/empty-get-initial-props' > < a id = 'empty-props' style = { linkStyle } > Empty Props</ a > </ Link >
28
28
< Link href = '/nav/self-reload' > < a id = 'self-reload-link' style = { linkStyle } > Self Reload</ a > </ Link >
29
29
< Link href = '/nav/shallow-routing' > < a id = 'shallow-routing-link' style = { linkStyle } > Shallow Routing</ a > </ Link >
30
+ < Link href = '/nav/redirect' > < a id = 'redirect-link' style = { linkStyle } > Redirect</ a > </ Link >
30
31
< Link
31
32
href = { { pathname : '/nav/querystring' , query : { id : 10 } } }
32
33
as = { { pathname : '/nav/querystring/10' , hash : '10' } }
Original file line number Diff line number Diff line change
1
+ import Router from 'next/router'
2
+
3
+ const Page = ( ) => (
4
+ < p > This is the page</ p >
5
+ )
6
+
7
+ Page . getInitialProps = ( ctx ) => {
8
+ if ( ctx . res ) {
9
+ ctx . res . writeHead ( 302 , { Location : '/nav/about' } )
10
+ ctx . res . end ( )
11
+ } else {
12
+ Router . push ( '/nav/about' )
13
+ }
14
+
15
+ return { }
16
+ }
17
+
18
+ export default Page
Original file line number Diff line number Diff line change @@ -298,5 +298,28 @@ export default (context, render) => {
298
298
browser . close ( )
299
299
} )
300
300
} )
301
+
302
+ describe ( 'with getInitialProp redirect' , ( ) => {
303
+ it ( 'should redirect the page via client side' , async ( ) => {
304
+ const browser = await webdriver ( context . appPort , '/nav' )
305
+ const text = await browser
306
+ . elementByCss ( '#redirect-link' ) . click ( )
307
+ . waitForElementByCss ( '.nav-about' )
308
+ . elementByCss ( 'p' ) . text ( )
309
+
310
+ expect ( text ) . toBe ( 'This is the about page.' )
311
+ browser . close ( )
312
+ } )
313
+
314
+ it ( 'should redirect the page when loading' , async ( ) => {
315
+ const browser = await webdriver ( context . appPort , '/nav/redirect' )
316
+ const text = await browser
317
+ . waitForElementByCss ( '.nav-about' )
318
+ . elementByCss ( 'p' ) . text ( )
319
+
320
+ expect ( text ) . toBe ( 'This is the about page.' )
321
+ browser . close ( )
322
+ } )
323
+ } )
301
324
} )
302
325
}
Original file line number Diff line number Diff line change @@ -48,7 +48,8 @@ describe('Basic Features', () => {
48
48
renderViaHTTP ( context . appPort , '/nav/querystring' ) ,
49
49
renderViaHTTP ( context . appPort , '/nav/self-reload' ) ,
50
50
renderViaHTTP ( context . appPort , '/nav/hash-changes' ) ,
51
- renderViaHTTP ( context . appPort , '/nav/shallow-routing' )
51
+ renderViaHTTP ( context . appPort , '/nav/shallow-routing' ) ,
52
+ renderViaHTTP ( context . appPort , '/nav/redirect' )
52
53
] )
53
54
} )
54
55
afterAll ( ( ) => stopApp ( context . server ) )
You can’t perform that action at this time.
0 commit comments