diff --git a/src/composables/data.ts b/src/composables/data.ts deleted file mode 100644 index fd337c72..00000000 --- a/src/composables/data.ts +++ /dev/null @@ -1,89 +0,0 @@ -const normalizeVercelResponse = async (r: unknown) => - process.client && r instanceof Blob ? JSON.parse(await r.text()) : r - -export const useAsyncHome = () => - useAsyncData( - () => - ((process.server || process.dev) as true) && - queryContent('/') - .only(['title', 'type', 'body']) - .findOne() - .then(normalizeVercelResponse) - ) - -export const useAsyncUses = () => - useAsyncData( - () => - ((process.server || process.dev) as true) && - queryContent('/uses') - .only(['title', 'type', 'body']) - .findOne() - .then(normalizeVercelResponse) - ) - -export const useAsyncBlogIndex = () => - useAsyncData( - () => - ((process.server || process.dev) as true) && - queryContent('/blog') - .only(['title', 'date', '_path']) - .find() - .then(async r => - process.client && r instanceof Blob ? JSON.parse(await r.text()) : r - ), - { - transform: ( - result: Array<{ title: string; date: string; _path: string }> - ) => { - return result - .map(e => ({ - ...formatDateField(e), - path: e._path, - })) - .sort( - (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime() - ) - }, - } - ) - -interface Talk { - title: string - source: string - tags: string - link: string - date: string - formattedDate: string -} -export const useAsyncTalks = () => - useAsyncData( - () => - ( - ((process.server || process.dev) as true) && - import('../data/talks.json').then(r => r.default) - ).then(async r => - process.client && r instanceof Blob ? JSON.parse(await r.text()) : r - ) as unknown as Promise, - { - transform: talks => { - return ( - (talks - ?.sort( - (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime() - ) - .map(formatDateField) as Talk[]) || [] - ) - }, - } - ) - -export const useAsyncBlogArticle = (path: string) => - useAsyncData( - path, - () => - ((process.server || process.dev) as true) && - queryContent(path) - .only(['title', 'type', 'body', 'date', 'tags']) - .findOne() - .then(normalizeVercelResponse) - ) diff --git a/src/pages/blog/[article].vue b/src/pages/blog/[article].vue index b945737e..67b2ecbc 100644 --- a/src/pages/blog/[article].vue +++ b/src/pages/blog/[article].vue @@ -29,11 +29,24 @@ const route = useRoute() const slug = route.params.article if (!slug) navigateTo('/blog') -const { data: page } = await useAsyncBlogArticle( - useRoute() - .path.replace(/(index)?\.json$/, '') - .replace(/\/$/, '') +const path = useRoute() + .path.replace(/(index)?\.json$/, '') + .replace(/\/$/, '') + +const { data: page } = await useAsyncData( + path, + () => + ((process.server || process.dev) as true) && + queryContent(path).only(['title', 'type', 'body', 'date', 'tags']).findOne() ) + +if (!page.value) { + throw createError({ + statusCode: 404, + fatal: true, + }) +} + const d = new Date(page.value.date) const formattedDate = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}` @@ -63,6 +76,7 @@ useHead({ }, ], }) + if (process.server) { appendHeader( nuxtApp.ssrContext!.event, diff --git a/src/pages/blog/index.vue b/src/pages/blog/index.vue index fa9829f0..01a8ff07 100644 --- a/src/pages/blog/index.vue +++ b/src/pages/blog/index.vue @@ -28,5 +28,19 @@ diff --git a/src/pages/index.vue b/src/pages/index.vue index 1ca4f600..604afe5c 100644 --- a/src/pages/index.vue +++ b/src/pages/index.vue @@ -47,7 +47,11 @@ useHead({ meta: [{ hid: 'og:title', property: 'og:title', content: `Daniel Roe` }], }) -const { data: page } = await useAsyncHome() +const { data: page } = await useAsyncData( + () => + ((process.server || process.dev) as true) && + queryContent('/').only(['title', 'type', 'body']).findOne() +)