@tangen/ext-seo 0.2.3 → 0.2.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangen/ext-seo",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Production SEO for a Tangen site: metadata, canonical URLs, robots, sitemaps, redirects and structured data",
5
5
  "homepage": "https://github.com/raghunathj/tangen#readme",
6
6
  "bugs": {
@@ -33,25 +33,25 @@
33
33
  "test": "bun test"
34
34
  },
35
35
  "peerDependencies": {
36
- "@tangen/cms": "^0.2",
37
- "@tangen/icons": "^0.1.0",
38
- "@tangen/ui": "^0.2.4",
36
+ "@tangen/cms": "^0.2.3",
37
+ "@tangen/icons": "^0.1.1",
38
+ "@tangen/ui": "^0.2.5",
39
39
  "react": "*",
40
- "@tangen/sdk": "^0.2"
40
+ "@tangen/sdk": "^0.3.0"
41
41
  },
42
42
  "dependencies": {
43
43
  "clsx": "^2.1.1"
44
44
  },
45
45
  "devDependencies": {
46
- "@tangen/cms": "^0.2.2",
47
- "@tangen/extension-testkit": "0.2.1",
48
- "@tangen/ui": "^0.2.4",
46
+ "@tangen/cms": "^0.2.3",
47
+ "@tangen/extension-testkit": "0.2.2",
48
+ "@tangen/ui": "^0.2.5",
49
49
  "@types/react": "^18.3.3",
50
50
  "clsx": "^2.1.1",
51
51
  "bun-types": "^1.1.0",
52
52
  "react": "^18.3.1",
53
- "@tangen/icons": "^0.1.0",
54
- "react-router-dom": "^7.18.0",
55
- "@tangen/sdk": "^0.2.0"
53
+ "@tangen/icons": "^0.1.1",
54
+ "react-router-dom": "^7.18.4",
55
+ "@tangen/sdk": "^0.3.0"
56
56
  }
57
57
  }
