diff --git a/frontend/scenes/Document/Document.js b/frontend/scenes/Document/Document.js index b7f80090a..b64c0023f 100644 --- a/frontend/scenes/Document/Document.js +++ b/frontend/scenes/Document/Document.js @@ -6,7 +6,7 @@ import { observer, inject } from 'mobx-react'; import { withRouter, Prompt } from 'react-router'; import Flex from 'components/Flex'; import { color, layout } from 'styles/constants'; -import { collectionUrl } from 'utils/routeHelpers'; +import { collectionUrl, updateDocumentUrl } from 'utils/routeHelpers'; import Document from 'models/Document'; import UiStore from 'stores/UiStore'; @@ -92,8 +92,9 @@ type Props = { document.view(); // Update url to match the current one - const urlParts = this.props.match.url.split('/'); - this.props.history.replace([document.url, urlParts.slice(3)].join('/')); + this.props.history.replace( + updateDocumentUrl(this.props.match.url, document.url) + ); } else { // Render 404 with search this.setState({ notFound: true }); diff --git a/frontend/utils/routeHelpers.js b/frontend/utils/routeHelpers.js index ce11b20f3..2a273bd51 100644 --- a/frontend/utils/routeHelpers.js +++ b/frontend/utils/routeHelpers.js @@ -38,3 +38,13 @@ export function searchUrl(query?: string): string { export function notFoundUrl(): string { return '/404'; } + +/** + * Replace full url's document part with the new one in case + * the document slug has been updated + */ +export function updateDocumentUrl(oldUrl: string, newUrl: string): string { + // Update url to match the current one + const urlParts = oldUrl.split('/'); + return [newUrl, urlParts.slice(3)].join('/'); +}