Skip to content

Commit

Permalink
refactor: move md data fetching logic to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoidPhantom committed Sep 18, 2024
1 parent c90fe33 commit db8be5d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
21 changes: 21 additions & 0 deletions app/middleware/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { parseMarkdown } from "@nuxtjs/mdc/runtime";

export default defineNuxtRouteMiddleware(async (to) => {
const supabase = useSupabaseClient();

const { data } = await supabase
.from("content")
.select("md")
.eq("slug", to.path)
.maybeSingle();
if (data) {
const { md } = data;
const ast = await parseMarkdown(md);
to.meta.ast = ast;
} else {
return abortNavigation({
statusCode: 404,
message: "ツ",
});
}
});
31 changes: 8 additions & 23 deletions app/pages/[...slug].vue
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
<script setup lang="ts">
import { parseMarkdown } from "@nuxtjs/mdc/runtime";
const supabase = useSupabaseClient();
definePageMeta({
middleware: ["content"],
});
const {
params: { slug },
} = useRoute();
const { data: ast } = await useAsyncData<typeof parseMarkdown>(
`md_${slug}`,
async () => {
const { data } = await supabase
.from("content")
.select("md")
.eq("slug", `/${slug.join("/")}`)
.maybeSingle();
if (!data) return null;
const { md } = data;
return await parseMarkdown(md);
},
);
meta: { ast },
} = useRoute() as any;
const refreshSeo = () => {
if (ast.value) {
if (ast) {
useSeoMeta({
title: ast.value.data.title,
title: ast.data.title,
description:
ast.value.data.description === ""
? undefined
: ast.value.data.description,
ast.data.description === "" ? undefined : ast.data.description,
});
}
};
Expand Down

0 comments on commit db8be5d

Please sign in to comment.