package/src/contract.ts CHANGED
@@ -182,7 +182,7 @@ export interface ResolvedSeo {
182
182
 
183
183
  // ── The application boundary ─────────────────────────────────────────────────
184
184
 
185
- /** What the resolver is asked about. Identity, never a path. */
185
+ /** What the resolver is asked about: identity, plus whatever already answers the address. */
186
186
  export interface SeoUrlInput {
187
187
  workspaceId: string
188
188
  type: string
@@ -196,6 +196,13 @@ export interface SeoUrlInput {
196
196
  * so a resolver that only needs the slug never sees it.
197
197
  */
198
198
  fieldValue?: string | null
199
+ /**
200
+ * The address Tangen composed from the type's declared `path`, locale
201
+ * prefix included. Absent for a type that declares none. A resolver that
202
+ * only wants to prefix or rewrite the declared address reads this and
203
+ * returns, instead of rebuilding a route it did not have to know.
204
+ */
205
+ path?: string | null
199
206
  }
200
207
 
201
208
  /**
@@ -234,14 +241,19 @@ export interface SeoHelpers {
234
241
  * Which FIELD holds a type's URL path, for a site that routes by a field
235
242
  * rather than the slug: `{ page: 'path' }`, or one name for every type.
236
243
  * Tangen reads that one field - never the whole object - and, with
237
- * `siteUrl`, that is enough on its own: a type listed here needs no
238
- * `resolveUrl`. Most sites route by slug and need none of this.
244
+ * `siteUrl`, that is enough on its own.
245
+ *
246
+ * A type that declares `path` in its schema needs neither this nor
247
+ * `resolveUrl`: Tangen composes the address and `siteUrl` makes it
248
+ * absolute. Declaring this for such a type is a deliberate override - a
249
+ * legacy column that still holds the real address, say - and it wins.
239
250
  */
240
251
  urlField?: string | Record<string, string>
241
252
  /**
242
253
  * How this application builds a URL for a content object. Takes precedence
243
- * over `urlField`, and receives that field's value as `fieldValue` when one
244
- * is declared, so a resolver can prefix or localise it.
254
+ * over both `urlField` and the type's declared `path`, and receives each as
255
+ * `fieldValue` and `path`, so a resolver can prefix or localise rather than
256
+ * rebuild the address.
245
257
  */
246
258
  resolveUrl?: SeoUrlResolver
247
259
  /** How this application builds an absolute media URL. */
@@ -0,0 +1,37 @@
1
+ /**
2
+ * File Name: composed-path.ts
3
+ * Purpose: Reading the address Tangen composed. A read that asked for it
4
+ * (`.includePath()`) answers `_path`: the type's declared `path`
5
+ * with every relation and ancestor filled in and the locale prefix
6
+ * applied. `null` for a type that declares none.
7
+ *
8
+ * One function rather than four inline casts, because the key is
9
+ * the platform's contract and this extension reads it in four
10
+ * places - the canonical, the sitemap, llms.txt and a redirect.
11
+ *
12
+ * Author: Raghunath J
13
+ * License: MIT
14
+ */
15
+
16
+ /** The platform's key for a composed address on a read that asked for one. */
17
+ export const PATH_KEY = '_path'
18
+
19
+ /** The composed address on a row, or null - never a guess from the slug. */
20
+ export function composedPath(row: Record<string, unknown> | null | undefined): string | null {
21
+ const value = row?.[PATH_KEY]
22
+ return typeof value === 'string' && value ? value : null
23
+ }
24
+
25
+ /**
26
+ * Whether a type declares an address at all. The gate that keeps a redirect
27
+ * map cheap: a type with no `path` and no declared field has nothing on its
28
+ * row worth reading, so the object is never fetched.
29
+ */
30
+ export function typeDeclaresPath(
31
+ ctx: { types?: () => Array<{ name: string; path?: unknown }> } | undefined,
32
+ type: string,
33
+ ): boolean {
34
+ try {
35
+ return !!ctx?.types?.().find((t) => t.name === type)?.path
36
+ } catch { return false }
37
+ }
@@ -1,11 +1,15 @@
1
1
  /**
2
2
  * File Name: object-url.ts
3
- * Purpose: One place that answers "what is this object's URL", for a site that
4
- * routes by a FIELD rather than by the slug. The site declares the
5
- * field per type (`urlField: { page: 'path' }`); Tangen reads that one
6
- * field - never the whole object - and, with `siteUrl`, that is the
7
- * whole answer. An explicit `resolveUrl` still wins, and receives the
8
- * field's value so it can prefix or localise it. Pure.
3
+ * Purpose: One place that answers "what is this object's URL". Three answers,
4
+ * most explicit first: the site's own `resolveUrl`, then a FIELD the
5
+ * site declared as this type's address (`urlField: { page: 'path' }`),
6
+ * then the address Tangen itself composed from the type's declared
7
+ * `path`. The last is why a site that declares `path` configures
8
+ * nothing here: `siteUrl` is the only thing Tangen cannot know.
9
+ *
10
+ * Every answer is a PATH made absolute under one origin, and a
11
+ * resolver is handed the other two so it can prefix rather than
12
+ * rebuild. Pure.
9
13
  *
10
14
  * Author: Raghunath J
11
15
  * License: MIT
@@ -52,18 +56,33 @@ export interface ObjectUrlInput {
52
56
  slug?: string | null
53
57
  /** The declared field's value, already read by the caller (it holds the row). */
54
58
  fieldValue?: string | null
59
+ /**
60
+ * The address Tangen composed from the type's declared `path` (`_path` on a
61
+ * read that asked for it), locale prefix included. Null for a type that
62
+ * declares no path, which is why the answer below can still be null.
63
+ */
64
+ pathValue?: string | null
65
+ }
66
+
67
+ /** A path is only a path: a bare slug, a protocol-relative host or an absolute URL is not one. */
68
+ const pathLike = (value: string | null | undefined): string | null => {
69
+ const v = value?.trim()
70
+ return v && v.startsWith('/') && !v.startsWith('//') ? v : null
55
71
  }
56
72
 
57
73
  /**
58
- * The object's absolute URL: the site's own resolver first, then the declared
59
- * field under the site's origin. Null means "this object has no page", which
74
+ * The object's absolute URL. Three sources, most explicit first: the site's
75
+ * own resolver, the field it declared for this type, then the address the
76
+ * type's own `path` composes. Null means "this object has no page", which
60
77
  * every caller treats as a reason to omit it rather than invent an address.
61
78
  */
62
79
  export async function objectUrl(input: ObjectUrlInput): Promise<string | null> {
80
+ const composed = pathLike(input.pathValue)
63
81
  const identity = {
64
82
  workspaceId: input.workspaceId, type: input.type, id: input.id,
65
83
  locale: input.locale ?? null, slug: input.slug ?? null,
66
84
  ...(input.fieldValue ? { fieldValue: input.fieldValue } : {}),
85
+ ...(composed ? { path: composed } : {}),
67
86
  }
68
87
  const resolver = input.helpers?.resolveUrl
69
88
  if (resolver) {
@@ -72,11 +91,13 @@ export async function objectUrl(input: ObjectUrlInput): Promise<string | null> {
72
91
  if (answer) return answer
73
92
  } catch { /* a site resolver that throws must not take the page down */ }
74
93
  }
75
- // The convention: the declared field IS the path, under the declared origin.
94
+ // The convention, in the order of how explicitly the site said it: a field
95
+ // it declared for this type, then the address the type itself declares.
76
96
  const origin = originOf(input.helpers)
77
- if (origin && input.fieldValue) {
78
- const candidate = `${origin}${input.fieldValue}`
79
- const checked = validateCanonical(candidate)
97
+ if (!origin) return null
98
+ for (const path of [input.fieldValue, composed]) {
99
+ if (!path) continue
100
+ const checked = validateCanonical(`${origin}${path}`)
80
101
  if (checked.ok) return checked.url
81
102
  }
82
103
  return null
@@ -141,7 +141,7 @@ export function Stat({ label, summary, metric, format }: { label: string; summar
141
141
  <p className="mt-1.5 text-2xl font-semibold tracking-tight text-fg tabular-nums">{format(summary[metric])}</p>
142
142
  {change ? (
143
143
  <p className={`text-ui-xs font-semibold ${change.better ? 'text-success-fg' : 'text-danger-fg'}`}>
144
- {change.delta >= 0 ? '+' : ''}{(change.delta * 100).toFixed(1)}% <span className="font-medium text-fg-faint">vs previous {summary.days} days</span>
144
+ {change.delta >= 0 ? '+' : ''}{(change.delta * 100).toFixed(1)}% <span className="font-medium text-fg-subtle">vs previous {summary.days} days</span>
145
145
  </p>
146
146
  ) : (
147
147
  <p className="text-ui-xs text-fg-subtle">No earlier window to compare</p>
@@ -120,7 +120,7 @@ function HealthCard({ report }: { report: HealthReport }) {
120
120
  </svg>
121
121
  <div className="absolute inset-0 grid place-content-center text-center">
122
122
  <span className="text-[34px] font-semibold tracking-tight text-fg tabular-nums">{report.score ?? '–'}</span>
123
- <span className={`text-[10.5px] font-semibold uppercase tracking-wider ${tone ? TONE_TEXT[tone] : 'text-fg-faint'}`}>{tone ? TONE_LABEL[tone] : 'No content'}</span>
123
+ <span className={`text-[10.5px] font-semibold uppercase tracking-wider ${tone ? TONE_TEXT[tone] : 'text-fg-subtle'}`}>{tone ? TONE_LABEL[tone] : 'No content'}</span>
124
124
  </div>
125
125
  </div>
126
126
  <div className="w-full space-y-2 text-ui-xs">
@@ -115,13 +115,13 @@ export function SeoTitlesPage() {
115
115
  key: 'title', header: 'SEO title', icon: <IconText />, sortKey: 'title',
116
116
  cell: (r) => r.published.title
117
117
  ? <span className="line-clamp-1 text-fg-muted">{r.published.title}</span>
118
- : <span className="text-fg-faint">Falls back to the page title</span>,
118
+ : <span className="text-fg-subtle">Falls back to the page title</span>,
119
119
  },
120
120
  {
121
121
  key: 'description', header: 'Meta description', icon: <IconFileText />,
122
122
  cell: (r) => r.published.description
123
123
  ? <span className="line-clamp-1 text-fg-muted">{r.published.description}</span>
124
- : <span className="text-fg-faint">Not set</span>,
124
+ : <span className="text-fg-subtle">Not set</span>,
125
125
  },
126
126
  {
127
127
  key: 'state', header: 'State', icon: <IconListChecks />, sortKey: 'state',
@@ -23,6 +23,7 @@ import { buildLlmsTxt, type LlmsGroup, type LlmsItem } from '../lib/llms'
23
23
  import { robotsTxtFor, serializeRobotsTxt } from '../lib/robots'
24
24
  import { buildSitemap, SITEMAP_PAGE_SIZE, type SitemapObject } from './sitemap-source'
25
25
  import { objectUrl, urlFieldValue } from '../lib/object-url'
26
+ import { composedPath } from '../lib/composed-path'
26
27
  import { partitionEntries, sitemapIndexXml, sitemapXml } from '../lib/sitemap'
27
28
  import type { ResolvedSeo, SeoHelpers } from '../contract'
28
29
  import type { ExtensionServerCtx } from '@tangen/cms'
@@ -113,7 +114,9 @@ export async function resolvedSeoFor(ctx: ExtensionServerCtx, args: ResolveArgs)
113
114
 
114
115
  let object: Record<string, unknown>
115
116
  try {
116
- const res = await client.objects(args.type as never).status('any').find(args.id)
117
+ // `includePath`: the canonical is the first thing a declared `path` should
118
+ // answer, and the read that fetches the object is the only one that runs.
119
+ const res = await client.objects(args.type as never).status('any').includePath().find(args.id)
117
120
  object = res.data as Record<string, unknown>
118
121
  } catch { return null }
119
122
  if (!object) return null
@@ -143,6 +146,7 @@ export async function resolvedSeoFor(ctx: ExtensionServerCtx, args: ResolveArgs)
143
146
  const resolvedPageUrl = await objectUrl({
144
147
  helpers, workspaceId: ctx.workspaceId, type: args.type, id: args.id, locale: args.locale, slug,
145
148
  fieldValue: urlFieldValue(helpers, args.type, object),
149
+ pathValue: composedPath(object),
146
150
  })
147
151
  const pageUrl = resolvedPageUrl ?? undefined
148
152
  const typeSchema = typeConfig
@@ -196,7 +200,7 @@ export async function* publishedObjects(ctx: ExtensionServerCtx, types: string[]
196
200
  let rows: Record<string, unknown>[] = []
197
201
  try {
198
202
  const res = await client.objects(type as never).status('published')
199
- .sort('id', 'asc').limit(SITEMAP_PAGE_SIZE).offset(offset).get()
203
+ .sort('id', 'asc').limit(SITEMAP_PAGE_SIZE).offset(offset).includePath().get()
200
204
  rows = (res.data ?? []) as Record<string, unknown>[]
201
205
  } catch { break }
202
206
  if (rows.length === 0) break
@@ -206,6 +210,7 @@ export async function* publishedObjects(ctx: ExtensionServerCtx, types: string[]
206
210
  slug: (r.slug as string | null) ?? null,
207
211
  title: (r.title as string | null) ?? null,
208
212
  urlFieldValue: urlFieldValue(helpersOf(ctx), type, r),
213
+ path: composedPath(r),
209
214
  updatedAt: (r.updated_at ?? r.updatedAt) as string | null,
210
215
  publishedAt: (r.published_at ?? r.publishedAt) as string | null,
211
216
  }))
@@ -231,14 +236,14 @@ export async function buildLlmsManifest(ctx: ExtensionServerCtx, opts: { full?:
231
236
  try { for (const t of ctx.types?.() ?? []) typeLabels.set(t.name, (t as { label?: string }).label ?? t.name) } catch { /* names only */ }
232
237
 
233
238
  const groups: LlmsGroup[] = []
234
- if (client && helpers.resolveUrl) {
239
+ if (client) {
235
240
  for (const type of sitemapTypes(ctx)) {
236
241
  const items: LlmsItem[] = []
237
242
  let offset = 0
238
243
  for (;;) {
239
244
  let rows: Record<string, unknown>[] = []
240
245
  try {
241
- const res = await client.objects(type as never).status('published').sort('id', 'asc').limit(SITEMAP_PAGE_SIZE).offset(offset).get()
246
+ const res = await client.objects(type as never).status('published').sort('id', 'asc').limit(SITEMAP_PAGE_SIZE).offset(offset).includePath().get()
242
247
  rows = (res.data ?? []) as Record<string, unknown>[]
243
248
  } catch { break }
244
249
  if (rows.length === 0) break
@@ -249,6 +254,7 @@ export async function buildLlmsManifest(ctx: ExtensionServerCtx, opts: { full?:
249
254
  url = await objectUrl({
250
255
  helpers, workspaceId: ctx.workspaceId, type, id, locale: null,
251
256
  slug: (row.slug as string | null) ?? null, fieldValue: urlFieldValue(helpers, type, row),
257
+ pathValue: composedPath(row),
252
258
  })
253
259
  } catch { /* app owns urls */ }
254
260
  if (!url) continue
@@ -35,6 +35,7 @@ import { readTitles, writeTitle } from './titles'
35
35
  import { optimizeObject } from './optimize'
36
36
  import { parseRedirectCsv, toRedirectCsv } from '../lib/redirects-csv'
37
37
  import { objectUrl, urlFieldName, urlFieldValue } from '../lib/object-url'
38
+ import { composedPath, typeDeclaresPath } from '../lib/composed-path'
38
39
 
39
40
  const XML = { 'Content-Type': 'application/xml; charset=utf-8', 'Cache-Control': 'public, max-age=300' }
40
41
  const TEXT = { 'Content-Type': 'text/plain; charset=utf-8', 'Cache-Control': 'public, max-age=300' }
@@ -549,17 +550,28 @@ async function resolveDestination(ctx: ExtensionServerCtx, redirect: SeoRedirect
549
550
  if (redirect.destination.kind === 'path') return redirect.destination.path
550
551
  const helpers = helpersOf(ctx)
551
552
  const { type, id } = redirect.destination
552
- // Read the ONE field the site declared for this type, and only when it did:
553
- // a redirect map of 10,000 rules must not become 10,000 object reads.
553
+ // Read the object only when something on it could answer the address: the
554
+ // ONE field the site declared for this type, or the type's own `path`. A
555
+ // redirect map of 10,000 rules must not become 10,000 object reads, and a
556
+ // type with neither has no address to find.
557
+ const wantsField = !!urlFieldName(helpers, type)
558
+ // A type's own declaration, read from the vocabulary rather than guessed:
559
+ // without it this is the ONE place that would read an object per rule for
560
+ // nothing, because the page above resolves every rule it lists.
561
+ const wantsPath = typeDeclaresPath(ctx, type)
554
562
  let fieldValue: string | null = null
555
- if (urlFieldName(helpers, type) && ctx.tangen) {
563
+ let pathValue: string | null = null
564
+ if ((wantsField || wantsPath) && ctx.tangen) {
556
565
  try {
557
- const res = await ctx.tangen.objects(type as never).status('any').find(id)
558
- fieldValue = urlFieldValue(helpers, type, (res.data ?? null) as Record<string, unknown> | null)
566
+ const builder = ctx.tangen.objects(type as never).status('any')
567
+ const res = await (wantsPath ? builder.includePath() : builder).find(id)
568
+ const row = (res.data ?? null) as Record<string, unknown> | null
569
+ fieldValue = urlFieldValue(helpers, type, row)
570
+ pathValue = composedPath(row)
559
571
  } catch { /* an object that cannot be read has no URL, which is the answer below */ }
560
572
  }
561
573
  try {
562
- const url = await objectUrl({ helpers, workspaceId: ctx.workspaceId, type, id, locale: null, fieldValue })
574
+ const url = await objectUrl({ helpers, workspaceId: ctx.workspaceId, type, id, locale: null, fieldValue, pathValue })
563
575
  if (!url) return null
564
576
  const checked = validateCanonical(url)
565
577
  return checked.ok ? checked.url : null
@@ -8,9 +8,11 @@
8
8
  * Author: Raghunath J
9
9
  * License: MIT
10
10
  *
11
- * Dependencies/Notes: The URL for each object comes from the application's
12
- * resolver. An object it cannot place has no sitemap entry and the
13
- * run reports the reason; nothing here assembles a path from a slug.
11
+ * Dependencies/Notes: The URL for each object is whatever answers first: the
12
+ * application's resolver, the field it declared, or the address the
13
+ * type's own `path` composes. An object none of them can place has no
14
+ * sitemap entry and the run reports the reason; nothing here
15
+ * assembles a path from a slug.
14
16
  */
15
17
  import { sitemapEligibility, sortEntries, type IneligibleReason, type SitemapEntry } from '../lib/sitemap'
16
18
  import { readStored, pickMetadata } from '../lib/metadata'
@@ -41,6 +43,8 @@ export interface SitemapObject {
41
43
  publishedAt?: string | null
42
44
  /** The declared URL field's value, read by the producer (it holds the row). */
43
45
  urlFieldValue?: string | null
46
+ /** The address the type's declared `path` composes (`_path`), read by the producer. */
47
+ path?: string | null
44
48
  }
45
49
 
46
50
  export interface SitemapBuild {
@@ -88,7 +92,7 @@ export async function buildSitemap(input: {
88
92
 
89
93
  const url = await objectUrl({
90
94
  helpers: helpersWithResolver(input), workspaceId: input.workspaceId, type: object.type, id: object.id,
91
- locale: input.locale, slug: object.slug, fieldValue: object.urlFieldValue,
95
+ locale: input.locale, slug: object.slug, fieldValue: object.urlFieldValue, pathValue: object.path,
92
96
  })
93
97
 
94
98
  const verdict = sitemapEligibility({
@@ -126,7 +130,7 @@ async function resolveAlternates(
126
130
  for (const code of locales) {
127
131
  const href = await objectUrl({
128
132
  helpers: helpersWithResolver(input), workspaceId: input.workspaceId, type: object.type, id: object.id,
129
- locale: code, slug: object.slug, fieldValue: object.urlFieldValue,
133
+ locale: code, slug: object.slug, fieldValue: object.urlFieldValue, pathValue: object.path,
130
134
  })
131
135
  if (href) out.push({ hreflang: code, href })
132
136
  }