@michaelthielemann/kestrel 1.4.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  <script setup lang="ts">
2
2
  import type { LayoutKey } from 'nuxt/app'
3
+ import type { SiteHead } from '../utils/site-head'
3
4
 
4
5
  // The record decides its own layout, so route-meta resolution is opted out of and this page renders the
5
6
  // `<NuxtLayout>` itself. Side effect worth knowing: the layout becomes a CHILD of the page, so it can read
@@ -41,6 +42,7 @@ const { data: resolved } = await useAsyncData(`page:${locale}:${path}`, () =>
41
42
  collection: string | null
42
43
  page: (RenderedPage & Record<string, unknown>) | null
43
44
  alternates?: Array<{ locale: string; path: string }>
45
+ site?: SiteHead | null
44
46
  }),
45
47
  )
46
48
  const page = computed(() => resolved.value?.page ?? null)
@@ -86,6 +88,11 @@ if (!page.value && path !== '/') throw createError({ statusCode: 404, statusMess
86
88
  // og:image) require a configured siteUrl and degrade away without one.
87
89
  const publicRc = useRuntimeConfig().public as { siteUrl?: string; siteName?: string }
88
90
  const seo = page.value?.seo ?? {}
91
+ const siteHead = resolved.value?.site ?? null
92
+ const fallbacks = siteHeadFallbacks(seo, siteHead)
93
+ // og:title stays the bare page title (og:site_name already carries the site); only <title> is composed.
94
+ const pageTitle = seo.title || page.value?.title
95
+ const documentTitle = composeTitle(pageTitle, siteHead)
89
96
  const head = buildPageHead({
90
97
  siteUrl: typeof publicRc.siteUrl === 'string' ? publicRc.siteUrl : '',
91
98
  siteName: typeof publicRc.siteName === 'string' ? publicRc.siteName : '',
@@ -93,9 +100,9 @@ const head = buildPageHead({
93
100
  locale,
94
101
  primary,
95
102
  prefixPrimary,
96
- title: seo.title || page.value?.title,
97
- description: seo.description || undefined,
98
- image: seo.$media?.image ?? null,
103
+ title: pageTitle,
104
+ description: fallbacks.description,
105
+ image: fallbacks.image,
99
106
  alternates: resolved.value?.alternates ?? [],
100
107
  })
101
108
 
@@ -111,8 +118,8 @@ useHead({
111
118
  ],
112
119
  })
113
120
  useSeoMeta({
114
- title: seo.title || page.value?.title,
115
- description: seo.description || undefined,
121
+ title: documentTitle,
122
+ description: fallbacks.description,
116
123
  robots: seo.noindex ? 'noindex, nofollow' : undefined,
117
124
  ogTitle: head.meta.ogTitle,
118
125
  ogDescription: head.meta.ogDescription,
@@ -0,0 +1,43 @@
1
+ export interface SiteHead {
2
+ baseTitle?: string | null
3
+ titleSeparator?: string | null
4
+ titlePosition?: 'before' | 'after' | null
5
+ description?: string | null
6
+ $media?: { image?: { src: string, width: number | null, height: number | null } | null } | null
7
+ }
8
+
9
+ const DEFAULT_SEPARATOR = '|'
10
+
11
+ const trimmed = (v: unknown): string | undefined => {
12
+ const s = typeof v === 'string' ? v.trim() : ''
13
+ return s || undefined
14
+ }
15
+
16
+ /**
17
+ * The `<title>` for a page. Only the document title is composed — `og:title` keeps the bare page title,
18
+ * because `og:site_name` already carries the site.
19
+ */
20
+ export function composeTitle(pageTitle: string | undefined | null, site: SiteHead | null | undefined): string | undefined {
21
+ const page = trimmed(pageTitle)
22
+ const base = trimmed(site?.baseTitle)
23
+ if (!base) return page
24
+ if (!page) return base
25
+ // Migrated content often carries the site name in the page title already; appending it again reads as a
26
+ // bug to every visitor who looks at the tab.
27
+ if (page === base || page.endsWith(base)) return page
28
+ // The separator is stored as a bare token and padded here: a `text` field trims on write, so a stored
29
+ // " | " would come back as "|" and glue the two titles together.
30
+ const separator = trimmed(site?.titleSeparator) ?? DEFAULT_SEPARATOR
31
+ return site?.titlePosition === 'before' ? `${base} ${separator} ${page}` : `${page} ${separator} ${base}`
32
+ }
33
+
34
+ /** The page wins, the site stands in, and both degrade to absent so the tags disappear entirely. */
35
+ export function siteHeadFallbacks(
36
+ seo: { description?: string | null, $media?: { image?: { src: string, width: number | null, height: number | null } | null } | null } | null | undefined,
37
+ site: SiteHead | null | undefined,
38
+ ): { description?: string, image: { src: string, width: number | null, height: number | null } | null } {
39
+ return {
40
+ description: trimmed(seo?.description) ?? trimmed(site?.description),
41
+ image: seo?.$media?.image ?? site?.$media?.image ?? null,
42
+ }
43
+ }
@@ -13,6 +13,14 @@ export default defineEventHandler((event) => {
13
13
  const locale = typeof q.locale === 'string' ? q.locale : undefined
14
14
  const isStaticRender = import.meta.prerender === true || isRendererContext()
15
15
  const publishedOnly = isStaticRender || event.context.readScope !== 'all'
16
- const resolved = resolvePage(useDb(), allCollections(), path, locale, publishedOnly)
17
- return { collection: resolved?.collection ?? null, page: resolved?.page ?? null, alternates: resolved?.alternates ?? [] }
16
+ const db = useDb()
17
+ const resolved = resolvePage(db, allCollections(), path, locale, publishedOnly)
18
+ // The site-wide head tier rides along on the fetch the page already awaits, so it reaches SSR and the
19
+ // prerender on a path that is known to work. Looked up through the registry, not imported, so a consumer
20
+ // that disables the collection gets `null` instead of a query against a table the schema never created.
21
+ // `depth: 1` resolves the sharing image into `$media`; `getSingleton` captures the read, so an edit
22
+ // re-publishes every route that embedded it.
23
+ const siteCollection = getCollection('site')
24
+ const site = siteCollection ? getSingleton(db, siteCollection, locale, false, 1) : null
25
+ return { collection: resolved?.collection ?? null, page: resolved?.page ?? null, alternates: resolved?.alternates ?? [], site }
18
26
  })
@@ -0,0 +1,44 @@
1
+ import { buildCollection } from '../../../fields/server/utils/buildCollection'
2
+ import { defineCollection } from '../../../core/server/utils/defineCollection'
3
+
4
+ // `siteUrl`/`siteName` stay in `kestrel.config` on purpose: the build needs them for canonical URLs, the
5
+ // sitemap and robots.txt, so they cannot live in the DB. What is here is editorial instead — and a write
6
+ // re-publishes the routes that embed it, which a config value frozen at module setup can never do.
7
+ const built = buildCollection(defineCollection({
8
+ name: 'site',
9
+ mode: 'single',
10
+ translatable: true,
11
+ builtin: true,
12
+ label: { singular: { en: 'Site', de: 'Website' }, plural: { en: 'Site', de: 'Website' } },
13
+ icon: 'globe',
14
+ fields: {
15
+ baseTitle: { type: 'text', label: { en: 'Base title', de: 'Basis-Titel' } },
16
+ titleSeparator: { type: 'text', label: { en: 'Title separator', de: 'Titel-Trenner' }, default: '|' },
17
+ titlePosition: {
18
+ type: 'choice',
19
+ label: { en: 'Base title position', de: 'Position des Basis-Titels' },
20
+ options: {
21
+ choices: [
22
+ { label: { en: 'After the page title', de: 'Nach dem Seitentitel' }, value: 'after' },
23
+ { label: { en: 'Before the page title', de: 'Vor dem Seitentitel' }, value: 'before' },
24
+ ],
25
+ display: 'buttons',
26
+ },
27
+ default: 'after',
28
+ },
29
+ description: {
30
+ type: 'text',
31
+ label: { en: 'Default meta description', de: 'Standard-Meta-Beschreibung' },
32
+ options: { multiline: true },
33
+ },
34
+ image: {
35
+ type: 'media',
36
+ label: { en: 'Default sharing image', de: 'Standard-Sharing-Bild' },
37
+ options: { accept: 'image' },
38
+ },
39
+ },
40
+ fieldLayout: [['baseTitle|2', 'titleSeparator|1'], 'titlePosition', 'description', 'image'],
41
+ }))
42
+
43
+ export const site = built.table
44
+ export default built
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@michaelthielemann/kestrel",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "A slim, collection-driven Nuxt 4 CMS meta-layer with a runtime schema engine. Add `extends: ['@michaelthielemann/kestrel']`, define collections, and the database migrates itself.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Michael Thielemann <283621694+MichaelThielemann@users.noreply.github.com>",