Skip to content

Route redirects #5427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ workflows:
filters:
branches:
only:
- gigwork-updates
- route-redirects
# This is alternate dev env for parallel testing
- "build-qa":
context : org-global
Expand Down
31 changes: 29 additions & 2 deletions src/shared/components/Contentful/Route.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import LoadingIndicator from 'components/LoadingIndicator';
import MetaTags from 'components/MetaTags';
import PT from 'prop-types';
import React from 'react';
import { Route, Switch } from 'react-router-dom';
import { Route, Switch, Redirect } from 'react-router-dom';
import Viewport from 'components/Contentful/Viewport';
import PasswordScreen from 'components/Contentful/PasswordScreen';
import { isomorphy } from 'topcoder-react-utils';

// Concatenates a base and segment and handles optional trailing slashes
const buildUrl = (base, segment) => `${_.trimEnd(base, '/')}/${_.trim(segment, '/')}`;
Expand Down Expand Up @@ -117,6 +118,29 @@ ChildRoutesLoader.propTypes = {
url: PT.string.isRequired,
};

function RedirectWithStatus({ from, to, status }) {
return (
<Route
render={({ staticContext }) => {
if (to[0] !== '/' && isomorphy.isClientSide()) {
window.location.href = to;
return null;
}
// there is no `staticContext` on the client, so
// we need to guard against that here
if (staticContext) staticContext.status = status;
return <Redirect from={from} to={to} />;
}}
/>
);
}

RedirectWithStatus.propTypes = {
from: PT.string.isRequired,
to: PT.string.isRequired,
status: PT.number.isRequired,
};

export default function ContentfulRoute(props) {
const {
baseUrl,
Expand Down Expand Up @@ -145,7 +169,10 @@ export default function ContentfulRoute(props) {
render={(data) => {
const { fields } = Object.values(data.entries.items)[0];
const url = path || buildUrl(baseUrl, fields.url);
return (
const redirectToUrl = _.trim(fields.redirectToUrl);
return redirectToUrl ? (
<RedirectWithStatus status={301} from={url} to={redirectToUrl} />
) : (
<Route
/* This route prevents fetching data about child routes and their
* rendering, if they already known to not match. */
Expand Down
1 change: 1 addition & 0 deletions src/shared/routes/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function Routes({ communityId }) {
path="/community/(competitive-programming|data-science|design|development|qa)/how-to-compete"
/>
<Redirect
exact
from="/community/gigs"
to="/gigs"
/>
Expand Down