@@ -10,9 +10,10 @@ import LoadingIndicator from 'components/LoadingIndicator';
1010import MetaTags from 'components/MetaTags' ;
1111import PT from 'prop-types' ;
1212import React from 'react' ;
13- import { Route , Switch } from 'react-router-dom' ;
13+ import { Route , Switch , Redirect } from 'react-router-dom' ;
1414import Viewport from 'components/Contentful/Viewport' ;
1515import PasswordScreen from 'components/Contentful/PasswordScreen' ;
16+ import { isomorphy } from 'topcoder-react-utils' ;
1617
1718// Concatenates a base and segment and handles optional trailing slashes
1819const buildUrl = ( base , segment ) => `${ _ . trimEnd ( base , '/' ) } /${ _ . trim ( segment , '/' ) } ` ;
@@ -117,6 +118,29 @@ ChildRoutesLoader.propTypes = {
117118 url : PT . string . isRequired ,
118119} ;
119120
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+
120144export default function ContentfulRoute ( props ) {
121145 const {
122146 baseUrl,
@@ -145,7 +169,10 @@ export default function ContentfulRoute(props) {
145169 render = { ( data ) => {
146170 const { fields } = Object . values ( data . entries . items ) [ 0 ] ;
147171 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+ ) : (
149176 < Route
150177 /* This route prevents fetching data about child routes and their
151178 * rendering, if they already known to not match. */
0 commit comments