Skip to content

Commit

Permalink
Add support for absolute URLs in useRedirect
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto authored and ogustavo-pereira committed Nov 23, 2021
1 parent 55253db commit 7ec6d8e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
14 changes: 14 additions & 0 deletions packages/ra-core/src/sideEffect/useRedirect.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,18 @@ describe('useRedirect', () => {
state: { _scrollToTop: true, bar: 'baz' },
});
});
it('should support absolute URLs', () => {
const oldLocation = window.location;
delete window.location;
// @ts-ignore
window.location = { href: '' };
const history = createMemoryHistory();
renderWithRedux(
<Router history={history}>
<Redirect redirectTo="https://google.com" />
</Router>
);
expect(window.location.href).toBe('https://google.com');
window.location = oldLocation;
});
});
20 changes: 16 additions & 4 deletions packages/ra-core/src/sideEffect/useRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,22 @@ const useRedirect = () => {
return;
}

history.push({
...parsePath(resolveRedirectTo(redirectTo, basePath, id, data)),
state: { _scrollToTop: true, ...state },
});
if (
typeof redirectTo === 'string' &&
redirectTo.startsWith('http') &&
window
) {
// redirection to an absolute url
// history doesn't handle that case, so we handle it by hand
window.location.href = redirectTo;
} else {
history.push({
...parsePath(
resolveRedirectTo(redirectTo, basePath, id, data)
),
state: { _scrollToTop: true, ...state },
});
}
},
[dispatch, history]
);
Expand Down

0 comments on commit 7ec6d8e

Please sign in to comment.