diff --git a/src/lib/components/organisms/Navbar.svelte b/src/lib/components/organisms/Navbar.svelte index da037f8..17dce4b 100644 --- a/src/lib/components/organisms/Navbar.svelte +++ b/src/lib/components/organisms/Navbar.svelte @@ -24,14 +24,6 @@ const toggleMobileMenu = () => (mobileMenu = !mobileMenu); const hideMobileMenu = () => (mobileMenu = false); - - $: postQuery = createQuery({ - queryFn: () => trpc($page).posts.fetchPosts.query(), - queryKey: ["posts"] - }); - - $: posts = $postQuery.data ?? []; - $: currentPost = posts.find((obj) => obj.slug === $page.url.pathname.substring(1)); @@ -60,8 +52,7 @@ {#each Navlinks as Navlink} {@const highlightRoute = clsx({ "!bg-neutral font-bold text-primary active:text-primary dark:text-secondary hover:before:bg-primary/20 hover:before:dark:bg-secondary/20": - $page.url.pathname.includes(Navlink.route) || - (currentPost && Navlink.name === "Blog") + $page.url.pathname.includes(Navlink.route) })}
  • diff --git a/src/routes/blog/+page.svelte b/src/routes/blog/+page.svelte index 3675ff8..7bdf314 100644 --- a/src/routes/blog/+page.svelte +++ b/src/routes/blog/+page.svelte @@ -19,6 +19,7 @@ import type { Post } from "$utils/types"; + // This data is cached by prefetchQuery in +page.ts so no fetch actually happens here $: postQuery = createQuery({ queryFn: () => trpc($page).posts.fetchPosts.query(), queryKey: ["posts"] @@ -79,26 +80,7 @@
    - {#if paginatedPosts.length} - {#each paginatedPosts as post, i} - handleClick(post, event)} - on:keydown={(event) => handleKeyDown(post, event)} - /> - {/each} - ($currentPage = e.detail.page)} - /> - {:else} + {#if $postQuery.isLoading}

    - No Posts Found + Loading Posts...

    + {:else if $postQuery.isSuccess} + {#if paginatedPosts.length} + {#each paginatedPosts as post, i} + handleClick(post, event)} + on:keydown={(event) => handleKeyDown(post, event)} + /> + {/each} + ($currentPage = e.detail.page)} + /> + {:else} +
    + + +

    + No Posts Found +

    +
    + {/if} {/if}
    diff --git a/src/routes/blog/+page.ts b/src/routes/blog/+page.ts new file mode 100644 index 0000000..89c1970 --- /dev/null +++ b/src/routes/blog/+page.ts @@ -0,0 +1,13 @@ +import { trpc } from "$lib/trpc/client"; + +import type { PageLoad } from "../[slug]/$types"; +import type { LoadEvent } from "@sveltejs/kit"; + +export const load: PageLoad = async (event: LoadEvent) => { + const { queryClient } = await event.parent(); + + await queryClient.prefetchQuery({ + queryFn: async () => trpc(event).posts.fetchPosts.query(), + queryKey: ["posts"] + }); +};