@getvitops/astro 0.9.0 → 1.0.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.
- package/CHANGELOG.md +218 -0
- package/README.md +96 -2
- package/dist/index.d.mts +284 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +152 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -5
- package/src/components/Cards.astro +1 -1
- package/src/components/Head.astro +3 -0
- package/src/components/Seo.astro +68 -0
- package/src/seo.ts +307 -0
- package/virtual.d.ts +10 -0
package/src/seo.ts
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `<Seo />` resolution layer — site-level defaults merged with per-page props
|
|
3
|
+
* into the exact tags to emit.
|
|
4
|
+
*
|
|
5
|
+
* Kept separate from `Seo.astro` deliberately. The component this replaces
|
|
6
|
+
* (`SEO.astro`, deleted in c949cae) fused resolution with markup, which is why it
|
|
7
|
+
* was never unit-tested and why its title handling drifted: it computed a title
|
|
8
|
+
* for `og:title` but left `<title>` to the layout, so the two could disagree.
|
|
9
|
+
* Everything decidable lives here and is asserted in `seo.test.ts`; the component
|
|
10
|
+
* only renders what this returns.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Twitter card types, per <https://developer.x.com/en/docs/twitter-for-websites/cards>. */
|
|
14
|
+
export type SeoTwitterCard = 'summary' | 'summary_large_image' | 'app' | 'player';
|
|
15
|
+
|
|
16
|
+
/** Open Graph object types this component knows how to describe. */
|
|
17
|
+
export type SeoOgType = 'website' | 'article' | 'profile';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Site-level SEO defaults, passed to `getvitops({ seo })` and baked into the
|
|
21
|
+
* `virtual:getvitops/head` module. Per-page `<Seo />` props override these.
|
|
22
|
+
*
|
|
23
|
+
* The vocabulary mirrors the site-config schema's `seo` block
|
|
24
|
+
* (`@getvitops/generator`, `src/site.ts`) so a `SiteConfig` → these adapter would
|
|
25
|
+
* be a flat field map — but it is not imported from there. This is the
|
|
26
|
+
* integration's own option surface, and it must stay JSON-serialisable: it is
|
|
27
|
+
* `JSON.stringify`d into the virtual module, so no functions, no `URL`s, no dates.
|
|
28
|
+
*/
|
|
29
|
+
export interface GetvitopsSeoOptions {
|
|
30
|
+
/** `og:site_name`, and the `<title>` when a page passes none. */
|
|
31
|
+
siteName?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Page-title pattern; `%s` is the page's own title. e.g. `'%s · Acme'`.
|
|
34
|
+
*
|
|
35
|
+
* Skipped when a page's title already equals `siteName` — a homepage titled
|
|
36
|
+
* "Acme" emits `Acme`, not `Acme · Acme` — and never applied to `defaultTitle`.
|
|
37
|
+
*/
|
|
38
|
+
titleTemplate?: string;
|
|
39
|
+
/** `<title>` for pages that pass none. Used verbatim — the template is not applied. */
|
|
40
|
+
defaultTitle?: string;
|
|
41
|
+
/** `<meta name="description">` for pages that pass none. */
|
|
42
|
+
defaultDescription?: string;
|
|
43
|
+
/** Site-wide robots directive, e.g. `'index,follow,max-image-preview:large'`. */
|
|
44
|
+
robots?: string;
|
|
45
|
+
/** Google Search Console verification token. */
|
|
46
|
+
googleSiteVerification?: string;
|
|
47
|
+
/** Bing Webmaster Tools verification token. */
|
|
48
|
+
bingSiteVerification?: string;
|
|
49
|
+
openGraph?: {
|
|
50
|
+
/** Default `og:type` (default `'website'`). */
|
|
51
|
+
type?: SeoOgType;
|
|
52
|
+
/** `og:locale`. Written either way — `en-CA` and `en_CA` both emit `en_CA`. */
|
|
53
|
+
locale?: string;
|
|
54
|
+
/** Additional locales the same content exists in → `og:locale:alternate`. */
|
|
55
|
+
localeAlternates?: string[];
|
|
56
|
+
/** Fallback share image. Relative URLs resolve against `site`. */
|
|
57
|
+
image?: { url: string; alt?: string; width?: number; height?: number; type?: string };
|
|
58
|
+
};
|
|
59
|
+
twitter?: {
|
|
60
|
+
/** Card type when no image resolves (default `'summary'`). */
|
|
61
|
+
card?: SeoTwitterCard;
|
|
62
|
+
/** The site's own handle, `@acme`. */
|
|
63
|
+
site?: string;
|
|
64
|
+
/** Default author handle, `@ada`. */
|
|
65
|
+
creator?: string;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Per-page `<Seo />` props. Everything is optional; defaults fill the gaps. */
|
|
70
|
+
export interface SeoProps {
|
|
71
|
+
/** Page title, before `titleTemplate` is applied. */
|
|
72
|
+
title?: string;
|
|
73
|
+
description?: string;
|
|
74
|
+
/** Overrides the computed canonical. Relative values resolve against `site`. */
|
|
75
|
+
canonical?: string;
|
|
76
|
+
/** Overrides `og:site_name` and the title fallback. */
|
|
77
|
+
siteName?: string;
|
|
78
|
+
/** Share image for this page. Relative URLs resolve against `site`. */
|
|
79
|
+
image?: string;
|
|
80
|
+
imageAlt?: string;
|
|
81
|
+
imageWidth?: number;
|
|
82
|
+
imageHeight?: number;
|
|
83
|
+
/** MIME type of the share image, e.g. `'image/png'`. */
|
|
84
|
+
imageType?: string;
|
|
85
|
+
noindex?: boolean;
|
|
86
|
+
nofollow?: boolean;
|
|
87
|
+
noarchive?: boolean;
|
|
88
|
+
nocache?: boolean;
|
|
89
|
+
/** Extra robots directives, e.g. `'max-snippet:-1'`. Appended to the composed value. */
|
|
90
|
+
robotsExtras?: string;
|
|
91
|
+
/** Full robots override. Wins over the flags above and the site default. */
|
|
92
|
+
robots?: string;
|
|
93
|
+
/** `og:type` for this page (default: the site default, else `'website'`). */
|
|
94
|
+
type?: SeoOgType;
|
|
95
|
+
/** Emitted only when `type` is `'article'`. */
|
|
96
|
+
article?: {
|
|
97
|
+
publishedTime?: string;
|
|
98
|
+
modifiedTime?: string;
|
|
99
|
+
expirationTime?: string;
|
|
100
|
+
authors?: string[];
|
|
101
|
+
section?: string;
|
|
102
|
+
tags?: string[];
|
|
103
|
+
};
|
|
104
|
+
/** `og:locale` for this page. */
|
|
105
|
+
locale?: string;
|
|
106
|
+
/** Twitter author handle for this page, `@ada`. */
|
|
107
|
+
twitterCreator?: string;
|
|
108
|
+
/**
|
|
109
|
+
* Translations of *this* page, as `<link rel="alternate" hreflang>`.
|
|
110
|
+
*
|
|
111
|
+
* Explicit only, and it must include the current page. Nothing is inferred:
|
|
112
|
+
* the deleted component derived alternates from the site's locale list and
|
|
113
|
+
* pointed every one of them at that locale's homepage, so every page on the
|
|
114
|
+
* site claimed the homepage as its translation. Only the page knows its own
|
|
115
|
+
* translated URLs. Pass `hreflang: 'x-default'` for the fallback entry.
|
|
116
|
+
*/
|
|
117
|
+
alternates?: { hreflang: string; href: string }[];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** A `<meta property>` tag (Open Graph uses `property`, not `name`). */
|
|
121
|
+
export interface SeoPropertyTag {
|
|
122
|
+
property: string;
|
|
123
|
+
content: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** A `<meta name>` tag. */
|
|
127
|
+
export interface SeoNameTag {
|
|
128
|
+
name: string;
|
|
129
|
+
content: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Everything `<Seo />` renders, already decided. `null` means "emit nothing". */
|
|
133
|
+
export interface ResolvedSeo {
|
|
134
|
+
title: string | null;
|
|
135
|
+
description: string | null;
|
|
136
|
+
canonical: string | null;
|
|
137
|
+
robots: string | null;
|
|
138
|
+
openGraph: SeoPropertyTag[];
|
|
139
|
+
twitter: SeoNameTag[];
|
|
140
|
+
verification: SeoNameTag[];
|
|
141
|
+
alternates: { hreflang: string; href: string }[];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Render context supplied by the component from `Astro`. */
|
|
145
|
+
export interface SeoContext {
|
|
146
|
+
/**
|
|
147
|
+
* The deployed origin — `Astro.site`, or `config.site` via the virtual module.
|
|
148
|
+
*
|
|
149
|
+
* `undefined` when neither is set, and then every absolute-URL tag is dropped
|
|
150
|
+
* rather than being derived from the request. `astro-seo` falls back to
|
|
151
|
+
* `Astro.url` here; on a static build that is `http://localhost:4321`, which
|
|
152
|
+
* would bake a localhost canonical into production HTML.
|
|
153
|
+
*/
|
|
154
|
+
site?: URL | undefined;
|
|
155
|
+
/** `Astro.url.pathname` — already includes `base`. */
|
|
156
|
+
pathname: string;
|
|
157
|
+
/** `Astro.url.search`, if the canonical should keep the query string. */
|
|
158
|
+
search?: string;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** `en-CA` → `en_CA`; Open Graph wants the underscore form. */
|
|
162
|
+
const ogLocale = (locale: string) => locale.replace('-', '_');
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Absolute URLs pass through; relative ones resolve against `site`, or are
|
|
166
|
+
* dropped when there is no `site` to resolve them against. A relative `og:image`
|
|
167
|
+
* or canonical is worse than none — crawlers resolve it against whatever origin
|
|
168
|
+
* served the page.
|
|
169
|
+
*/
|
|
170
|
+
function absolute(url: string | undefined, site: URL | undefined): string | null {
|
|
171
|
+
if (!url) return null;
|
|
172
|
+
try {
|
|
173
|
+
return new URL(url).href;
|
|
174
|
+
} catch {
|
|
175
|
+
return site ? new URL(url, site).href : null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Composes the robots directive, or null when nothing meaningful was asked for. */
|
|
180
|
+
function resolveRobots(props: SeoProps, defaults: GetvitopsSeoOptions): string | null {
|
|
181
|
+
if (props.robots) return props.robots;
|
|
182
|
+
const { noindex, nofollow, noarchive, nocache, robotsExtras } = props;
|
|
183
|
+
if (noindex || nofollow || noarchive || nocache || robotsExtras) {
|
|
184
|
+
return [
|
|
185
|
+
noindex ? 'noindex' : 'index',
|
|
186
|
+
nofollow ? 'nofollow' : 'follow',
|
|
187
|
+
...(noarchive ? ['noarchive'] : []),
|
|
188
|
+
...(nocache ? ['nocache'] : []),
|
|
189
|
+
...(robotsExtras ? [robotsExtras] : []),
|
|
190
|
+
].join(', ');
|
|
191
|
+
}
|
|
192
|
+
// No explicit default either: emit nothing. `index, follow` is what crawlers
|
|
193
|
+
// already assume, so stating it adds bytes and no meaning.
|
|
194
|
+
return defaults.robots ?? null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Merges site defaults with page props into the final tag set.
|
|
199
|
+
*
|
|
200
|
+
* Pure — no `Astro`, no filesystem, no config lookup. Everything it needs comes
|
|
201
|
+
* through its three arguments, which is what makes it testable and what keeps the
|
|
202
|
+
* package free of the consumer-global coupling that sank the previous site model.
|
|
203
|
+
*/
|
|
204
|
+
export function resolveSeo(
|
|
205
|
+
defaults: GetvitopsSeoOptions,
|
|
206
|
+
props: SeoProps,
|
|
207
|
+
ctx: SeoContext,
|
|
208
|
+
): ResolvedSeo {
|
|
209
|
+
const { site } = ctx;
|
|
210
|
+
const siteName = props.siteName ?? defaults.siteName ?? null;
|
|
211
|
+
|
|
212
|
+
// The template applies to a page's own title only, and only when that title
|
|
213
|
+
// isn't already the site name. Two ways to get "Acme · Acme" otherwise: running
|
|
214
|
+
// it over `defaultTitle`, and a homepage whose own title happens to equal the
|
|
215
|
+
// site name — which is the common case, not an edge case.
|
|
216
|
+
const templated = props.title && defaults.titleTemplate && props.title !== siteName;
|
|
217
|
+
const title = props.title
|
|
218
|
+
? templated
|
|
219
|
+
? String(defaults.titleTemplate).replaceAll('%s', props.title)
|
|
220
|
+
: props.title
|
|
221
|
+
: (defaults.defaultTitle ?? siteName);
|
|
222
|
+
|
|
223
|
+
const description = props.description ?? defaults.defaultDescription ?? null;
|
|
224
|
+
|
|
225
|
+
const canonical = props.canonical
|
|
226
|
+
? absolute(props.canonical, site)
|
|
227
|
+
: site
|
|
228
|
+
? new URL(ctx.pathname + (ctx.search ?? ''), site).href
|
|
229
|
+
: null;
|
|
230
|
+
|
|
231
|
+
const image = absolute(props.image, site) ?? absolute(defaults.openGraph?.image?.url, site);
|
|
232
|
+
// Only inherit the default image's metadata when the page didn't bring its own
|
|
233
|
+
// image — otherwise a page's image would carry the default's alt text.
|
|
234
|
+
const usingDefaultImage = !props.image;
|
|
235
|
+
const defaultImage = defaults.openGraph?.image;
|
|
236
|
+
const imageAlt = props.imageAlt ?? (usingDefaultImage ? defaultImage?.alt : undefined);
|
|
237
|
+
const imageWidth = props.imageWidth ?? (usingDefaultImage ? defaultImage?.width : undefined);
|
|
238
|
+
const imageHeight = props.imageHeight ?? (usingDefaultImage ? defaultImage?.height : undefined);
|
|
239
|
+
const imageType = props.imageType ?? (usingDefaultImage ? defaultImage?.type : undefined);
|
|
240
|
+
|
|
241
|
+
const type = props.type ?? defaults.openGraph?.type ?? 'website';
|
|
242
|
+
const locale = props.locale ?? defaults.openGraph?.locale;
|
|
243
|
+
|
|
244
|
+
const openGraph: SeoPropertyTag[] = [];
|
|
245
|
+
const og = (property: string, content: string | number | null | undefined) => {
|
|
246
|
+
if (content !== null && content !== undefined && content !== '')
|
|
247
|
+
openGraph.push({ property, content: String(content) });
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
og('og:type', type);
|
|
251
|
+
og('og:title', title);
|
|
252
|
+
og('og:description', description);
|
|
253
|
+
og('og:url', canonical);
|
|
254
|
+
og('og:site_name', siteName);
|
|
255
|
+
og('og:locale', locale ? ogLocale(locale) : null);
|
|
256
|
+
for (const alt of defaults.openGraph?.localeAlternates ?? [])
|
|
257
|
+
og('og:locale:alternate', ogLocale(alt));
|
|
258
|
+
og('og:image', image);
|
|
259
|
+
if (image) {
|
|
260
|
+
og('og:image:alt', imageAlt);
|
|
261
|
+
og('og:image:width', imageWidth);
|
|
262
|
+
og('og:image:height', imageHeight);
|
|
263
|
+
og('og:image:type', imageType);
|
|
264
|
+
}
|
|
265
|
+
if (type === 'article' && props.article) {
|
|
266
|
+
const a = props.article;
|
|
267
|
+
og('article:published_time', a.publishedTime);
|
|
268
|
+
og('article:modified_time', a.modifiedTime);
|
|
269
|
+
og('article:expiration_time', a.expirationTime);
|
|
270
|
+
for (const author of a.authors ?? []) og('article:author', author);
|
|
271
|
+
og('article:section', a.section);
|
|
272
|
+
for (const tag of a.tags ?? []) og('article:tag', tag);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const twitter: SeoNameTag[] = [];
|
|
276
|
+
const tw = (name: string, content: string | null | undefined) => {
|
|
277
|
+
if (content) twitter.push({ name, content });
|
|
278
|
+
};
|
|
279
|
+
// A card with a large image is only honoured if there *is* an image.
|
|
280
|
+
tw('twitter:card', image ? 'summary_large_image' : (defaults.twitter?.card ?? 'summary'));
|
|
281
|
+
tw('twitter:site', defaults.twitter?.site);
|
|
282
|
+
tw('twitter:creator', props.twitterCreator ?? defaults.twitter?.creator);
|
|
283
|
+
tw('twitter:title', title);
|
|
284
|
+
tw('twitter:description', description);
|
|
285
|
+
tw('twitter:image', image);
|
|
286
|
+
tw('twitter:image:alt', image ? imageAlt : null);
|
|
287
|
+
|
|
288
|
+
const verification: SeoNameTag[] = [];
|
|
289
|
+
if (defaults.googleSiteVerification)
|
|
290
|
+
verification.push({
|
|
291
|
+
name: 'google-site-verification',
|
|
292
|
+
content: defaults.googleSiteVerification,
|
|
293
|
+
});
|
|
294
|
+
if (defaults.bingSiteVerification)
|
|
295
|
+
verification.push({ name: 'msvalidate.01', content: defaults.bingSiteVerification });
|
|
296
|
+
|
|
297
|
+
return {
|
|
298
|
+
title,
|
|
299
|
+
description,
|
|
300
|
+
canonical,
|
|
301
|
+
robots: resolveRobots(props, defaults),
|
|
302
|
+
openGraph,
|
|
303
|
+
twitter,
|
|
304
|
+
verification,
|
|
305
|
+
alternates: props.alternates ?? [],
|
|
306
|
+
};
|
|
307
|
+
}
|
package/virtual.d.ts
CHANGED
|
@@ -6,12 +6,22 @@ declare module 'virtual:getvitops/head' {
|
|
|
6
6
|
type?: string;
|
|
7
7
|
sizes?: string;
|
|
8
8
|
}
|
|
9
|
+
// Keep these keys in step with `HeadData` in src/integration.ts — the two are
|
|
10
|
+
// hand-duplicated (an ambient `declare module` in a shipped .d.ts cannot import
|
|
11
|
+
// from src), and `head.test.ts` asserts the key sets match.
|
|
9
12
|
interface HeadData {
|
|
10
13
|
favicons: boolean;
|
|
11
14
|
faviconLinks: FaviconLink[];
|
|
12
15
|
themeColor: string | null;
|
|
13
16
|
webComponents: boolean;
|
|
14
17
|
wcBase: string;
|
|
18
|
+
editor: boolean;
|
|
19
|
+
sitemap: string | null;
|
|
20
|
+
site: string | null;
|
|
21
|
+
// Structurally `GetvitopsSeoOptions` (src/seo.ts). Left loose here because
|
|
22
|
+
// this file can't import from src, and <Seo /> re-types it on the way into
|
|
23
|
+
// `resolveSeo`, which is where the real contract is enforced.
|
|
24
|
+
seo: Record<string, unknown>;
|
|
15
25
|
}
|
|
16
26
|
const data: HeadData;
|
|
17
27
|
export default data;
|