Skip to content

Commit f1ad4df

Browse files
committed
fix: resolve blog content on direct links
1 parent d8585cf commit f1ad4df

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

composables/useBlogContent.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { queryContent } from '#imports'
12
import type { BlogArticle, BlogAlternateLanguageLink, BlogAlternateHeader } from '~/types/blog'
23

34
export interface BlogOverviewOptions {
@@ -79,21 +80,32 @@ export function useBlogArticle() {
7980
const config = useRuntimeConfig()
8081

8182
// Slug derived from catch-all route param; reactive on client navigation
82-
const slug = computed<string | undefined>(() => {
83+
const slugSegments = computed<string[]>(() => {
8384
const p = (route.params as any)?.slug
84-
if (Array.isArray(p) && p.length) return String(p[p.length - 1])
85-
if (typeof p === 'string') return p
86-
const parts = (route.path || '').split('/')
87-
return parts.at(3)
85+
if (Array.isArray(p) && p.length) return p.map(String)
86+
if (typeof p === 'string' && p.length > 0) return [p]
87+
88+
const parts = (route.path || '').split('/').filter(Boolean)
89+
const blogIndex = parts.indexOf('blog')
90+
if (blogIndex !== -1) return parts.slice(blogIndex + 1)
91+
return []
8892
})
8993

94+
const slug = computed<string | undefined>(() => slugSegments.value.at(-1))
95+
const contentPath = computed(
96+
() => `/${['blog', locale.value, ...slugSegments.value].filter(Boolean).join('/')}`
97+
)
98+
9099
const { data: article } = useAsyncData<BlogArticle | null>(
91100
() => `${route.path}-${locale.value}`,
92-
() => {
93-
// @ts-ignore queryCollection is provided by @nuxt/content
94-
return queryCollection('blog_' + (locale?.value || 'de'))
95-
.where('slug', '=', slug.value as any)
96-
.first()
101+
async () => {
102+
if (!slug.value) return null
103+
104+
const doc = await queryContent(`blog/${locale.value}`)
105+
.where('_path', '=', contentPath.value)
106+
.findOne()
107+
108+
return (doc as BlogArticle | null) || null
97109
},
98110
{ watch: [locale, slug] }
99111
)

0 commit comments

Comments
 (0)