Skip to content

Commit

Permalink
improved loading performance of blog a lot more
Browse files Browse the repository at this point in the history
  • Loading branch information
probablyjassin committed Nov 25, 2024
1 parent 0118b38 commit bb64b4f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
22 changes: 0 additions & 22 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,6 @@ useHead({
],
});
interface BlogPost {
_path: string;
title: string;
description?: string;
image?: string;
date?: string;
_file: string;
_dir: string;
}
const { data: posts, status, error, refresh, clear } = await useAsyncData("blog-posts", async () =>
await queryContent<BlogPost>("blog")
.where({
_partial: false,
_draft: false,
_path: { $ne: "/blog" }, // Exclude index page
})
.sort({ _path: 1 })
.find()
);
const postsState = useState<BlogPost[]>("posts", () => posts.value || []);
</script>

<style>
Expand Down
40 changes: 30 additions & 10 deletions components/RelatedPosts.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
<template>
<section v-if="postsState?.length" class="mt-8">
<h2 class="text-2xl font-bold mb-4">Did you also read these? 👀</h2>
<ul>
<li v-for="post in shuffled(postsState.filter((p) => p._path !== $route.path).slice(0, 3))"
:key="post._path">
<NuxtLink :to="post._path" class="text-primary underline">{{ post.title }}</NuxtLink>
</li>
</ul>
</section>
<Suspense>
<template #default>
<section v-if="posts?.length" class="mt-8">
<h2 class="text-2xl font-bold mb-4">Did you also read these? 👀</h2>
<ul>
<li v-for="post in shuffled(posts.filter((p) => p._path !== $route.path).slice(0, 3))"
:key="post._path">
<NuxtLink :to="post._path" class="text-primary underline">{{ post.title }}</NuxtLink>
</li>
</ul>
</section>
</template>
<template #fallback>
<div class="animate-pulse">
<div class="h-4 bg-[var(--text-200)] rounded w-3/4 mb-4"></div>
<div class="h-4 bg-[var(--text-200)] rounded w-1/2 mb-4"></div>
</div>
</template>
</Suspense>

</template>

<script setup lang="ts">
Expand All @@ -21,7 +32,16 @@ interface BlogPost {
_dir: string;
}
const postsState = useState<BlogPost[]>("posts", () => []);
const { data: posts, status, error, refresh, clear } = await useLazyAsyncData("blog-posts", async () =>
await queryContent<BlogPost>("blog")
.where({
_partial: false,
_draft: false,
_path: { $ne: "/blog" }, // Exclude index page
})
.sort({ _path: 1 })
.find()
);
const shuffled = <T>(items: T[]): T[] =>
items
Expand Down
2 changes: 1 addition & 1 deletion pages/blog/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<main>
<ContentLoader />
<hr />
<RelatedPosts v-if="showRelated" :current-path="$route.path" />
<RelatedPosts v-if="showRelated" />
</main>
</template>
<template #fallback>
Expand Down
5 changes: 2 additions & 3 deletions pages/blog/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<main class="max-w-4xl mb-4 mx-auto px-4">
<h1 class="text-4xl font-bold mb-8">Things I wrote about</h1>

<div v-if="pending && !posts?.length"
class="animate-pulse grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
<div v-if="!posts?.length" class="animate-pulse grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
<div v-for="i in 6" :key="i" class="mb-8 bg-slate-800 rounded-3xl h-[35em]">
<div class="px-6 w-full rounded-lg border border-[var(--primary-200)]"></div>
</div>
Expand Down Expand Up @@ -64,7 +63,7 @@ interface BlogPost {
_dir: string;
}
const { data: posts, pending } = await useAsyncData("blog-posts", () =>
const { data: posts, status, error, refresh, clear } = await useAsyncData("blog-posts", () =>
queryContent<BlogPost>("blog")
.where({
_partial: false,
Expand Down

0 comments on commit bb64b4f

Please sign in to comment.