Skip to content

Commit

Permalink
feat(article): redirect old links to new links (#193)
Browse files Browse the repository at this point in the history
* feat: add query for getArticleByOldID

* feat: check for old url and redirect
  • Loading branch information
rutajdash authored May 11, 2022
1 parent f5dc8f8 commit c5f784f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 10 additions & 0 deletions client/src/graphql/queries/article/getArticleByOldID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { gql } from '@apollo/client';
import completeArticleData from '../../fragments/article/completeArticleData';

export default gql`
query getArticleByOldID($id: Int!) {
getArticleByOldID(id: $id) {
${completeArticleData}
}
}
`;
18 changes: 17 additions & 1 deletion client/src/pages/article/[...article].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import theme from '../../config/themes/light';

// Queries
import getArticleByID from '../../graphql/queries/article/getArticleByID';
import getArticleByOldID from '../../graphql/queries/article/getArticleByOldID';
import getArticleLink, { getArticleSlug } from '../../utils/getArticleLink';

function ArticlePage({ article }) {
Expand Down Expand Up @@ -148,10 +149,25 @@ function ArticlePage({ article }) {

export async function getStaticProps({
params: {
article: [articleId, articleSlug],
article: [articleId, articleSlug, _date, oldArticleLink],
},
preview,
}) {
if (oldArticleLink) {
const {
data: { getArticleByOldID: article },
} = await GraphClient.query({
query: getArticleByOldID,
variables: { id: parseInt(oldArticleLink.split('-')[0]) },
});
return {
redirect: {
destination: getArticleLink(article.id, article.title),
permanent: false,
},
};
}

const {
data: { getArticleByID: article },
} = await GraphClient.query({
Expand Down

0 comments on commit c5f784f

Please sign in to comment.