Skip to content
Open
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
19 changes: 12 additions & 7 deletions package/src/Guard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useCallback, useContext, useEffect, useMemo } from 'react';
import { __RouterContext as RouterContext } from 'react-router';
import { matchPath, Redirect, Route } from 'react-router-dom';
import { Redirect, Route } from 'react-router-dom';
import { ErrorPageContext, FromRouteContext, GuardContext, LoadingPageContext } from './contexts';
import { usePrevious, useStateRef, useStateWhenMounted } from './hooks';
import renderPage from './renderPage';
Expand Down Expand Up @@ -133,8 +133,17 @@ const Guard: React.FunctionComponent<GuardProps> = ({ children, component, meta,
const { props, redirect } = await resolveAllGuards();
pageProps = props;
routeRedirect = redirect;

// If there's a redirect, determine if it'll be infinite and throw error if so
if (routeRedirect) {
const redirectPath =
typeof routeRedirect === 'string' ? routeRedirect : routeRedirect.pathname;
Comment on lines +139 to +140
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const redirectPath =
typeof routeRedirect === 'string' ? routeRedirect : routeRedirect.pathname;
const redirectPath = routeRedirect?.pathname || routeRedirect;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's actually a TypeScript thing! routeRedirect can either be an object or string, so we need to type-check it first before accessing the pathname property

There's also this way to write it if it's preferable!:

let redirectPath: string | undefined;
if (typeof routeRedirect === 'string') {
  redirectPath = routeRedirect;
} else {
  redirectPath = routeRedirect.pathname;
}

if (redirectPath === routeProps.location.pathname) {
throw new Error('rrg/infinite-redirect');
}
}
} catch (error) {
routeError = error.message || 'Not found.';
routeError = error.message || 'rrg/error';
}

if (currentRequests === getValidationsRequested()) {
Expand Down Expand Up @@ -171,11 +180,7 @@ const Guard: React.FunctionComponent<GuardProps> = ({ children, component, meta,
} else if (routeError) {
return renderPage(ErrorPage, { ...routeProps, error: routeError });
} else if (routeRedirect) {
const pathToMatch = typeof routeRedirect === 'string' ? routeRedirect : routeRedirect.pathname;
const { path, isExact: exact } = routeProps.match;
if (pathToMatch && !matchPath(pathToMatch, { path, exact })) {
return <Redirect to={routeRedirect} />;
}
return <Redirect to={routeRedirect} />;
}
return (
<RouterContext.Provider value={{ ...routeProps, ...pageProps }}>
Expand Down