Skip to content

Commit

Permalink
[APM] Hash-based links into APM app don't work (#108777)
Browse files Browse the repository at this point in the history
* fixing navigation issues

* addressing PR comments
  • Loading branch information
cauemarcondes authored Aug 19, 2021
1 parent 1fd7038 commit 6317202
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ const apmRoutes = route([
}),
}),
]),
defaults: {
query: {
rangeFrom: 'now-15m',
rangeTo: 'now',
},
},
},
{
path: '/',
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/apm/public/components/routing/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { i18n } from '@kbn/i18n';
import { Outlet } from '@kbn/typed-react-router-config';
import * as t from 'io-ts';
import React from 'react';
import { Redirect } from 'react-router-dom';
import { RedirectTo } from '../redirect_to';
import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values';
import { environmentRt } from '../../../../common/environment_rt';
import { BackendDetailOverview } from '../../app/backend_detail_overview';
Expand Down Expand Up @@ -121,7 +121,7 @@ export const home = {
},
{
path: '/',
element: <Redirect to="/services" />,
element: <RedirectTo pathname="/services" />,
},
],
} as const;
63 changes: 40 additions & 23 deletions x-pack/plugins/apm/public/components/routing/redirect_to.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,50 @@
*/

import React from 'react';
import { Redirect, RouteComponentProps } from 'react-router-dom';
import { Location } from 'history';
import { Redirect, useLocation, RouteComponentProps } from 'react-router-dom';

/**
* Given a path, redirect to that location, preserving the search and maintaining
* backward-compatibilty with legacy (pre-7.9) hash-based URLs.
* Function that returns a react component to redirect to a given pathname removing hash-based URLs
* @param pathname
*/
export function redirectTo(to: string) {
export function redirectTo(pathname: string) {
return ({ location }: RouteComponentProps<{}>) => {
let resolvedUrl: URL | undefined;
return <RenderRedirectTo location={location} pathname={pathname} />;
};
}

// Redirect root URLs with a hash to support backward compatibility with URLs
// from before we switched to the non-hash platform history.
if (location.pathname === '' && location.hash.length > 0) {
// We just want the search and pathname so the host doesn't matter
resolvedUrl = new URL(location.hash.slice(1), 'http://localhost');
to = resolvedUrl.pathname;
}
/**
* React component to redirect to a given pathname removing hash-based URLs
* @param param0
*/
export function RedirectTo({ pathname }: { pathname: string }) {
const location = useLocation();
return <RenderRedirectTo location={location} pathname={pathname} />;
}

return (
<Redirect
to={{
...location,
hash: '',
pathname: to,
search: resolvedUrl ? resolvedUrl.search : location.search,
}}
/>
);
};
interface Props {
location: Location;
pathname: string;
}

/**
* Given a pathname, redirect to that location, preserving the search and maintaining
* backward-compatibilty with legacy (pre-7.9) hash-based URLs.
*/
function RenderRedirectTo(props: Props) {
const { location } = props;
let search = location.search;
let pathname = props.pathname;

// Redirect root URLs with a hash to support backward compatibility with URLs
// from before we switched to the non-hash platform history.
if (location.pathname === '' && location.hash.length > 0) {
// We just want the search and pathname so the host doesn't matter
const resolvedUrl = new URL(location.hash.slice(1), 'http://localhost');
search = resolvedUrl.search;
pathname = resolvedUrl.pathname;
}

return <Redirect to={{ ...location, hash: '', pathname, search }} />;
}

0 comments on commit 6317202

Please sign in to comment.