Skip to content

Instantly share code, notes, and snippets.

@Glutnix
Created July 15, 2020 06:47
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glutnix/64d768c0e2f780c5247905f4ee4d65ab to your computer and use it in GitHub Desktop.
Save Glutnix/64d768c0e2f780c5247905f4ee4d65ab to your computer and use it in GitHub Desktop.
nuxt + nuxt-composition-api + typescript + @nuxt/content
<template>
<div>
nuxt.js + nuxt-composition-api + typescript + @nuxt/content to work with meta generated by $content in setup,
and have it generate the appropriate tags at generation time!
<journal-post v-if="journal" :post="journal" />
</div>
</template>
<script lang="ts">
import {
defineComponent,
useMeta,
useContext,
computed,
useStatic,
} from 'nuxt-composition-api'
import { Result } from '@nuxt/content';
interface JournalResult extends Result {
title: string;
description: string;
excerpt?: string;
};
const journalSlug = defineComponent({
head: {},
setup () {
const { params, $content } = useContext();
const slug = computed(() => params.value.slug);
const { title, meta } = useMeta();
const journal = useStatic<JournalResult>(async (slug) => {
const theJournal = await $content('journals', slug).fetch<JournalResult>();
const theTitle: theJournal.title,
const theDescription: theJournal.description ?? theJournal.excerpt ?? undefined,
});
title.value = title;
meta.value = [
{ hid: 'description', name: 'description', content: description },
];
return theJournal;
}, slug, 'journal');
return {
journal,
}
},
});
export default journalSlug;
</script>
export default {
head: { /* ... */ },
pwa: { /* ... */ },
buildModules: [
'nuxt-composition-api',
'@nuxt/typescript-build',
],
modules: [
'nuxt-buefy',
'@nuxtjs/pwa',
'@nuxt/content',
],
hooks: {
'content:file:beforeInsert': (document) => {
if (document.extension === '.md') {
document.excerpt = document.text.substring(0, 120);
}
},
},
}
@pkej
Copy link

pkej commented Mar 13, 2021

I get an error:

Module '"@nuxt/content"' has no exported member 'Result'.

And I went into the nuxt/content repository and didn't find it in the types

Though it still works (what little I've adapted)

EDIT:

Seems like the import should be:

import { Result } from 'nuxt'

EDIT 2:
Just noticed that it's not in nuxt either (can't find an annotation file, still works)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment