diff --git a/composables/useBlog.ts b/composables/useBlog.ts index 0038f541..fb513b83 100644 --- a/composables/useBlog.ts +++ b/composables/useBlog.ts @@ -20,9 +20,11 @@ export function useBlog() { }, }) - const data = useState('content:blog', () => []) const route = useRoute() + const allArticles = ref([]) + const articles = ref([]) + // This is important to avoid a merge the URL and some data in storage for each missing query in URL. We cannot directly check for query to avoid having UTM breaking the system. const hasQuery = computed(() => { return route.query.q || route.query['categories[]'] || route.query['packages[]'] || route.query['authors[]'] || route.query.order || route.query.orderby @@ -34,7 +36,7 @@ export function useBlog() { }) const categoriesOptions = computed(() => { - const categories = data.value.flatMap(item => item.categories || []) + const categories = articles.value.flatMap(item => item.categories || []) const dedupe = new Set(categories) return Array.from(dedupe).sort() @@ -47,7 +49,7 @@ export function useBlog() { }) const packagesOptions = computed(() => { - const packages = data.value.flatMap(item => item.packages || []) + const packages = articles.value.flatMap(item => item.packages || []) const dedupe = new Set(packages) return Array.from(dedupe).sort() @@ -60,7 +62,7 @@ export function useBlog() { }) const authorsOptions = computed(() => { - const authors = data.value.flatMap(item => item.authors || []) + const authors = articles.value.flatMap(item => item.authors || []) const dedupe = new Map() authors.forEach((author) => { @@ -99,8 +101,6 @@ export function useBlog() { return route.query.orderBy as LocationQueryValue || defaultOrderBy }) - const articles = useState('articles', () => []) - const updateQuery = (query?: { q?: string, 'categories[]'?: string[], 'packages[]'?: string[], 'authors[]'?: string[], order?: Order, orderBy?: string }) => { navigateTo({ query: { @@ -150,15 +150,15 @@ export function useBlog() { return data } - const search = (data: BlogPostCard[]) => { - if (!q.value) + const search = (data: BlogPostCard[], q: string) => { + if (!q) return data as (BlogPostCard & SearchResult)[] - return miniSearch.search(q.value) as (BlogPostCard & SearchResult)[] + return miniSearch.search(q) as (BlogPostCard & SearchResult)[] } const getArticles = () => { - const searched = search(data.value) + const searched = search(allArticles.value, q.value) const filtered = filter(searched) const sorted = sort(filtered, order.value, orderBy.value) @@ -166,25 +166,19 @@ export function useBlog() { } const fetchBlogArticles = async () => { - if (data.value.length) { - miniSearch.addAll(data.value) - // Do not update articles list on client side to avoid hydration error - return - } + const { data: res, error } = await useAsyncData('content:use-blog:data', () => queryContent('/blog/').only(fields).sort({ publishedAt: -1 }).find()) - try { - const res = await queryContent('/blog/').only(fields).find() - data.value = res as BlogPostCard[] - miniSearch.addAll(res) - articles.value = getArticles() - } - catch (error) { + if (error.value) { throw createError({ - statusCode: 500, - statusMessage: 'Server Error', + statusCode: error.value?.statusCode, + statusMessage: error.value?.statusMessage, fatal: true, }) } + + miniSearch.addAll(res.value as BlogPostCard[]) + allArticles.value = res.value as BlogPostCard[] + articles.value = res.value as BlogPostCard[] } const storage = useStorage('blog', { diff --git a/composables/usePackages.ts b/composables/usePackages.ts index 9ba237f5..8f6bff5e 100644 --- a/composables/usePackages.ts +++ b/composables/usePackages.ts @@ -20,9 +20,11 @@ export function usePackages() { }, }) - const data = useState('content:packages', () => []) const route = useRoute() + const allPackages = ref([]) + const packages = ref([]) + // This is important to avoid a merge the URL and some data in storage for each missing query in URL. We cannot directly check for query to avoid having UTM breaking the system. const hasQuery = computed(() => { return route.query.q || route.query.order || route.query.orderby @@ -63,8 +65,6 @@ export function usePackages() { return route.query.orderBy as LocationQueryValue || defaultOrderBy }) - const packages = useState('packages', () => []) - const updateQuery = (query?: { q?: string, order?: Order, orderBy?: string }) => { navigateTo({ query: { @@ -83,39 +83,34 @@ export function usePackages() { updateQuery(defaultQuery) } - const search = (data: Package[]) => { - if (!q.value) + const search = (data: Package[], q: string) => { + if (!q) return data as (Package & SearchResult)[] - return miniSearch.search(q.value) as (Package & SearchResult)[] + return miniSearch.search(q) as (Package & SearchResult)[] } const getPackages = () => { - const searched = search(data.value) + const searched = search(allPackages.value, q.value) const sorted = sort(searched, order.value, orderBy.value) return sorted } const fetchPackages = async () => { - if (data.value.length) { - miniSearch.addAll(data.value) - return - } + const { data: res, error } = await useAsyncData('content:use-packages:data', () => $fetch('/api/content/packages.json')) - try { - const res = await $fetch('/api/content/packages.json') - data.value = res as Package[] - miniSearch.addAll(data.value) - packages.value = getPackages() - } - catch (error) { + if (error.value) { throw createError({ - statusCode: 500, - statusMessage: 'Server Error', + statusCode: error.value?.statusCode, + statusMessage: error.value?.statusMessage, fatal: true, }) } + + miniSearch.addAll(res.value as Package[]) + allPackages.value = res.value as Package[] + packages.value = res.value as Package[] } const storage = useStorage('packages', { @@ -151,9 +146,9 @@ export function usePackages() { } }) - const numberOfPackages = computed(() => data.value.length) + const numberOfPackages = computed(() => allPackages.value.length) - const monthlyDownloads = computed(() => data.value.reduce((acc, pkg) => { + const monthlyDownloads = computed(() => allPackages.value.reduce((acc, pkg) => { if (!pkg.monthlyDownloads) return acc diff --git a/package.json b/package.json index c15862c8..2c205abd 100644 --- a/package.json +++ b/package.json @@ -49,5 +49,10 @@ "ofetch": "^1.3.3", "slugify": "^1.6.6", "unstorage": "^1.10.1" + }, + "pnpm": { + "patchedDependencies": { + "@nuxt/content@2.10.0": "patches/@nuxt__content@2.10.0.patch" + } } } diff --git a/pages/blog/[...slug].vue b/pages/blog/[slug].vue similarity index 100% rename from pages/blog/[...slug].vue rename to pages/blog/[slug].vue diff --git a/pages/index.vue b/pages/index.vue index b86486b8..a4408c21 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1,13 +1,5 @@