@tangen/ext-seo 0.0.0-stage → 0.2.1
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 +55 -4
- package/src/client.ts +48 -0
- package/src/components/SeoPanel.tsx +489 -0
- package/src/contract.ts +269 -0
- package/src/hooks.ts +74 -0
- package/src/index.ts +41 -0
- package/src/lib/alt-text.ts +59 -0
- package/src/lib/analytics.ts +236 -0
- package/src/lib/canonical.ts +87 -0
- package/src/lib/deployment-role.ts +18 -0
- package/src/lib/health-report.ts +93 -0
- package/src/lib/llms.ts +55 -0
- package/src/lib/metadata.ts +268 -0
- package/src/lib/not-found.ts +83 -0
- package/src/lib/object-url.ts +83 -0
- package/src/lib/optimize.ts +77 -0
- package/src/lib/redirects-csv.ts +84 -0
- package/src/lib/redirects.ts +178 -0
- package/src/lib/resolve.ts +157 -0
- package/src/lib/robots.ts +76 -0
- package/src/lib/schema-map.ts +66 -0
- package/src/lib/schemas.ts +114 -0
- package/src/lib/seo-analysis.ts +185 -0
- package/src/lib/seo-manager-nav.ts +72 -0
- package/src/lib/settings.ts +167 -0
- package/src/lib/sitemap-report.ts +38 -0
- package/src/lib/sitemap.ts +109 -0
- package/src/lib/structured-data.ts +93 -0
- package/src/lib/titles.ts +109 -0
- package/src/nextjs.ts +70 -0
- package/src/pages/AiGeoTab.tsx +78 -0
- package/src/pages/AnalyticsTab.tsx +192 -0
- package/src/pages/GeoTab.tsx +76 -0
- package/src/pages/NotFoundTab.tsx +120 -0
- package/src/pages/SchemasTab.tsx +134 -0
- package/src/pages/SeoManagerPage.tsx +329 -0
- package/src/pages/SettingsTab.tsx +298 -0
- package/src/pages/SitemapTab.tsx +51 -0
- package/src/pages/TitlesTab.tsx +326 -0
- package/src/pages/ToolsTab.tsx +142 -0
- package/src/pages/manager-pages.tsx +46 -0
- package/src/pages/redirects.tsx +411 -0
- package/src/server/analytics-read.ts +51 -0
- package/src/server/analytics-sync.ts +182 -0
- package/src/server/artifacts.ts +305 -0
- package/src/server/health.ts +97 -0
- package/src/server/not-found-store.ts +71 -0
- package/src/server/optimize.ts +75 -0
- package/src/server/redirect-store.ts +128 -0
- package/src/server/routes.ts +604 -0
- package/src/server/search-console.ts +148 -0
- package/src/server/sitemap-source.ts +153 -0
- package/src/server/titles.ts +88 -0
- package/src/server.ts +47 -0
- package/tangen.extension.ts +153 -0
- package/README.md +0 -3
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Name: search-console.ts
|
|
3
|
+
* Purpose: Search Console for the SEO manager, through the GOOGLE CONNECTOR
|
|
4
|
+
* PACK (plan-connectors F2).
|
|
5
|
+
*
|
|
6
|
+
* SEO used to offer its own `google-search-console` connector and
|
|
7
|
+
* speak the API itself. It no longer does either: the pack owns the
|
|
8
|
+
* provider, its `google.search-console@1` service owns the API, and
|
|
9
|
+
* this module only decides WHICH connection to read and says
|
|
10
|
+
* honestly what is missing when it cannot.
|
|
11
|
+
*
|
|
12
|
+
* That is the whole point of the connector programme from a
|
|
13
|
+
* consumer's side - an extension that wants Search Console asks for
|
|
14
|
+
* a capability and gets a typed service, and knows nothing about
|
|
15
|
+
* OAuth, scopes, refresh, or Google's URLs.
|
|
16
|
+
*
|
|
17
|
+
* Nothing is lost by the move. An operator's existing connection is
|
|
18
|
+
* ADOPTED by the pack (`supersedes`), so the same account, the same
|
|
19
|
+
* consent and the same id already stored in these settings keep
|
|
20
|
+
* working.
|
|
21
|
+
*
|
|
22
|
+
* Author: Raghunath J
|
|
23
|
+
* License: MIT
|
|
24
|
+
*/
|
|
25
|
+
import type { ExtensionServerCtx } from '@tangen/cms'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The half of `google.search-console@1` this extension uses, declared HERE.
|
|
29
|
+
*
|
|
30
|
+
* Not imported from the pack, deliberately: SEO must build, install and run in
|
|
31
|
+
* a project that has no Google pack at all, and a package it imported would be
|
|
32
|
+
* a runtime dependency on something optional. A service contract is a shape,
|
|
33
|
+
* and this is the shape SEO asks for - the platform hands over whatever the
|
|
34
|
+
* active provider registered under that ref.
|
|
35
|
+
*/
|
|
36
|
+
interface SearchConsoleService {
|
|
37
|
+
listSites(input: { connectionId: string }): Promise<SearchConsoleSite[]>
|
|
38
|
+
query(input: {
|
|
39
|
+
connectionId: string
|
|
40
|
+
siteUrl: string
|
|
41
|
+
startDate: string
|
|
42
|
+
endDate: string
|
|
43
|
+
dimensions?: string[]
|
|
44
|
+
rowLimit?: number
|
|
45
|
+
}): Promise<SearchConsoleRow[]>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface SearchConsoleSite { siteUrl: string; permissionLevel: string }
|
|
49
|
+
export interface SearchConsoleRow { keys: string[]; clicks: number; impressions: number; ctr: number; position: number }
|
|
50
|
+
|
|
51
|
+
/** The pack's service, and the capability a connection needs for it. */
|
|
52
|
+
export const SEARCH_CONSOLE_SERVICE = 'google.search-console@^1'
|
|
53
|
+
export const SEARCH_CONSOLE_CAPABILITY = 'google.search-console.read'
|
|
54
|
+
/** The provider a connection belongs to, for the picker and for resolution. */
|
|
55
|
+
export const SEARCH_CONSOLE_PROVIDER = 'google'
|
|
56
|
+
|
|
57
|
+
/** The row shape the analytics sync stores. Unchanged by the move. */
|
|
58
|
+
export interface SearchAnalyticsRow extends SearchConsoleRow {}
|
|
59
|
+
|
|
60
|
+
export interface SearchAnalyticsQuery {
|
|
61
|
+
siteUrl: string
|
|
62
|
+
startDate: string
|
|
63
|
+
endDate: string
|
|
64
|
+
dimensions: Array<'date' | 'page' | 'query' | 'country' | 'device'>
|
|
65
|
+
rowLimit?: number
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type SearchConsoleStatus =
|
|
69
|
+
| { connected: true; connectionId: string; label: string; property: string | null }
|
|
70
|
+
| { connected: false; reason: string }
|
|
71
|
+
|
|
72
|
+
/** What this module hands back: the status, and - when there is one - a reader. */
|
|
73
|
+
export interface SearchConsoleReader {
|
|
74
|
+
listSites(): Promise<SearchConsoleSite[]>
|
|
75
|
+
query(q: SearchAnalyticsQuery): Promise<SearchAnalyticsRow[]>
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const PACK_MISSING =
|
|
79
|
+
'Search Console needs the Google connector pack: install @tangen/connectors-google and enable it, then connect a Google account under Settings > Connections. Everything else in SEO works without it.'
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The connection the operator chose (SEO Manager > Settings), else the
|
|
83
|
+
* provider's first usable one - with a reason an operator can act on when
|
|
84
|
+
* there is none.
|
|
85
|
+
*
|
|
86
|
+
* Three quite different absences, told apart rather than merged: the pack is
|
|
87
|
+
* not here, the grant was not approved, or nothing is connected yet.
|
|
88
|
+
*/
|
|
89
|
+
export async function searchConsoleConnection(
|
|
90
|
+
ctx: ExtensionServerCtx,
|
|
91
|
+
): Promise<{ status: SearchConsoleStatus; reader?: SearchConsoleReader }> {
|
|
92
|
+
const service = ctx.services?.get<SearchConsoleService>(SEARCH_CONSOLE_SERVICE)
|
|
93
|
+
if (!service) return { status: { connected: false, reason: PACK_MISSING } }
|
|
94
|
+
if (!ctx.connections) {
|
|
95
|
+
return {
|
|
96
|
+
status: {
|
|
97
|
+
connected: false,
|
|
98
|
+
reason: `Reading Search Console needs the ${SEARCH_CONSOLE_CAPABILITY} grant: approve it with \`tangen extension upgrade seo --approve-optional connections:${SEARCH_CONSOLE_CAPABILITY}\`.`,
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const settings: Record<string, unknown> = (await ctx.settings?.get().catch(() => ({}))) ?? {}
|
|
104
|
+
const chosenId = typeof settings.searchConsole === 'string' && settings.searchConsole ? settings.searchConsole : undefined
|
|
105
|
+
const property = typeof settings.searchConsoleProperty === 'string' && settings.searchConsoleProperty.trim()
|
|
106
|
+
? settings.searchConsoleProperty.trim()
|
|
107
|
+
: null
|
|
108
|
+
|
|
109
|
+
// The platform's own derivation says what the state is and why, in the same
|
|
110
|
+
// words the Connections page and every picker show - so SEO never invents a
|
|
111
|
+
// second explanation for the same situation.
|
|
112
|
+
const available = await ctx.connections.availability?.(SEARCH_CONSOLE_CAPABILITY)
|
|
113
|
+
let conn: Awaited<ReturnType<NonNullable<ExtensionServerCtx['connections']>['get']>>
|
|
114
|
+
try {
|
|
115
|
+
conn = await ctx.connections.get(SEARCH_CONSOLE_PROVIDER, {
|
|
116
|
+
...(chosenId ? { id: chosenId } : {}),
|
|
117
|
+
capabilities: ['search-console.read'],
|
|
118
|
+
})
|
|
119
|
+
} catch (e) {
|
|
120
|
+
return { status: { connected: false, reason: (e as Error).message } }
|
|
121
|
+
}
|
|
122
|
+
if (!conn) {
|
|
123
|
+
return {
|
|
124
|
+
status: {
|
|
125
|
+
connected: false,
|
|
126
|
+
reason: chosenId
|
|
127
|
+
? 'The Google connection chosen on the SEO Manager\'s Settings tab no longer exists or is not active.'
|
|
128
|
+
: available?.reason ?? 'No Google connection yet: add one under Settings > Connections.',
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const connectionId = conn.id
|
|
134
|
+
return {
|
|
135
|
+
status: { connected: true, connectionId, label: conn.label, property },
|
|
136
|
+
reader: {
|
|
137
|
+
listSites: () => service.listSites({ connectionId }),
|
|
138
|
+
query: (q) => service.query({
|
|
139
|
+
connectionId,
|
|
140
|
+
siteUrl: q.siteUrl,
|
|
141
|
+
startDate: q.startDate,
|
|
142
|
+
endDate: q.endDate,
|
|
143
|
+
...(q.dimensions ? { dimensions: q.dimensions as never } : {}),
|
|
144
|
+
...(q.rowLimit ? { rowLimit: q.rowLimit } : {}),
|
|
145
|
+
}),
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Name: sitemap-source.ts
|
|
3
|
+
* Purpose: Building the sitemap from published content (closure plan Phase 9A,
|
|
4
|
+
* Units 7 and 20). Bounded iteration and a SINGLE metadata read for
|
|
5
|
+
* the whole run - not one query per object, which is the failure this
|
|
6
|
+
* file exists to avoid.
|
|
7
|
+
*
|
|
8
|
+
* Author: Raghunath J
|
|
9
|
+
* License: MIT
|
|
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.
|
|
14
|
+
*/
|
|
15
|
+
import { sitemapEligibility, sortEntries, type IneligibleReason, type SitemapEntry } from '../lib/sitemap'
|
|
16
|
+
import { readStored, pickMetadata } from '../lib/metadata'
|
|
17
|
+
import type { SeoHelpers, SeoUrlResolver } from '../contract'
|
|
18
|
+
import type { ExtensionStore } from '@tangen/cms'
|
|
19
|
+
import { objectUrl } from '../lib/object-url'
|
|
20
|
+
|
|
21
|
+
/** How many objects one query reads. Bounded, so a large site pages. */
|
|
22
|
+
export const SITEMAP_PAGE_SIZE = 500
|
|
23
|
+
|
|
24
|
+
/** A hard stop, so a runaway site cannot exhaust the process building a file. */
|
|
25
|
+
export const SITEMAP_MAX_OBJECTS = 200_000
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The sitemap takes its resolver as its own argument (a caller may hand one in
|
|
29
|
+
* that the helpers do not name), so the URL answer must see that one.
|
|
30
|
+
*/
|
|
31
|
+
const helpersWithResolver = (input: { helpers: SeoHelpers; resolveUrl?: SeoUrlResolver }): SeoHelpers =>
|
|
32
|
+
(input.resolveUrl ? { ...input.helpers, resolveUrl: input.resolveUrl } : input.helpers)
|
|
33
|
+
|
|
34
|
+
export interface SitemapObject {
|
|
35
|
+
id: string
|
|
36
|
+
type: string
|
|
37
|
+
slug?: string | null
|
|
38
|
+
/** The object's own title, for the health report; the sitemap itself never emits it. */
|
|
39
|
+
title?: string | null
|
|
40
|
+
updatedAt?: string | null
|
|
41
|
+
publishedAt?: string | null
|
|
42
|
+
/** The declared URL field's value, read by the producer (it holds the row). */
|
|
43
|
+
urlFieldValue?: string | null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface SitemapBuild {
|
|
47
|
+
entries: SitemapEntry[]
|
|
48
|
+
/** Why objects were left out, counted. The answer to "where is my page?". */
|
|
49
|
+
skipped: Record<IneligibleReason, number>
|
|
50
|
+
scanned: number
|
|
51
|
+
/** True when the scan stopped at its bound rather than at the end. */
|
|
52
|
+
truncated: boolean
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const emptySkipped = (): Record<IneligibleReason, number> =>
|
|
56
|
+
({ NOT_PUBLISHED: 0, NOINDEX: 0, EXCLUDED: 0, UNRESOLVED_URL: 0, TYPE_NOT_ELIGIBLE: 0 })
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Turns published objects into sitemap entries.
|
|
60
|
+
*
|
|
61
|
+
* The published SEO metadata for every object is read ONCE, as one prefix
|
|
62
|
+
* scan of the extension's own store, and looked up in memory per object. The
|
|
63
|
+
* alternative - a read per object - is the N+1 the measurements in the Phase
|
|
64
|
+
* 9A report check for.
|
|
65
|
+
*/
|
|
66
|
+
export async function buildSitemap(input: {
|
|
67
|
+
workspaceId: string
|
|
68
|
+
locale?: string | null
|
|
69
|
+
helpers: SeoHelpers
|
|
70
|
+
store: ExtensionStore
|
|
71
|
+
/** Pages of published objects, already bounded by the caller. */
|
|
72
|
+
objects: AsyncIterable<SitemapObject[]>
|
|
73
|
+
resolveUrl?: SeoUrlResolver
|
|
74
|
+
}): Promise<SitemapBuild> {
|
|
75
|
+
const metadataByObject = await loadPublishedMetadata(input.store)
|
|
76
|
+
const skipped = emptySkipped()
|
|
77
|
+
const entries: SitemapEntry[] = []
|
|
78
|
+
let scanned = 0
|
|
79
|
+
let truncated = false
|
|
80
|
+
|
|
81
|
+
for await (const page of input.objects) {
|
|
82
|
+
for (const object of page) {
|
|
83
|
+
if (scanned >= SITEMAP_MAX_OBJECTS) { truncated = true; break }
|
|
84
|
+
scanned++
|
|
85
|
+
|
|
86
|
+
const scoped = metadataByObject.get(object.id)
|
|
87
|
+
const metadata = scoped ? pickMetadata(scoped, input.locale) : {}
|
|
88
|
+
|
|
89
|
+
const url = await objectUrl({
|
|
90
|
+
helpers: helpersWithResolver(input), workspaceId: input.workspaceId, type: object.type, id: object.id,
|
|
91
|
+
locale: input.locale, slug: object.slug, fieldValue: object.urlFieldValue,
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
const verdict = sitemapEligibility({
|
|
95
|
+
published: true, metadata, type: object.type, url, sitemapTypes: input.helpers.sitemapTypes,
|
|
96
|
+
})
|
|
97
|
+
if (!verdict.eligible) { skipped[verdict.reason]++; continue }
|
|
98
|
+
|
|
99
|
+
const entry: SitemapEntry = { loc: url! }
|
|
100
|
+
const lastModified = object.updatedAt ?? object.publishedAt
|
|
101
|
+
if (lastModified) entry.lastModified = new Date(lastModified).toISOString()
|
|
102
|
+
|
|
103
|
+
const alternates = await resolveAlternates(input, object)
|
|
104
|
+
if (alternates.length) entry.alternates = alternates
|
|
105
|
+
|
|
106
|
+
entries.push(entry)
|
|
107
|
+
}
|
|
108
|
+
if (truncated) break
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return { entries: sortEntries(entries), skipped, scanned, truncated }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Alternates only for locales that ACTUALLY resolved. A site that cannot
|
|
116
|
+
* place its French page gets no `hreflang="fr"` rather than a fabricated one:
|
|
117
|
+
* a wrong alternate is a worse answer than a missing one.
|
|
118
|
+
*/
|
|
119
|
+
async function resolveAlternates(
|
|
120
|
+
input: { workspaceId: string; locale?: string | null; helpers: SeoHelpers; resolveUrl?: SeoUrlResolver },
|
|
121
|
+
object: SitemapObject,
|
|
122
|
+
): Promise<Array<{ hreflang: string; href: string }>> {
|
|
123
|
+
const locales = input.helpers.locales
|
|
124
|
+
if (!locales?.length || !input.resolveUrl) return []
|
|
125
|
+
const out: Array<{ hreflang: string; href: string }> = []
|
|
126
|
+
for (const code of locales) {
|
|
127
|
+
const href = await objectUrl({
|
|
128
|
+
helpers: helpersWithResolver(input), workspaceId: input.workspaceId, type: object.type, id: object.id,
|
|
129
|
+
locale: code, slug: object.slug, fieldValue: object.urlFieldValue,
|
|
130
|
+
})
|
|
131
|
+
if (href) out.push({ hreflang: code, href })
|
|
132
|
+
}
|
|
133
|
+
return out
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function safeResolve(resolver: SeoUrlResolver | undefined, args: Parameters<SeoUrlResolver>[0]): Promise<string | null> {
|
|
137
|
+
if (!resolver) return null
|
|
138
|
+
try { return (await resolver(args)) ?? null } catch { return null }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Every object's PUBLISHED SEO snapshot, in one prefix scan. The draft is
|
|
143
|
+
* deliberately not read: a sitemap is a public artifact.
|
|
144
|
+
*/
|
|
145
|
+
async function loadPublishedMetadata(store: ExtensionStore) {
|
|
146
|
+
const map = new Map<string, ReturnType<typeof readStored>['draft']>()
|
|
147
|
+
const rows = await store.list('obj_', { limit: SITEMAP_MAX_OBJECTS })
|
|
148
|
+
for (const { key, data } of rows) {
|
|
149
|
+
const stored = readStored(data)
|
|
150
|
+
if (stored.published) map.set(key, stored.published)
|
|
151
|
+
}
|
|
152
|
+
return map
|
|
153
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Name: titles.ts
|
|
3
|
+
* Purpose: The Titles & Meta table's server side (plan-seo-manager-v2 V2):
|
|
4
|
+
* the published objects with both halves of their stored SEO, and
|
|
5
|
+
* the edit an editor makes there. The edit writes the DRAFT through
|
|
6
|
+
* the same codec the panel writes through, so Tangen's publishing
|
|
7
|
+
* semantics hold - the live snapshot changes when the object is
|
|
8
|
+
* published, never because a table row was saved.
|
|
9
|
+
*
|
|
10
|
+
* Author: Raghunath J
|
|
11
|
+
* License: MIT
|
|
12
|
+
*/
|
|
13
|
+
import type { ExtensionServerCtx } from '@tangen/cms'
|
|
14
|
+
import { publishedObjects, sitemapTypes } from './artifacts'
|
|
15
|
+
import { readStored, writeDraft } from '../lib/metadata'
|
|
16
|
+
import { countTitleStates, filterTitleRows, sortTitleRows, titleRowOf, TITLES_PAGE_SIZE, type TitleRow, type TitleSort, type TitleState } from '../lib/titles'
|
|
17
|
+
|
|
18
|
+
/** The scan's ceiling, the health report's: a table over a bounded set beats an unbounded one. */
|
|
19
|
+
export const TITLES_SCAN_LIMIT = 20_000
|
|
20
|
+
const STORE_PAGE = 1000
|
|
21
|
+
|
|
22
|
+
export interface TitlesView {
|
|
23
|
+
rows: TitleRow[]
|
|
24
|
+
total: number
|
|
25
|
+
scanned: number
|
|
26
|
+
counts: Record<TitleState, number>
|
|
27
|
+
types: string[]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Every published object with its stored SEO, filtered and paged. */
|
|
31
|
+
export async function readTitles(ctx: ExtensionServerCtx, opts: { type?: string | null; state?: string | null; search?: string | null; limit?: number; offset?: number; locale?: string | null; sort?: TitleSort; desc?: boolean } = {}): Promise<TitlesView> {
|
|
32
|
+
const objects: Array<{ id: string; type: string; title: string | null }> = []
|
|
33
|
+
scan: for await (const page of publishedObjects(ctx, sitemapTypes(ctx))) {
|
|
34
|
+
for (const o of page) {
|
|
35
|
+
objects.push({ id: o.id, type: o.type, title: o.title ?? null })
|
|
36
|
+
if (objects.length >= TITLES_SCAN_LIMIT) break scan
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// One pass over the store rather than a read per object.
|
|
40
|
+
const wanted = new Set(objects.map((o) => o.id))
|
|
41
|
+
const stored = new Map<string, unknown>()
|
|
42
|
+
for (let offset = 0; ; offset += STORE_PAGE) {
|
|
43
|
+
const rows = await ctx.store.list('', { limit: STORE_PAGE, offset })
|
|
44
|
+
for (const row of rows) if (wanted.has(row.key)) stored.set(row.key, row.data)
|
|
45
|
+
if (rows.length < STORE_PAGE) break
|
|
46
|
+
}
|
|
47
|
+
const all = objects.map((o) => titleRowOf({ id: o.id, type: o.type, title: o.title, stored: stored.get(o.id), locale: opts.locale ?? null }))
|
|
48
|
+
const filtered = sortTitleRows(filterTitleRows(all, opts), opts)
|
|
49
|
+
const offset = Math.max(0, Math.floor(opts.offset ?? 0))
|
|
50
|
+
const limit = Math.max(1, Math.min(Math.floor(opts.limit ?? TITLES_PAGE_SIZE), 100))
|
|
51
|
+
return {
|
|
52
|
+
rows: filtered.slice(offset, offset + limit),
|
|
53
|
+
total: filtered.length,
|
|
54
|
+
scanned: all.length,
|
|
55
|
+
counts: countTitleStates(all),
|
|
56
|
+
types: [...new Set(all.map((r) => r.type))].sort(),
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type TitleWrite = { ok: true; row: TitleRow } | { ok: false; reason: string }
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Writes a title and description onto the object's DRAFT. The published
|
|
64
|
+
* snapshot is left exactly as it is: it changes when the object is published,
|
|
65
|
+
* which is what the row tells the editor. `null` clears a field.
|
|
66
|
+
*/
|
|
67
|
+
export async function writeTitle(ctx: ExtensionServerCtx, input: { id: string; type?: string; title?: string | null; description?: string | null; locale?: string | null }): Promise<TitleWrite> {
|
|
68
|
+
const id = String(input.id ?? '').trim()
|
|
69
|
+
if (!id) return { ok: false, reason: 'id is required' }
|
|
70
|
+
if (input.locale) return { ok: false, reason: 'Per-locale values are edited in the object\'s SEO panel, where the locale switcher lives.' }
|
|
71
|
+
const current = readStored(await ctx.store.get(id))
|
|
72
|
+
const base = { ...current.draft.base }
|
|
73
|
+
if (input.title !== undefined) {
|
|
74
|
+
const t = typeof input.title === 'string' ? input.title.trim() : ''
|
|
75
|
+
if (t) base.title = t; else delete base.title
|
|
76
|
+
}
|
|
77
|
+
if (input.description !== undefined) {
|
|
78
|
+
const d = typeof input.description === 'string' ? input.description.trim() : ''
|
|
79
|
+
if (d) base.description = d; else delete base.description
|
|
80
|
+
}
|
|
81
|
+
// writeDraft REPLACES the draft, so it carries every locale and every other field through.
|
|
82
|
+
const next = writeDraft(current, { ...current.draft, base })
|
|
83
|
+
await ctx.store.put(id, next as unknown as Record<string, unknown>)
|
|
84
|
+
return {
|
|
85
|
+
ok: true,
|
|
86
|
+
row: titleRowOf({ id, type: String(input.type ?? ''), title: null, stored: next }),
|
|
87
|
+
}
|
|
88
|
+
}
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Name: server.ts
|
|
3
|
+
* Purpose: The SEO extension's server contributions (closure plan Phase 9A) -
|
|
4
|
+
* referenced from `tangen.extension.ts` as `server: './src/server.ts'`.
|
|
5
|
+
* Everything here is gated by the platform on the extension being
|
|
6
|
+
* ACTIVE in the resolved workspace x environment, so a disabled SEO
|
|
7
|
+
* serves nothing and hooks nothing.
|
|
8
|
+
*
|
|
9
|
+
* Author: Raghunath J
|
|
10
|
+
* License: MIT
|
|
11
|
+
*/
|
|
12
|
+
import { defineExtensionServer } from '@tangen/cms'
|
|
13
|
+
import { seoContentHooks } from './hooks'
|
|
14
|
+
import { readStored, writeDraft } from './lib/metadata'
|
|
15
|
+
import { seoRoutes } from './server/routes'
|
|
16
|
+
import { armAnalyticsSync, runAnalyticsSyncTask, syncAfterSettingsChange, SYNC_TASK } from './server/analytics-sync'
|
|
17
|
+
|
|
18
|
+
export default defineExtensionServer({
|
|
19
|
+
contentHooks: seoContentHooks,
|
|
20
|
+
// The panel-data contract (SEO Manager slice 1): the panel emits a scoped bag,
|
|
21
|
+
// the store holds { schemaVersion, draft, published? }, and this is the ONLY
|
|
22
|
+
// transform between them - the same functions the afterChange hook uses.
|
|
23
|
+
panelDataCodec: {
|
|
24
|
+
read: (stored) => readStored(stored).draft as unknown as Record<string, unknown>,
|
|
25
|
+
write: (current, panel) => writeDraft(readStored(current), panel) as unknown as Record<string, unknown>,
|
|
26
|
+
},
|
|
27
|
+
adminRoutes: seoRoutes,
|
|
28
|
+
// V3: the Search Console connector; core owns the OAuth flow and the tokens.
|
|
29
|
+
|
|
30
|
+
// V4: the daily Search Console sync is a platform task, not a timer of our
|
|
31
|
+
// own; it runs under the extension's authority because no member asked for it.
|
|
32
|
+
tasks: [{
|
|
33
|
+
name: SYNC_TASK.split(':').pop()!,
|
|
34
|
+
description: 'Brings the last days of Search Console data into the extension store',
|
|
35
|
+
runAs: 'extension',
|
|
36
|
+
handler: runAnalyticsSyncTask,
|
|
37
|
+
}],
|
|
38
|
+
|
|
39
|
+
// Arms the chain for this workspace; a new generation retires the last one.
|
|
40
|
+
onStart: async (ctx) => { await armAnalyticsSync(ctx, { delaySeconds: 60 }).catch(() => undefined) },
|
|
41
|
+
|
|
42
|
+
// An operator just chose the Google connection or the Search Console
|
|
43
|
+
// property: sync now rather than leaving the Analytics page reciting a
|
|
44
|
+
// failure the save already fixed (the user, 2026-09-20). Enqueued, so the
|
|
45
|
+
// Save button does not wait on Google.
|
|
46
|
+
onSettingsChange: async (ctx, { changed }) => { await syncAfterSettingsChange(ctx, changed) },
|
|
47
|
+
})
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { defineExtension, settings } from "@tangen/cms";
|
|
2
|
+
|
|
3
|
+
export default defineExtension({
|
|
4
|
+
id: "seo",
|
|
5
|
+
name: "SEO",
|
|
6
|
+
version: "2.1.1",
|
|
7
|
+
// Provenance, shown on Settings > Extensions.
|
|
8
|
+
author: "Tangen",
|
|
9
|
+
homepage: "https://tangen.app",
|
|
10
|
+
license: "MIT",
|
|
11
|
+
contract: 1,
|
|
12
|
+
description:
|
|
13
|
+
"Production SEO for a Tangen site: metadata with deterministic precedence, canonical URLs, robots directives, sitemaps, robots.txt, redirects and structured data - resolved for the website, never guessed.",
|
|
14
|
+
versionedData: true,
|
|
15
|
+
server: "./src/server.ts",
|
|
16
|
+
|
|
17
|
+
// Grants (Extension Kernel, Block E): what the server entry is allowed to
|
|
18
|
+
// touch, each with its reason. `content:read:draft` is deliberate: the
|
|
19
|
+
// resolver reads an object at any status and answers only when it is
|
|
20
|
+
// published, and the member preview reads drafts behind `seo:read`.
|
|
21
|
+
grants: {
|
|
22
|
+
required: [
|
|
23
|
+
{ grant: "store:own", reason: "Keeps metadata, settings, redirects, schema maps and artifact versions in the extension store." },
|
|
24
|
+
{ grant: "routes:public", reason: "Serves meta, robots.txt, llms.txt, sitemaps and redirects to the website." },
|
|
25
|
+
{ grant: "routes:key", reason: "Serves the SDK resolver to a workspace API key." },
|
|
26
|
+
{ grant: "routes:member", reason: "Serves the SEO Manager its settings, redirects, schemas, preview and sitemap report." },
|
|
27
|
+
{ grant: "hooks:content", reason: "Stores the panel's metadata with the object and retires cached artifacts when content publishes." },
|
|
28
|
+
{ grant: "content:read", reason: "Reads published objects for meta, sitemaps and llms.txt." },
|
|
29
|
+
{ grant: "content:read:draft", reason: "Reads an object at any status to answer only when published, and drafts for the member preview." },
|
|
30
|
+
{ grant: "jobs:enqueue", reason: "Runs the daily Search Console sync as a platform task." },
|
|
31
|
+
],
|
|
32
|
+
// S3: share-image alt text through a vision provider. Both optional: without them the panel's field is hand-written.
|
|
33
|
+
optional: [
|
|
34
|
+
{ grant: "ai:vision", reason: "Describes an object's share image to write its alt text (og:image:alt)." },
|
|
35
|
+
{ grant: "media:read", reason: "Reads the share image's bytes to hand them to the vision provider." },
|
|
36
|
+
{ grant: "connections:google.search-console.read", reason: "Reads clicks, impressions and positions from the Google connection an operator chose. Read-only, and only that one capability of Google." },
|
|
37
|
+
{ grant: "ai:generate", reason: "Proposes a page's SEO title, description and answer summary with your AI provider. The proposal is a draft for review." },
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* F2: Search Console is READ through the Google connector pack now, not
|
|
43
|
+
* through a connector SEO offers itself. Optional on purpose - sitemaps,
|
|
44
|
+
* redirects, titles and the panel are the bulk of SEO and need no Google at
|
|
45
|
+
* all - so a project without the pack keeps every one of them and the
|
|
46
|
+
* Analytics surfaces say what is missing.
|
|
47
|
+
*/
|
|
48
|
+
requires: {
|
|
49
|
+
optional: { services: ["google.search-console@^1"] },
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
// V3: which Search Console connection and property the manager reads. On
|
|
53
|
+
// the core settings form (Settings > Extensions > SEO); the site-level SEO
|
|
54
|
+
// settings (templates, social, robots) stay on the extension's own store.
|
|
55
|
+
settings: {
|
|
56
|
+
searchConsole: settings.connection("google").optional().label("Search Console connection").describe("Which Google account to read Search Console with. The picker says what to do when there is none.").requires("connections:google.search-console.read"),
|
|
57
|
+
// F2: PICK the property, do not type it. Search Console spells a property
|
|
58
|
+
// exactly (`sc-domain:acme.com` and `https://www.acme.com/` are different
|
|
59
|
+
// things), and a typo reads as "no data" rather than as a mistake. The
|
|
60
|
+
// pack declares `property` as a resource of the Google connector, so the
|
|
61
|
+
// platform lists the real ones through the connection chosen above -
|
|
62
|
+
// including the ones this account may see but not read, marked
|
|
63
|
+
// unselectable with the reason.
|
|
64
|
+
searchConsoleProperty: settings.resource("google", "property").dependsOn("searchConsole").optional().label("Search Console property").describe("The property to read. Listed from the connection above; an unverified one is shown but cannot be chosen."),
|
|
65
|
+
analyticsRetentionDays: settings.number().min(1).max(480).default(90).label("Analytics retention (days)").describe("How many days of daily Search Console aggregates to keep. Daily totals only; no raw analytics events are stored."),
|
|
66
|
+
analyticsRowLimit: settings.number().min(1).max(1000).default(200).label("Rows per day").describe("How many top pages and top queries to keep for each day."),
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
// What a workspace API KEY may do with this extension (the key editor shows
|
|
70
|
+
// this section while SEO is active). The site reads: resolved metadata for a
|
|
71
|
+
// page, and the whole redirect map.
|
|
72
|
+
apiKeyScope: {
|
|
73
|
+
label: "SEO",
|
|
74
|
+
description: "Resolved metadata and the redirect map a site reads",
|
|
75
|
+
actions: [{ id: "read", label: "Read SEO and redirects" }],
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
capabilities: [
|
|
79
|
+
{ id: "seo:read", label: "View SEO settings and redirects" },
|
|
80
|
+
{
|
|
81
|
+
id: "seo:write",
|
|
82
|
+
label: "Manage SEO redirects",
|
|
83
|
+
description:
|
|
84
|
+
"Create, edit and delete redirects. A redirect changes where a visitor lands - grant only to members you trust with the site's routing.",
|
|
85
|
+
implies: ["seo:read"],
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
|
|
89
|
+
// V8: the workspace dashboard reads the LAST stored health report; it never
|
|
90
|
+
// triggers a scan, so opening the dashboard costs one store read.
|
|
91
|
+
dashboardWidgets: [{
|
|
92
|
+
id: "seo-health",
|
|
93
|
+
title: "SEO health",
|
|
94
|
+
description: "How the published pages stand, from the last check.",
|
|
95
|
+
icon: "IconSearch",
|
|
96
|
+
type: "stat-group",
|
|
97
|
+
dataPath: "health-widget",
|
|
98
|
+
href: "extensions/seo",
|
|
99
|
+
capability: "seo:read",
|
|
100
|
+
}],
|
|
101
|
+
|
|
102
|
+
pages: [
|
|
103
|
+
{
|
|
104
|
+
// The SEO Manager: one sidebar entry (the Dashboard); inside it the
|
|
105
|
+
// sidebar becomes the manager's own menu (plan-seo-manager-v2 V0), one
|
|
106
|
+
// page per child path below. A v1 `?tab=` link is sent to its page.
|
|
107
|
+
path: "",
|
|
108
|
+
component: "SeoManagerPage",
|
|
109
|
+
nav: { label: "SEO Manager", icon: "IconSearch", section: "extensions", order: 40 },
|
|
110
|
+
access: { capability: "seo:read" },
|
|
111
|
+
},
|
|
112
|
+
{ path: "analytics", component: "SeoAnalyticsPage", access: { capability: "seo:read" } },
|
|
113
|
+
{ path: "titles", component: "SeoTitlesPage", access: { capability: "seo:read" } },
|
|
114
|
+
{ path: "schema", component: "SeoSchemaPage", access: { capability: "seo:read" } },
|
|
115
|
+
{ path: "sitemap", component: "SeoSitemapPage", access: { capability: "seo:read" } },
|
|
116
|
+
{ path: "redirects", component: "SeoRedirectsPage", access: { capability: "seo:read" } },
|
|
117
|
+
{ path: "not-found", component: "SeoNotFoundPage", access: { capability: "seo:read" } },
|
|
118
|
+
{ path: "ai-geo", component: "SeoAiGeoPage", access: { capability: "seo:read" } },
|
|
119
|
+
{ path: "tools", component: "SeoToolsPage", access: { capability: "seo:read" } },
|
|
120
|
+
{ path: "settings", component: "SeoSettingsPage", access: { capability: "seo:read" } },
|
|
121
|
+
],
|
|
122
|
+
|
|
123
|
+
sidebarPanels: [
|
|
124
|
+
{
|
|
125
|
+
id: "seo",
|
|
126
|
+
label: "SEO",
|
|
127
|
+
icon: "IconSearch",
|
|
128
|
+
component: "SeoPanel",
|
|
129
|
+
ai: {
|
|
130
|
+
// capability only - enable per workspace via config: options: { ai: true }
|
|
131
|
+
instructions:
|
|
132
|
+
"Write search-optimised metadata only - never invent facts not present in the content. seoTitle max 60 characters. metaDescription 120-160 characters, plain text, no quotes or emoji.",
|
|
133
|
+
fields: [
|
|
134
|
+
{ name: "focusKeyword", label: "Focus keyword", fieldType: "text", config: { ai: { hint: "The single search phrase this content should rank for." } } },
|
|
135
|
+
{ name: "seoTitle", label: "SEO title", fieldType: "text", config: { ai: { hint: "Search-result title, max 60 characters, include the focus keyword." } } },
|
|
136
|
+
{ name: "metaDescription", label: "Meta description", fieldType: "textarea", config: { ai: { hint: "Compelling search snippet, 120-160 characters, plain text." } } },
|
|
137
|
+
],
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// NOT declared, deliberately:
|
|
144
|
+
//
|
|
145
|
+
// - `fieldTypes: ['seo-metadata']`. It was in the manifest and implemented
|
|
146
|
+
// NOWHERE - no editor rendered it and no validator checked it, so a site
|
|
147
|
+
// developer could select a field that did not work (Phase 9A finding 9A-1).
|
|
148
|
+
// SEO metadata belongs to the panel, which is where it always actually was.
|
|
149
|
+
// - `migrations` and `customTables`. SEO owns redirects, its artifact version
|
|
150
|
+
// and every object's metadata, and all of it lives in the platform's own
|
|
151
|
+
// extension store - already workspace and environment scoped, already
|
|
152
|
+
// indexed for the single-row read a redirect lookup performs. A dedicated
|
|
153
|
+
// table would have been a second, weaker copy of that index.
|
package/README.md
DELETED