Skip to content

Commit

Permalink
fix: 404 for missing devblogs
Browse files Browse the repository at this point in the history
  • Loading branch information
kernoeb committed Oct 1, 2023
1 parent 0ffbc81 commit 6391377
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions pages/devblog/_id.vue
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,34 @@ export default {
data () {
return {
devblog: null,
error404: false,
otherDevblogs: [],
mdiGithub
}
},
async fetch () {
const id = this.$route.params.id
const devblog = await this.$content('devblogs', id).fetch()
devblog.id = id
devblog.author = await this.$content('members', devblog.authorId).fetch()
this.devblog = devblog
try {
const id = this.$route.params.id
const devblog = await this.$content('devblogs', id).fetch()
devblog.id = id
devblog.author = await this.$content('members', devblog.authorId).fetch()
this.devblog = devblog
const devblogs = await this.$content('devblogs').without(['body']).where({ slug: { $ne: id } }).sortBy('timestamp', 'desc').limit(3).fetch()
const authorIds = [...new Set(devblogs.map(v => v.authorId))]
const authors = await this.$content('members').where({ username: { $in: authorIds } }).fetch()
devblogs.forEach((v) => {
v.id = v.slug
v.author = authors.find(a => a.slug === v.authorId)
})
this.otherDevblogs = devblogs
const devblogs = await this.$content('devblogs').without(['body']).where({ slug: { $ne: id } }).sortBy('timestamp', 'desc').limit(3).fetch()
const authorIds = [...new Set(devblogs.map(v => v.authorId))]
const authors = await this.$content('members').where({ username: { $in: authorIds } }).fetch()
devblogs.forEach((v) => {
v.id = v.slug
v.author = authors.find(a => a.slug === v.authorId)
})
this.otherDevblogs = devblogs
} catch (e) {
if (e.message && e.message.includes('not found')) {
this.error404 = true
} else {
console.error(e)
}
}
},
head () {
if (!this.devblog) {
Expand All @@ -151,6 +160,13 @@ export default {
}
]
}
},
watch: {
error404 (val) {
if (val) {
this.$router.push('/404')
}
}
}
}
</script>

0 comments on commit 6391377

Please sign in to comment.