diff --git a/cypress/integration/common/search.js b/cypress/integration/common/search.js index de24bdccb..1c1981581 100644 --- a/cypress/integration/common/search.js +++ b/cypress/integration/common/search.js @@ -47,8 +47,8 @@ Then("I should be on the post's page", () => { '/post/' ) cy.location('pathname').should( - 'contain', - '/101-essays-that-will-change-the-way-you-think' + 'eq', + '/post/p1/101-essays-that-will-change-the-way-you-think' ) }) diff --git a/webapp/mixins/persistentLinks.js b/webapp/mixins/persistentLinks.js new file mode 100644 index 000000000..e902f2fbe --- /dev/null +++ b/webapp/mixins/persistentLinks.js @@ -0,0 +1,32 @@ +export default function(options = {}) { + const { queryId, querySlug, path, message = 'Page not found.' } = options + return { + asyncData: async context => { + const { + params: { id, slug }, + redirect, + error, + app: { apolloProvider } + } = context + const idOrSlug = id || slug + + const variables = { idOrSlug } + const client = apolloProvider.defaultClient + + let response + let thing + response = await client.query({ query: queryId, variables }) + thing = response.data[Object.keys(response.data)[0]][0] + if (thing && thing.slug === slug) return // all good + if (thing && thing.slug !== slug) { + return redirect(`/${path}/${thing.id}/${thing.slug}`) + } + + response = await client.query({ query: querySlug, variables }) + thing = response.data[Object.keys(response.data)[0]][0] + if (thing) return redirect(`/${path}/${thing.id}/${thing.slug}`) + + return error({ statusCode: 404, message }) + } + } +} diff --git a/webapp/pages/post/_id.vue b/webapp/pages/post/_id.vue index 5870a1867..21dd4b292 100644 --- a/webapp/pages/post/_id.vue +++ b/webapp/pages/post/_id.vue @@ -18,25 +18,32 @@ diff --git a/webapp/pages/profile/_id.vue b/webapp/pages/profile/_id.vue index 16a56d3eb..4d436146c 100644 --- a/webapp/pages/profile/_id.vue +++ b/webapp/pages/profile/_id.vue @@ -4,52 +4,31 @@