@@ -10,9 +10,10 @@ import LoadingIndicator from 'components/LoadingIndicator';
10
10
import MetaTags from 'components/MetaTags' ;
11
11
import PT from 'prop-types' ;
12
12
import React from 'react' ;
13
- import { Route , Switch } from 'react-router-dom' ;
13
+ import { Route , Switch , Redirect } from 'react-router-dom' ;
14
14
import Viewport from 'components/Contentful/Viewport' ;
15
15
import PasswordScreen from 'components/Contentful/PasswordScreen' ;
16
+ import { isomorphy } from 'topcoder-react-utils' ;
16
17
17
18
// Concatenates a base and segment and handles optional trailing slashes
18
19
const buildUrl = ( base , segment ) => `${ _ . trimEnd ( base , '/' ) } /${ _ . trim ( segment , '/' ) } ` ;
@@ -117,6 +118,29 @@ ChildRoutesLoader.propTypes = {
117
118
url : PT . string . isRequired ,
118
119
} ;
119
120
121
+ function RedirectWithStatus ( { from, to, status } ) {
122
+ return (
123
+ < Route
124
+ render = { ( { staticContext } ) => {
125
+ if ( to [ 0 ] !== '/' && isomorphy . isClientSide ( ) ) {
126
+ window . location . href = to ;
127
+ return null ;
128
+ }
129
+ // there is no `staticContext` on the client, so
130
+ // we need to guard against that here
131
+ if ( staticContext ) staticContext . status = status ;
132
+ return < Redirect from = { from } to = { to } /> ;
133
+ } }
134
+ />
135
+ ) ;
136
+ }
137
+
138
+ RedirectWithStatus . propTypes = {
139
+ from : PT . string . isRequired ,
140
+ to : PT . string . isRequired ,
141
+ status : PT . number . isRequired ,
142
+ } ;
143
+
120
144
export default function ContentfulRoute ( props ) {
121
145
const {
122
146
baseUrl,
@@ -145,7 +169,10 @@ export default function ContentfulRoute(props) {
145
169
render = { ( data ) => {
146
170
const { fields } = Object . values ( data . entries . items ) [ 0 ] ;
147
171
const url = path || buildUrl ( baseUrl , fields . url ) ;
148
- return (
172
+ const redirectToUrl = _ . trim ( fields . redirectToUrl ) ;
173
+ return redirectToUrl ? (
174
+ < RedirectWithStatus status = { 301 } from = { url } to = { redirectToUrl } />
175
+ ) : (
149
176
< Route
150
177
/* This route prevents fetching data about child routes and their
151
178
* rendering, if they already known to not match. */
0 commit comments