Skip to content

Commit

Permalink
Added canonical link to html head (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfkonecn authored May 3, 2024
1 parent 1d4b3c0 commit ebb5e2c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 29 deletions.
62 changes: 47 additions & 15 deletions src/main/webapp/app/routes/routes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Redirect, Route, Switch } from 'react-router-dom';
import { Redirect, Switch } from 'react-router-dom';
import ErrorBoundaryRoute from 'app/shared/error/error-boundary-route';
import Login from 'app/components/login/LoginPage';
import { Logout } from 'app/components/login/logout';
Expand Down Expand Up @@ -37,6 +37,7 @@ import * as QueryString from 'query-string';
import OncologyTherapiesPage from 'app/pages/oncologyTherapiesPage/oncologyTherapiesPage';
import { NewsPageNavTab } from 'app/pages/newsPage/NewsPageNavTab';
import CompanionDiagnosticDevicePage from 'app/pages/companionDiagnosticDevicesPage/companionDiagnosticDevicePage';
import OncokbRoute from 'app/shared/route/OncokbRoute';

const getOldLevelsRedirectRoute = (hash: string) => {
const queryStrings = QueryString.parse(hash) as {
Expand Down Expand Up @@ -73,7 +74,7 @@ const AppRouts = (props: {
// Redirect needs to be defined first
return (
<Switch>
<Route exact path={PAGE_ROUTE.HOME} component={HomePage} />
<OncokbRoute exact path={PAGE_ROUTE.HOME} component={HomePage} />
<Redirect exact from={'/updates'} to={PAGE_ROUTE.NEWS} />
<Redirect exact from={'/genes'} to={PAGE_ROUTE.CANCER_GENES} />
<Redirect
Expand Down Expand Up @@ -174,13 +175,15 @@ const AppRouts = (props: {
isUserAuthenticated={props.authenticationStore.isUserAuthenticated}
appStore={props.appStore}
path={PAGE_ROUTE.GENE}
addCanonicalLink
component={GenePage}
/>
<RecaptchaBoundaryRoute
exact
isUserAuthenticated={props.authenticationStore.isUserAuthenticated}
appStore={props.appStore}
path={PAGE_ROUTE.ALTERATION}
addCanonicalLink
component={AlterationPage}
/>
<RecaptchaBoundaryRoute
Expand All @@ -202,34 +205,63 @@ const AppRouts = (props: {
isUserAuthenticated={props.authenticationStore.isUserAuthenticated}
appStore={props.appStore}
path={PAGE_ROUTE.ALTERATION_TUMOR_TYPE}
addCanonicalLink
component={AlterationPage}
/>
<Route exact path={PAGE_ROUTE.ABOUT} component={AboutPageNavTab} />
<Route exact path={PAGE_ROUTE.TEAM} component={AboutPageNavTab} />
<Route exact path={PAGE_ROUTE.SOP} component={AboutPageNavTab} />
<Route
<OncokbRoute
exact
path={PAGE_ROUTE.ABOUT}
component={AboutPageNavTab}
/>
<OncokbRoute
exact
path={PAGE_ROUTE.TEAM}
component={AboutPageNavTab}
/>
<OncokbRoute
exact
path={PAGE_ROUTE.SOP}
component={AboutPageNavTab}
/>
<OncokbRoute
exact
path={PAGE_ROUTE.YEAR_END_SUMMARY}
component={NewsPageNavTab}
/>
<Route exact path={PAGE_ROUTE.NEWS} component={NewsPageNavTab} />
<Route
<OncokbRoute
exact
path={PAGE_ROUTE.NEWS}
component={NewsPageNavTab}
/>
<OncokbRoute
exact
path={PAGE_ROUTE.FDA_RECOGNITION}
component={AboutPageNavTab}
/>
<Route
<OncokbRoute
exact
path={PAGE_ROUTE.TERMS}
component={ApiAccessPageNavTab}
/>
<Route exact path={PAGE_ROUTE.LEVELS}>
<OncokbRoute exact path={PAGE_ROUTE.LEVELS}>
{getOldLevelsRedirectRoute(window.location.hash)}
</Route>
<Route exact path={PAGE_ROUTE.DX} component={LevelOfEvidencePage} />
<Route exact path={PAGE_ROUTE.PX} component={LevelOfEvidencePage} />
<Route exact path={PAGE_ROUTE.V2} component={LevelOfEvidencePage} />
<Route
</OncokbRoute>
<OncokbRoute
exact
path={PAGE_ROUTE.DX}
component={LevelOfEvidencePage}
/>
<OncokbRoute
exact
path={PAGE_ROUTE.PX}
component={LevelOfEvidencePage}
/>
<OncokbRoute
exact
path={PAGE_ROUTE.V2}
component={LevelOfEvidencePage}
/>
<OncokbRoute
exact
path={PAGE_ROUTE.FDA_NGS}
component={LevelOfEvidencePage}
Expand Down
5 changes: 2 additions & 3 deletions src/main/webapp/app/shared/auth/RecaptchaBoundaryRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import ErrorBoundaryRoute from 'app/shared/error/error-boundary-route';
import { observer } from 'mobx-react';
import React from 'react';
import { RouteProps } from 'react-router-dom';
import { ContactLink } from '../links/ContactLink';
import AppStore from 'app/store/AppStore';
import { OncokbRouteProps } from '../route/OncokbRoute';

export interface IRecaptchaBoundaryRoute extends RouteProps {
export interface IRecaptchaBoundaryRoute extends OncokbRouteProps {
isUserAuthenticated: boolean;
appStore: AppStore;
}
Expand Down
12 changes: 4 additions & 8 deletions src/main/webapp/app/shared/auth/private-route.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import React from 'react';
import {
Route,
Redirect,
RouteProps,
RouteComponentProps,
} from 'react-router-dom';
import { Redirect, RouteComponentProps } from 'react-router-dom';
import { observer } from 'mobx-react';
import ErrorBoundary from 'app/shared/error/error-boundary';
import AuthenticationStore from 'app/store/AuthenticationStore';
Expand All @@ -13,8 +8,9 @@ import { PAGE_ROUTE } from 'app/config/constants';
import { isAuthorized } from 'app/shared/auth/AuthUtils';
import { getRedirectLoginState } from 'app/shared/utils/Utils';
import { AppConfig } from 'app/appConfig';
import OncokbRoute, { OncokbRouteProps } from '../route/OncokbRoute';

export interface IPrivateRouteProps extends RouteProps {
export interface IPrivateRouteProps extends OncokbRouteProps {
authenticationStore: AuthenticationStore;
routing: RouterStore;
hasAnyAuthorities?: string[];
Expand Down Expand Up @@ -81,6 +77,6 @@ export const PrivateRoute = observer(
}`
);

return <Route {...rest} render={renderRedirect} />;
return <OncokbRoute {...rest} render={renderRedirect} />;
}
);
7 changes: 4 additions & 3 deletions src/main/webapp/app/shared/error/error-boundary-route.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';
import { Route, RouteProps, RouteComponentProps } from 'react-router-dom';
import { RouteComponentProps } from 'react-router-dom';
import ErrorBoundary from 'app/shared/error/error-boundary';
import OncokbRoute, { OncokbRouteProps } from '../route/OncokbRoute';

export const ErrorBoundaryRoute = ({
component,
render,
...rest
}: RouteProps) => {
}: OncokbRouteProps) => {
const encloseInErrorBoundary = (props: RouteComponentProps) => (
<ErrorBoundary>
{render ? <>{render(props)}</> : React.createElement(component!, props)}
Expand All @@ -18,7 +19,7 @@ export const ErrorBoundaryRoute = ({
`A component needs to be specified for path ${(rest as any).path}`
);

return <Route {...rest} render={encloseInErrorBoundary} />;
return <OncokbRoute {...rest} render={encloseInErrorBoundary} />;
};

export default ErrorBoundaryRoute;
52 changes: 52 additions & 0 deletions src/main/webapp/app/shared/route/OncokbRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useEffect } from 'react';
import { RouteProps, Route, RouteComponentProps } from 'react-router';

type CanonicalLinkProps = { routeProps: RouteComponentProps };

function CanonicalLink({ routeProps }: CanonicalLinkProps) {
useEffect(() => {
const canonicalEndpoint = routeProps.location.pathname;
const linkNode = document.createElement('link');

linkNode.setAttribute('id', 'canonical');
linkNode.setAttribute('rel', 'canonical');
linkNode.setAttribute(
'href',
`${location.protocol}${location.host}${canonicalEndpoint}`
);

document.head.appendChild(linkNode);

return () => {
document.head.removeChild(linkNode);
};
}, [routeProps]);
return <></>;
}

export type OncokbRouteProps = RouteProps & {
addCanonicalLink?: boolean;
};

export default function OncokbRoute({
render,
component,
addCanonicalLink = false,
...rest
}: OncokbRouteProps) {
const newRender = (props: RouteComponentProps) => {
return render ? (
<>
{render(props)}
{addCanonicalLink && <CanonicalLink routeProps={props} />}
</>
) : (
<>
{React.createElement(component!, props)}

{addCanonicalLink && <CanonicalLink routeProps={props} />}
</>
);
};
return <Route render={newRender} {...rest} />;
}

0 comments on commit ebb5e2c

Please sign in to comment.