Skip to content

Commit

Permalink
feat: allow to disable content head (#2142)
Browse files Browse the repository at this point in the history

Co-authored-by: nobkd <44443899+nobkd@users.noreply.github.com>
  • Loading branch information
Barbapapazes and nobkd committed Aug 23, 2023
1 parent 978cd46 commit 89862d7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 5 deletions.
9 changes: 8 additions & 1 deletion docs/content/4.api/3.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineNuxtConfig({
- Type: `Record<string, any>`{lang=ts}
- Default: `{ baseURL: '/api/_content' }`{lang=ts}

Change default behaviour of Content APIs.
Change default behaviour of Content APIs.

- `baseURL`{lang=ts}: change the base URL of Content APIs. Default is `/api/_content`.

Expand Down Expand Up @@ -366,6 +366,13 @@ Default locale for top level contents. Module will use first locale code from `l
Note that in case of defining multiple locales, Module will filter content with `defaultLocale`. If you want to fetch contents of another locale, you need to use `where` option.
::

## `contentHead`

- Type: `boolean`{lang=ts}
- Default: `true`{lang=ts}

Enable content head feature. If enabled, module will automatically use `useContentHead` composable to inject content head data to your page.

## `documentDriven`

- Type: `boolean | object`{lang=ts}
Expand Down
1 change: 1 addition & 0 deletions playground/custom-seo/content/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Content
11 changes: 11 additions & 0 deletions playground/custom-seo/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default defineNuxtConfig({
extends: ['../shared'],

content: {
contentHead: false
},

typescript: {
includeWorkspace: true
}
})
16 changes: 16 additions & 0 deletions playground/custom-seo/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script lang="ts" setup>
useServerHead({
title: 'My custom title',
meta: [
{
hid: 'description',
name: 'description',
content: 'My custom description'
}
]
})
</script>

<template>
<ContentDoc />
</template>
10 changes: 10 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ export interface ModuleOptions {
* @default undefined
*/
defaultLocale?: string
/**
* Enable automatic usage of `useContentHead`
*
* @default true
*/
contentHead?: boolean
/**
* Document-driven mode config
*
Expand Down Expand Up @@ -290,6 +296,7 @@ export default defineNuxtModule<ModuleOptions>({
navigation: {
fields: []
},
contentHead: true,
documentDriven: false,
respectPathCase: false,
experimental: {
Expand Down Expand Up @@ -658,6 +665,7 @@ export default defineNuxtModule<ModuleOptions>({
documentDriven: options.documentDriven as any,
host: typeof options.documentDriven !== 'boolean' ? options.documentDriven?.host ?? '' : '',
trailingSlash: typeof options.documentDriven !== 'boolean' ? options.documentDriven?.trailingSlash ?? false : false,
contentHead: options.contentHead ?? true,
// Anchor link generation config
anchorLinks: options.markdown.anchorLinks as { depth?: number, exclude?: number[] }
})
Expand Down Expand Up @@ -789,6 +797,8 @@ interface ModulePublicRuntimeConfig {

navigation: ModuleOptions['navigation']

contentHead: ModuleOptions['contentHead']

documentDriven: ModuleOptions['documentDriven']
}

Expand Down
12 changes: 9 additions & 3 deletions src/runtime/components/ContentDoc.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { type PropType, type VNode, defineComponent, h, useSlots } from 'vue'
import { withTrailingSlash } from 'ufo'
import { useRuntimeConfig } from '#app'
import type { ParsedContent, QueryBuilderParams } from '../types'
import ContentRenderer from './ContentRenderer.vue'
import ContentQuery from './ContentQuery.vue'
Expand Down Expand Up @@ -61,7 +62,7 @@ const ContentDoc = defineComponent({
head: {
type: Boolean,
required: false,
default: true
default: undefined
}
},
Expand All @@ -74,10 +75,15 @@ const ContentDoc = defineComponent({
* @slot not-found
*/
render (ctx: any) {
const { contentHead } = useRuntimeConfig().public.content
const slots = useSlots()
const { tag, excerpt, path, query, head } = ctx
// Allow user to overwrite the global `contentHead` config.
const shouldInjectContentHead = head === undefined ? contentHead : head
// Merge local `path` props and apply `findOne` query default.
const contentQueryProps = {
...query || {},
Expand All @@ -94,12 +100,12 @@ const ContentDoc = defineComponent({
// Default slot
default: slots?.default
? ({ data, refresh, isPartial }: any) => {
if (head) { useContentHead(data) }
if (shouldInjectContentHead) { useContentHead(data) }
return slots.default?.({ doc: data, refresh, isPartial, excerpt, ...this.$attrs })
}
: ({ data }: any) => {
if (head) { useContentHead(data) }
if (shouldInjectContentHead) { useContentHead(data) }
return h(
ContentRenderer,
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/pages/document-driven.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script setup lang="ts">
import { useRuntimeConfig } from '#app'
import { useContent, useContentHead, useRequestEvent } from '#imports'
const { contentHead } = useRuntimeConfig().public.content
const { page, layout } = useContent()
// Page not found, set correct status code on SSR
Expand All @@ -9,7 +11,9 @@ if (!(page as any).value && process.server) {
event.res.statusCode = 404
}
useContentHead(page)
if (contentHead) {
useContentHead(page)
}
</script>

<template>
Expand Down

0 comments on commit 89862d7

Please sign in to comment.