@ingram-tech/nk-seo 0.8.0 → 0.8.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 +7 -4
- package/src/alternates.ts +166 -0
- package/src/components.ts +4 -0
- package/src/hreflang.tsx +70 -0
- package/src/index.ts +92 -0
- package/src/json-ld.tsx +35 -0
- package/src/metadata.ts +203 -0
- package/src/og.tsx +155 -0
- package/src/routes.ts +170 -0
- package/src/schema.ts +980 -0
- package/src/url.ts +23 -0
- package/src/verify.ts +195 -0
package/src/og.tsx
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { ImageResponse } from "next/og";
|
|
2
|
+
|
|
3
|
+
type ImageResponseOptions = NonNullable<ConstructorParameters<typeof ImageResponse>[1]>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A branded Open Graph / Twitter card built with `next/og`. Lives on its own
|
|
7
|
+
* entry (`@ingram-tech/nk-seo/og`) because it imports `next/og` (satori + the
|
|
8
|
+
* resvg wasm) — the package root and `/components` stay free of it.
|
|
9
|
+
*
|
|
10
|
+
* Satori gotcha this template encodes so you don't have to: **every element
|
|
11
|
+
* with more than one child must set `display: "flex"`**, and you may not mix a
|
|
12
|
+
* text node and an element as siblings. The layout below keeps each region a
|
|
13
|
+
* single text node, so titles stay plain strings and the accent is carried by
|
|
14
|
+
* the mark — never by a coloured `<span>` inside the headline.
|
|
15
|
+
*/
|
|
16
|
+
export interface OgImageOptions {
|
|
17
|
+
/** The headline. Kept a single text node (no inline markup) for Satori. */
|
|
18
|
+
title: string;
|
|
19
|
+
/** Secondary line under the title. */
|
|
20
|
+
subtitle?: string;
|
|
21
|
+
/** Small label above the title (e.g. a section or category). */
|
|
22
|
+
eyebrow?: string;
|
|
23
|
+
/** Bottom-left footer, e.g. the bare domain. */
|
|
24
|
+
footer?: string;
|
|
25
|
+
/** Brand name shown next to the mark, top-left. */
|
|
26
|
+
wordmark?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Logo image rendered as the top-left mark instead of the accent square.
|
|
29
|
+
* Absolute URL or data URI (Satori fetches it at render time).
|
|
30
|
+
*/
|
|
31
|
+
logo?: string;
|
|
32
|
+
/** Accent colour — the mark, and the eyebrow if present. Default indigo. */
|
|
33
|
+
accent?: string;
|
|
34
|
+
background?: string;
|
|
35
|
+
/** Primary text colour. */
|
|
36
|
+
ink?: string;
|
|
37
|
+
/** Secondary text colour (subtitle, footer). */
|
|
38
|
+
muted?: string;
|
|
39
|
+
size?: { width: number; height: number };
|
|
40
|
+
/**
|
|
41
|
+
* Custom fonts, passed through to `ImageResponse` (Satori accepts raw
|
|
42
|
+
* TTF/OTF/WOFF data). Pair with `fontFamily` to actually use them.
|
|
43
|
+
*/
|
|
44
|
+
fonts?: ImageResponseOptions["fonts"];
|
|
45
|
+
/** Font family for the whole card. Default "sans-serif". */
|
|
46
|
+
fontFamily?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const DEFAULTS = {
|
|
50
|
+
accent: "#565ac9",
|
|
51
|
+
background: "#ffffff",
|
|
52
|
+
ink: "#171717",
|
|
53
|
+
muted: "#8a8a8a",
|
|
54
|
+
size: { width: 1200, height: 630 },
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns an `ImageResponse` for use as the body of an `opengraph-image` /
|
|
59
|
+
* `twitter-image` route.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* // app/opengraph-image.tsx
|
|
63
|
+
* import { ogImageResponse } from "@ingram-tech/nk-seo/og";
|
|
64
|
+
* export const size = { width: 1200, height: 630 };
|
|
65
|
+
* export const contentType = "image/png";
|
|
66
|
+
* export default () =>
|
|
67
|
+
* ogImageResponse({
|
|
68
|
+
* title: "Ship faster with Acme",
|
|
69
|
+
* subtitle: "The all-in-one platform for modern teams.",
|
|
70
|
+
* wordmark: "Acme",
|
|
71
|
+
* footer: "example.com",
|
|
72
|
+
* });
|
|
73
|
+
*/
|
|
74
|
+
export function ogImageResponse(options: OgImageOptions): ImageResponse {
|
|
75
|
+
const accent = options.accent ?? DEFAULTS.accent;
|
|
76
|
+
const background = options.background ?? DEFAULTS.background;
|
|
77
|
+
const ink = options.ink ?? DEFAULTS.ink;
|
|
78
|
+
const muted = options.muted ?? DEFAULTS.muted;
|
|
79
|
+
const size = options.size ?? DEFAULTS.size;
|
|
80
|
+
|
|
81
|
+
return new ImageResponse(
|
|
82
|
+
<div
|
|
83
|
+
style={{
|
|
84
|
+
width: "100%",
|
|
85
|
+
height: "100%",
|
|
86
|
+
display: "flex",
|
|
87
|
+
flexDirection: "column",
|
|
88
|
+
justifyContent: "space-between",
|
|
89
|
+
background,
|
|
90
|
+
padding: "80px",
|
|
91
|
+
fontFamily: options.fontFamily ?? "sans-serif",
|
|
92
|
+
}}
|
|
93
|
+
>
|
|
94
|
+
<div style={{ display: "flex", alignItems: "center", gap: "20px" }}>
|
|
95
|
+
{options.logo ? (
|
|
96
|
+
<img src={options.logo} width={44} height={44} alt="" />
|
|
97
|
+
) : (
|
|
98
|
+
<div
|
|
99
|
+
style={{
|
|
100
|
+
width: "44px",
|
|
101
|
+
height: "44px",
|
|
102
|
+
borderRadius: "12px",
|
|
103
|
+
background: accent,
|
|
104
|
+
}}
|
|
105
|
+
/>
|
|
106
|
+
)}
|
|
107
|
+
{options.wordmark ? (
|
|
108
|
+
<div style={{ fontSize: "40px", fontWeight: 600, color: ink }}>
|
|
109
|
+
{options.wordmark}
|
|
110
|
+
</div>
|
|
111
|
+
) : null}
|
|
112
|
+
</div>
|
|
113
|
+
|
|
114
|
+
<div style={{ display: "flex", flexDirection: "column", gap: "24px" }}>
|
|
115
|
+
{options.eyebrow ? (
|
|
116
|
+
<div
|
|
117
|
+
style={{
|
|
118
|
+
fontSize: "28px",
|
|
119
|
+
fontWeight: 600,
|
|
120
|
+
letterSpacing: "0.04em",
|
|
121
|
+
textTransform: "uppercase",
|
|
122
|
+
color: accent,
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
{options.eyebrow}
|
|
126
|
+
</div>
|
|
127
|
+
) : null}
|
|
128
|
+
<div
|
|
129
|
+
style={{
|
|
130
|
+
fontSize: "76px",
|
|
131
|
+
fontWeight: 700,
|
|
132
|
+
lineHeight: 1.05,
|
|
133
|
+
letterSpacing: "-0.02em",
|
|
134
|
+
color: ink,
|
|
135
|
+
maxWidth: "1000px",
|
|
136
|
+
}}
|
|
137
|
+
>
|
|
138
|
+
{options.title}
|
|
139
|
+
</div>
|
|
140
|
+
{options.subtitle ? (
|
|
141
|
+
<div style={{ fontSize: "34px", color: muted, maxWidth: "900px" }}>
|
|
142
|
+
{options.subtitle}
|
|
143
|
+
</div>
|
|
144
|
+
) : null}
|
|
145
|
+
</div>
|
|
146
|
+
|
|
147
|
+
{options.footer ? (
|
|
148
|
+
<div style={{ fontSize: "30px", color: muted }}>{options.footer}</div>
|
|
149
|
+
) : (
|
|
150
|
+
<div style={{ display: "flex" }} />
|
|
151
|
+
)}
|
|
152
|
+
</div>,
|
|
153
|
+
{ ...size, ...(options.fonts ? { fonts: options.fonts } : {}) },
|
|
154
|
+
);
|
|
155
|
+
}
|
package/src/routes.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { MetadataRoute } from "next";
|
|
2
|
+
import { absoluteUrl } from "./url.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Route-handler helpers for `app/sitemap.ts` and `app/robots.ts`. Pure — no
|
|
6
|
+
* React — so they live in the package root next to the metadata factory and can
|
|
7
|
+
* be imported by route handlers freely.
|
|
8
|
+
*
|
|
9
|
+
* Both resolve site-relative paths against one `baseUrl` (pass your deployment's
|
|
10
|
+
* own origin, e.g. `getServerUrl()`), so preview / sandbox deployments advertise
|
|
11
|
+
* themselves and never leak the production host.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
type ChangeFrequency = NonNullable<MetadataRoute.Sitemap[number]["changeFrequency"]>;
|
|
15
|
+
|
|
16
|
+
/** hreflang → URL map, keyed by the locale codes Next accepts. */
|
|
17
|
+
export type SitemapLanguages = NonNullable<
|
|
18
|
+
NonNullable<MetadataRoute.Sitemap[number]["alternates"]>["languages"]
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
/** A sitemap entry. A bare string is shorthand for `{ path }`. */
|
|
22
|
+
export interface SitemapRoute {
|
|
23
|
+
/** Site-relative path (e.g. "/pricing") or an absolute URL. */
|
|
24
|
+
path: string;
|
|
25
|
+
lastModified?: string | Date;
|
|
26
|
+
changeFrequency?: ChangeFrequency;
|
|
27
|
+
/** 0.0–1.0. Defaults to 1 for "/" and {@link SitemapConfig.defaultPriority} otherwise. */
|
|
28
|
+
priority?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Per-locale alternates of this route (hreflang → site-relative path or
|
|
31
|
+
* absolute URL), e.g. `{ en: "/about", fr: "/fr/about" }`. Pairs with
|
|
32
|
+
* `<HreflangLinks>` / `hreflangAlternates` on the page side.
|
|
33
|
+
*/
|
|
34
|
+
languages?: SitemapLanguages;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SitemapConfig {
|
|
38
|
+
/** Absolute site origin, e.g. "https://example.com". */
|
|
39
|
+
baseUrl: string;
|
|
40
|
+
/** The indexable routes. Strings or `SitemapRoute` objects may be mixed. */
|
|
41
|
+
routes: Array<string | SitemapRoute>;
|
|
42
|
+
/** Applied when an entry omits `lastModified`. */
|
|
43
|
+
lastModified?: string | Date;
|
|
44
|
+
/** Applied when an entry omits `changeFrequency`. */
|
|
45
|
+
defaultChangeFrequency?: ChangeFrequency;
|
|
46
|
+
/** Priority for every non-root entry that omits its own. Default 0.7. */
|
|
47
|
+
defaultPriority?: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const resolveLanguages = (
|
|
51
|
+
languages: SitemapLanguages,
|
|
52
|
+
baseUrl: string,
|
|
53
|
+
): SitemapLanguages =>
|
|
54
|
+
// Object.entries/fromEntries erase the hreflang key union; the keys are
|
|
55
|
+
// passed through unchanged (only values are resolved), so restoring it is safe.
|
|
56
|
+
Object.fromEntries(
|
|
57
|
+
Object.entries(languages).map(([locale, path]) => [
|
|
58
|
+
locale,
|
|
59
|
+
path === undefined ? path : absoluteUrl(path, baseUrl),
|
|
60
|
+
]),
|
|
61
|
+
) as SitemapLanguages;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Builds a `MetadataRoute.Sitemap` from a list of routes, resolving relative
|
|
65
|
+
* paths against `baseUrl`. `"/"` defaults to priority 1; every other route
|
|
66
|
+
* defaults to `defaultPriority` (0.7).
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* // app/sitemap.ts
|
|
70
|
+
* import { createSitemap } from "@ingram-tech/nk-seo";
|
|
71
|
+
* export default () =>
|
|
72
|
+
* createSitemap({
|
|
73
|
+
* baseUrl: getServerUrl(),
|
|
74
|
+
* routes: ["/", "/pricing", "/docs", "/faq"],
|
|
75
|
+
* });
|
|
76
|
+
*/
|
|
77
|
+
export function createSitemap(config: SitemapConfig): MetadataRoute.Sitemap {
|
|
78
|
+
const defaultPriority = config.defaultPriority ?? 0.7;
|
|
79
|
+
return config.routes.map((entry) => {
|
|
80
|
+
const route: SitemapRoute = typeof entry === "string" ? { path: entry } : entry;
|
|
81
|
+
const lastModified = route.lastModified ?? config.lastModified;
|
|
82
|
+
const changeFrequency = route.changeFrequency ?? config.defaultChangeFrequency;
|
|
83
|
+
const priority = route.priority ?? (route.path === "/" ? 1 : defaultPriority);
|
|
84
|
+
if (priority < 0 || priority > 1) {
|
|
85
|
+
throw new RangeError(
|
|
86
|
+
`createSitemap: priority must be between 0 and 1, got ${priority} for "${route.path}"`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
url: absoluteUrl(route.path, config.baseUrl),
|
|
91
|
+
...(lastModified ? { lastModified } : {}),
|
|
92
|
+
...(changeFrequency ? { changeFrequency } : {}),
|
|
93
|
+
priority,
|
|
94
|
+
...(route.languages
|
|
95
|
+
? {
|
|
96
|
+
alternates: {
|
|
97
|
+
languages: resolveLanguages(
|
|
98
|
+
route.languages,
|
|
99
|
+
config.baseUrl,
|
|
100
|
+
),
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
: {}),
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface RobotsConfig {
|
|
109
|
+
/** Absolute site origin, e.g. "https://example.com". */
|
|
110
|
+
baseUrl: string;
|
|
111
|
+
/**
|
|
112
|
+
* Whether this deployment is the canonical production host. When false, the
|
|
113
|
+
* whole site is disallowed so preview / sandbox / branch deployments never
|
|
114
|
+
* get indexed and dilute the real domain with duplicate content. Derive it
|
|
115
|
+
* from `VERCEL_ENV === "production"` (and/or a host check).
|
|
116
|
+
*/
|
|
117
|
+
isProduction: boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Prod-only disallow list (private routes, APIs, auth flows). Entries that
|
|
120
|
+
* would block `/_next` are rejected: crawlers must reach the JS/CSS under it
|
|
121
|
+
* to render pages, so disallowing it silently harms indexing.
|
|
122
|
+
*/
|
|
123
|
+
disallow?: string[];
|
|
124
|
+
/** Prod allow rule. Default "/". */
|
|
125
|
+
allow?: string | string[];
|
|
126
|
+
/** Sitemap path advertised in prod. Default "/sitemap.xml". Pass null to omit. */
|
|
127
|
+
sitemapPath?: string | null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Builds a `MetadataRoute.Robots`. On non-production hosts it returns a
|
|
132
|
+
* blanket `Disallow: /` — the single most-forgotten SEO safeguard on Vercel,
|
|
133
|
+
* where preview and branch URLs are otherwise crawlable and compete with the
|
|
134
|
+
* production domain.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* // app/robots.ts
|
|
138
|
+
* import { createRobots } from "@ingram-tech/nk-seo";
|
|
139
|
+
* export default () =>
|
|
140
|
+
* createRobots({
|
|
141
|
+
* baseUrl: getServerUrl(),
|
|
142
|
+
* isProduction: process.env.VERCEL_ENV === "production",
|
|
143
|
+
* disallow: ["/api/", "/internal/", "/login"],
|
|
144
|
+
* });
|
|
145
|
+
*/
|
|
146
|
+
export function createRobots(config: RobotsConfig): MetadataRoute.Robots {
|
|
147
|
+
if (!config.isProduction) {
|
|
148
|
+
return { rules: { userAgent: "*", disallow: "/" } };
|
|
149
|
+
}
|
|
150
|
+
const nextAsset = config.disallow?.find((path) =>
|
|
151
|
+
path.trim().replace(/^\//, "").startsWith("_next"),
|
|
152
|
+
);
|
|
153
|
+
if (nextAsset !== undefined) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
`createRobots: refusing to disallow "${nextAsset}". Blocking /_next stops ` +
|
|
156
|
+
"crawlers from fetching the JS/CSS they need to render your pages, which " +
|
|
157
|
+
"hurts indexing. Next.js does not block it by default and neither should you.",
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
const sitemapPath =
|
|
161
|
+
config.sitemapPath === undefined ? "/sitemap.xml" : config.sitemapPath;
|
|
162
|
+
return {
|
|
163
|
+
rules: {
|
|
164
|
+
userAgent: "*",
|
|
165
|
+
allow: config.allow ?? "/",
|
|
166
|
+
...(config.disallow?.length ? { disallow: config.disallow } : {}),
|
|
167
|
+
},
|
|
168
|
+
...(sitemapPath ? { sitemap: absoluteUrl(sitemapPath, config.baseUrl) } : {}),
|
|
169
|
+
};
|
|
170
|
+
}
|