sanity-plugin-seofields 1.2.3 → 1.2.5

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/dist/next.js ADDED
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: !0 });
3
+ var jsxRuntime = require("react/jsx-runtime"), r = require("react");
4
+ function _interopDefaultCompat(e) {
5
+ return e && typeof e == "object" && "default" in e ? e : { default: e };
6
+ }
7
+ var r__default = /* @__PURE__ */ _interopDefaultCompat(r);
8
+ const VALID_OG_TYPES = [
9
+ "website",
10
+ "article",
11
+ "profile",
12
+ "book",
13
+ "music",
14
+ "video",
15
+ "product"
16
+ ];
17
+ function sanitizeOGType(value) {
18
+ return value && VALID_OG_TYPES.includes(value) ? value : "website";
19
+ }
20
+ const VALID_TWITTER_CARDS = ["summary", "summary_large_image", "app", "player"];
21
+ function sanitizeTwitterCard(value) {
22
+ return value && VALID_TWITTER_CARDS.includes(value) ? value : "summary_large_image";
23
+ }
24
+ function buildSeoMeta(options) {
25
+ const { seo, baseUrl = "", path = "", defaults = {}, imageUrlResolver } = options, normalizedBase = baseUrl.replace(/\/+$/, ""), normalizedPath = path.replace(/^\/+/, ""), fullUrl = [normalizedBase, normalizedPath].filter(Boolean).join("/");
26
+ let ogImageURL = defaults.ogImage || "";
27
+ seo?.openGraph?.imageType === "url" && seo.openGraph.imageUrl ? ogImageURL = seo.openGraph.imageUrl : seo?.openGraph?.image && imageUrlResolver && (ogImageURL = imageUrlResolver(seo.openGraph.image) || ogImageURL);
28
+ let twitterImageURL = ogImageURL;
29
+ seo?.twitter?.imageType === "url" && seo.twitter.imageUrl ? twitterImageURL = seo.twitter.imageUrl : seo?.twitter?.image && imageUrlResolver && (twitterImageURL = imageUrlResolver(seo.twitter.image) || twitterImageURL);
30
+ const other = {};
31
+ if (Array.isArray(seo?.metaAttributes))
32
+ for (const attr of seo.metaAttributes)
33
+ attr.key && attr.value && (other[attr.key] = attr.value);
34
+ const ogUrl = seo?.openGraph?.url || fullUrl;
35
+ return {
36
+ title: seo?.title ?? defaults.title ?? null,
37
+ description: seo?.description ?? defaults.description ?? null,
38
+ keywords: seo?.keywords?.length ? seo.keywords : void 0,
39
+ robots: {
40
+ index: !seo?.robots?.noIndex,
41
+ follow: !seo?.robots?.noFollow,
42
+ googleBot: {
43
+ index: !seo?.robots?.noIndex,
44
+ follow: !seo?.robots?.noFollow
45
+ }
46
+ },
47
+ openGraph: {
48
+ type: sanitizeOGType(seo?.openGraph?.type ?? void 0),
49
+ url: ogUrl || void 0,
50
+ title: seo?.openGraph?.title ?? defaults.title,
51
+ description: seo?.openGraph?.description ?? defaults.description,
52
+ siteName: seo?.openGraph?.siteName ?? defaults.siteName,
53
+ images: ogImageURL ? [{ url: ogImageURL }] : []
54
+ },
55
+ twitter: {
56
+ card: sanitizeTwitterCard(seo?.twitter?.card ?? void 0),
57
+ site: seo?.twitter?.site ?? defaults.twitterSite,
58
+ creator: seo?.twitter?.creator ?? defaults.twitterCreator,
59
+ title: seo?.twitter?.title ?? defaults.title,
60
+ description: seo?.twitter?.description ?? defaults.description,
61
+ images: twitterImageURL ? [twitterImageURL] : []
62
+ },
63
+ alternates: {
64
+ canonical: fullUrl || void 0
65
+ },
66
+ ...Object.keys(other).length > 0 ? { other } : {}
67
+ };
68
+ }
69
+ function SeoMetaTags({ data, baseUrl, path, defaults, imageUrlResolver }) {
70
+ const meta = buildSeoMeta({ seo: data, baseUrl, path, defaults, imageUrlResolver }), robotsContent = [
71
+ meta.robots?.index === !1 ? "noindex" : "index",
72
+ meta.robots?.follow === !1 ? "nofollow" : "follow"
73
+ ].join(", ");
74
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
75
+ meta.title && /* @__PURE__ */ jsxRuntime.jsx("title", { children: meta.title }),
76
+ meta.description && /* @__PURE__ */ jsxRuntime.jsx("meta", { name: "description", content: meta.description }),
77
+ meta.keywords?.length ? /* @__PURE__ */ jsxRuntime.jsx("meta", { name: "keywords", content: meta.keywords.join(", ") }) : null,
78
+ /* @__PURE__ */ jsxRuntime.jsx("meta", { name: "robots", content: robotsContent }),
79
+ /* @__PURE__ */ jsxRuntime.jsx("meta", { name: "googlebot", content: robotsContent }),
80
+ meta.openGraph?.type && /* @__PURE__ */ jsxRuntime.jsx("meta", { property: "og:type", content: meta.openGraph.type }),
81
+ meta.openGraph?.url && /* @__PURE__ */ jsxRuntime.jsx("meta", { property: "og:url", content: meta.openGraph.url }),
82
+ meta.openGraph?.title && /* @__PURE__ */ jsxRuntime.jsx("meta", { property: "og:title", content: meta.openGraph.title }),
83
+ meta.openGraph?.description && /* @__PURE__ */ jsxRuntime.jsx("meta", { property: "og:description", content: meta.openGraph.description }),
84
+ meta.openGraph?.siteName && /* @__PURE__ */ jsxRuntime.jsx("meta", { property: "og:site_name", content: meta.openGraph.siteName }),
85
+ meta.openGraph?.images?.map((img, i) => /* @__PURE__ */ jsxRuntime.jsxs(r__default.default.Fragment, { children: [
86
+ /* @__PURE__ */ jsxRuntime.jsx("meta", { property: "og:image", content: img.url }),
87
+ img.width && /* @__PURE__ */ jsxRuntime.jsx("meta", { property: "og:image:width", content: String(img.width) }),
88
+ img.height && /* @__PURE__ */ jsxRuntime.jsx("meta", { property: "og:image:height", content: String(img.height) }),
89
+ img.alt && /* @__PURE__ */ jsxRuntime.jsx("meta", { property: "og:image:alt", content: img.alt })
90
+ ] }, `og-img-${i}`)),
91
+ meta.twitter?.card && /* @__PURE__ */ jsxRuntime.jsx("meta", { name: "twitter:card", content: meta.twitter.card }),
92
+ meta.twitter?.site && /* @__PURE__ */ jsxRuntime.jsx("meta", { name: "twitter:site", content: meta.twitter.site }),
93
+ meta.twitter?.creator && /* @__PURE__ */ jsxRuntime.jsx("meta", { name: "twitter:creator", content: meta.twitter.creator }),
94
+ meta.twitter?.title && /* @__PURE__ */ jsxRuntime.jsx("meta", { name: "twitter:title", content: meta.twitter.title }),
95
+ meta.twitter?.description && /* @__PURE__ */ jsxRuntime.jsx("meta", { name: "twitter:description", content: meta.twitter.description }),
96
+ meta.twitter?.images?.map((url, i) => /* @__PURE__ */ jsxRuntime.jsx("meta", { name: "twitter:image", content: url }, `tw-img-${i}`)),
97
+ meta.other && Object.entries(meta.other).map(([name, content]) => /* @__PURE__ */ jsxRuntime.jsx("meta", { name, content }, `custom-${name}`)),
98
+ meta.alternates?.canonical && /* @__PURE__ */ jsxRuntime.jsx("link", { rel: "canonical", href: meta.alternates.canonical })
99
+ ] });
100
+ }
101
+ exports.SeoMetaTags = SeoMetaTags;
102
+ exports.buildSeoMeta = buildSeoMeta;
103
+ exports.sanitizeOGType = sanitizeOGType;
104
+ exports.sanitizeTwitterCard = sanitizeTwitterCard;
105
+ //# sourceMappingURL=next.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next.js","sources":["../src/helpers/seoMeta.ts","../src/helpers/SeoMetaTags.tsx"],"sourcesContent":["/**\n * Headless CMS integration helpers for sanity-plugin-seofields\n *\n * Provides framework-agnostic SEO metadata utilities for use with:\n * - Next.js App Router → buildSeoMeta() inside generateMetadata()\n * - Next.js Pages Router → <SeoMetaTags> inside Next.js <Head>\n * - Nuxt / Remix / any SSR → <SeoMetaTags> inside your <head> slot\n */\n\nimport type {SanityImage, SanityImageWithAlt, SeoFields} from '../types'\n\n// ─── Types ────────────────────────────────────────────────────────────────────\n\n/** Structured metadata returned by buildSeoMeta(). Compatible with Next.js Metadata (App Router). */\nexport interface SeoMetadata {\n title?: string | null\n description?: string | null\n keywords?: string[]\n robots?: {\n index?: boolean\n follow?: boolean\n googleBot?: {\n index?: boolean\n follow?: boolean\n }\n }\n openGraph?: {\n type?: string\n url?: string\n title?: string\n description?: string\n siteName?: string\n images?: Array<{url: string; width?: number; height?: number; alt?: string}>\n }\n twitter?: {\n card?: string\n site?: string\n creator?: string\n title?: string\n description?: string\n images?: string[]\n }\n alternates?: {\n canonical?: string\n }\n /** Any custom meta attributes from seo.metaAttributes */\n other?: Record<string, string>\n}\n\n/** Default values used when SEO fields are missing. */\nexport interface SeoMetaDefaults {\n title?: string\n description?: string\n siteName?: string\n twitterSite?: string\n twitterCreator?: string\n /** Fallback image URL when no OG / Twitter image is set. */\n ogImage?: string\n}\n\n/**\n * Permissive image shape accepted by buildSeoMeta — compatible with both the\n * plugin's SanityImage and Sanity's code-generated image type (where `asset`\n * and `alt` are optional).\n */\ninterface SeoImageInput {\n _type?: string\n asset?: {_ref: string; _type: string; _weak?: boolean; [key: string]: unknown}\n hotspot?: unknown\n crop?: unknown\n alt?: string\n}\n\n/**\n * Input-compatible variant of SeoFields. Structurally matches Sanity's\n * code-generated types (where `asset`, `alt`, `key`, and `type` are all\n * optional), so you can pass `data.seo` from a sanityFetch result directly\n * without any `as any` or manual casting.\n */\nexport interface SeoFieldsInput {\n _type?: string\n robots?: {noIndex?: boolean | null; noFollow?: boolean | null} | null\n title?: string | null\n description?: string | null\n metaImage?: SeoImageInput | null\n metaAttributes?: Array<{_key?: string; key?: string; value?: string; type?: string}> | null\n keywords?: string[] | null\n canonicalUrl?: string | null\n openGraph?: {\n _type?: string\n url?: string | null\n title?: string | null\n description?: string | null\n siteName?: string | null\n type?: string | null\n imageType?: string | null\n image?: SeoImageInput | null\n imageUrl?: string | null\n } | null\n twitter?: {\n _type?: string\n card?: string | null\n site?: string | null\n creator?: string | null\n title?: string | null\n description?: string | null\n imageType?: string | null\n image?: SeoImageInput | null\n imageUrl?: string | null\n } | null\n}\n\n/** Options accepted by buildSeoMeta(). */\nexport interface BuildSeoMetaOptions {\n /**\n * The raw SEO object from Sanity (_type excluded or included — both work).\n * Pass `null` or `undefined` to fall back entirely to `defaults`.\n *\n * Accepts both the strict plugin `SeoFields` type and Sanity's code-generated\n * type (which has all nested fields optional) without any `as any` cast.\n */\n seo?: SeoFieldsInput | null\n\n /**\n * The base URL of your site, e.g. \"https://example.com\".\n * Used for canonical URL and OpenGraph URL construction.\n */\n baseUrl?: string\n\n /**\n * The path for the current page, e.g. \"/about\".\n * Combined with baseUrl to produce the canonical + OG url.\n * Defaults to \"\".\n */\n path?: string\n\n /**\n * Default values used when the Sanity SEO fields are empty / missing.\n */\n defaults?: SeoMetaDefaults\n\n /**\n * Resolve a Sanity image asset to a plain URL string.\n *\n * @example (using @sanity/image-url)\n * imageUrlResolver: (img) => urlFor(img).width(1200).url()\n */\n imageUrlResolver?: (image: SanityImage | SanityImageWithAlt) => string | null | undefined\n}\n\n// ─── Internal helpers ─────────────────────────────────────────────────────────\n\nconst VALID_OG_TYPES = [\n 'website',\n 'article',\n 'profile',\n 'book',\n 'music',\n 'video',\n 'product',\n] as const\ntype OGType = (typeof VALID_OG_TYPES)[number]\n\n/**\n * Coerce an arbitrary string to a valid OpenGraph type.\n * Falls back to \"website\" when the value is invalid.\n */\nexport function sanitizeOGType(value?: string): OGType {\n if (value && (VALID_OG_TYPES as readonly string[]).includes(value)) {\n return value as OGType\n }\n return 'website'\n}\n\nconst VALID_TWITTER_CARDS = ['summary', 'summary_large_image', 'app', 'player'] as const\ntype TwitterCard = (typeof VALID_TWITTER_CARDS)[number]\n\n/**\n * Coerce an arbitrary string to a valid Twitter card type.\n * Falls back to \"summary_large_image\" when the value is invalid.\n */\nexport function sanitizeTwitterCard(value?: string): TwitterCard {\n if (value && (VALID_TWITTER_CARDS as readonly string[]).includes(value)) {\n return value as TwitterCard\n }\n return 'summary_large_image'\n}\n\n// ─── Core builder ─────────────────────────────────────────────────────────────\n\n/**\n * Convert a Sanity SEO object into a structured metadata object.\n *\n * The return value is structurally compatible with Next.js App Router's\n * `Metadata` type, so you can return it directly from `generateMetadata()`.\n *\n * @example Next.js App Router\n * ```ts\n * import { buildSeoMeta } from 'sanity-plugin-seofields'\n * import { urlFor } from '@/sanity/lib/image'\n *\n * export async function generateMetadata(): Promise<Metadata> {\n * const { seo } = await sanityFetch({ query: PAGE_SEO_QUERY })\n * return buildSeoMeta({\n * seo,\n * baseUrl: process.env.NEXT_PUBLIC_SITE_URL,\n * path: '/about',\n * defaults: { title: 'My Site', siteName: 'My Site' },\n * imageUrlResolver: (img) => urlFor(img).width(1200).url(),\n * })\n * }\n * ```\n */\nexport function buildSeoMeta(options: BuildSeoMetaOptions): SeoMetadata {\n const {seo, baseUrl = '', path = '', defaults = {}, imageUrlResolver} = options\n\n const normalizedBase = baseUrl.replace(/\\/+$/, '') // remove trailing /\n const normalizedPath = path.replace(/^\\/+/, '') // remove leading /\n\n const fullUrl = [normalizedBase, normalizedPath].filter(Boolean).join('/')\n\n // ── OG image resolution ──\n let ogImageURL: string = defaults.ogImage || ''\n if (seo?.openGraph?.imageType === 'url' && seo.openGraph.imageUrl) {\n ogImageURL = seo.openGraph.imageUrl\n } else if (seo?.openGraph?.image && imageUrlResolver) {\n ogImageURL = imageUrlResolver(seo.openGraph.image as SanityImage) || ogImageURL\n }\n\n // ── Twitter image resolution ──\n let twitterImageURL: string = ogImageURL // reuse OG image as fallback\n if (seo?.twitter?.imageType === 'url' && seo.twitter.imageUrl) {\n twitterImageURL = seo.twitter.imageUrl\n } else if (seo?.twitter?.image && imageUrlResolver) {\n twitterImageURL = imageUrlResolver(seo.twitter.image as SanityImage) || twitterImageURL\n }\n\n // ── Custom meta attributes → `other` map ──\n const other: Record<string, string> = {}\n if (Array.isArray(seo?.metaAttributes)) {\n for (const attr of seo!.metaAttributes!) {\n if (attr.key && attr.value) {\n other[attr.key] = attr.value\n }\n }\n }\n\n const ogUrl = seo?.openGraph?.url || fullUrl\n\n return {\n title: seo?.title ?? defaults.title ?? null,\n description: seo?.description ?? defaults.description ?? null,\n keywords: seo?.keywords?.length ? (seo.keywords as string[]) : undefined,\n robots: {\n index: !seo?.robots?.noIndex,\n follow: !seo?.robots?.noFollow,\n googleBot: {\n index: !seo?.robots?.noIndex,\n follow: !seo?.robots?.noFollow,\n },\n },\n openGraph: {\n type: sanitizeOGType(seo?.openGraph?.type ?? undefined),\n url: ogUrl || undefined,\n title: seo?.openGraph?.title ?? defaults.title,\n description: seo?.openGraph?.description ?? defaults.description,\n siteName: seo?.openGraph?.siteName ?? defaults.siteName,\n images: ogImageURL ? [{url: ogImageURL}] : [],\n },\n twitter: {\n card: sanitizeTwitterCard(seo?.twitter?.card ?? undefined),\n site: seo?.twitter?.site ?? defaults.twitterSite,\n creator: seo?.twitter?.creator ?? defaults.twitterCreator,\n title: seo?.twitter?.title ?? defaults.title,\n description: seo?.twitter?.description ?? defaults.description,\n images: twitterImageURL ? [twitterImageURL] : [],\n },\n alternates: {\n canonical: fullUrl || undefined,\n },\n ...(Object.keys(other).length > 0 ? {other} : {}),\n }\n}\n","/**\n * <SeoMetaTags> — Framework-agnostic React SEO meta tag renderer.\n *\n * Renders all SEO meta tags as plain React elements.\n * Place it inside your framework's <Head> component:\n *\n * @example Next.js Pages Router\n * ```tsx\n * import Head from 'next/head'\n * import { SeoMetaTags } from 'sanity-plugin-seofields'\n *\n * export default function Page({ seo }) {\n * return (\n * <>\n * <Head>\n * <SeoMetaTags\n * data={seo}\n * baseUrl=\"https://example.com\"\n * path=\"/about\"\n * defaults={{ title: 'My Site', siteName: 'My Site' }}\n * imageUrlResolver={(img) => urlFor(img).width(1200).url()}\n * />\n * </Head>\n * <main>...</main>\n * </>\n * )\n * }\n * ```\n *\n * @example Nuxt 3 / generic SSR (inside <Head> slot)\n * ```tsx\n * <Head>\n * <SeoMetaTags data={seo} baseUrl=\"https://example.com\" path=\"/\" />\n * </Head>\n * ```\n */\nimport React from 'react'\n\nimport type {SanityImage, SanityImageWithAlt, SeoFields} from '../types'\nimport {buildSeoMeta, type BuildSeoMetaOptions} from './seoMeta'\n\n// ─── Props ────────────────────────────────────────────────────────────────────\n\nexport interface SeoMetaTagsProps {\n /**\n * The raw SEO object from Sanity.\n * Pass `null` / `undefined` to render only the defaults.\n */\n data?: Partial<SeoFields> | null\n\n /**\n * Base URL of your site, e.g. \"https://example.com\".\n * Used for canonical link, og:url fallback.\n */\n baseUrl?: string\n\n /**\n * Current page path, e.g. \"/about\".\n * Defaults to \"\".\n */\n path?: string\n\n /**\n * Default values used when SEO fields are missing.\n */\n defaults?: BuildSeoMetaOptions['defaults']\n\n /**\n * Resolve a Sanity image asset reference to a full URL string.\n *\n * @example\n * imageUrlResolver={(img) => urlFor(img).width(1200).url()}\n */\n imageUrlResolver?: (image: SanityImage | SanityImageWithAlt) => string | null | undefined\n}\n\n// ─── Component ────────────────────────────────────────────────────────────────\n\n/**\n * Renders all SEO meta tags for a page as plain React elements.\n * Intended to be placed inside your framework's <Head> / <head> component.\n *\n * Renders:\n * - `<title>`\n * - `<meta name=\"description\">`\n * - `<meta name=\"keywords\">`\n * - `<meta name=\"robots\">`\n * - OpenGraph meta tags (`og:*`)\n * - Twitter Card meta tags (`twitter:*`)\n * - Any custom `seo.metaAttributes` as `<meta name=\"...\" content=\"...\">`\n */\nexport function SeoMetaTags({data, baseUrl, path, defaults, imageUrlResolver}: SeoMetaTagsProps) {\n const meta = buildSeoMeta({seo: data, baseUrl, path, defaults, imageUrlResolver})\n\n const robotsContent = [\n meta.robots?.index === false ? 'noindex' : 'index',\n meta.robots?.follow === false ? 'nofollow' : 'follow',\n ].join(', ')\n\n return (\n <>\n {/* ── Title ── */}\n {meta.title && <title>{meta.title}</title>}\n\n {/* ── Basic meta ── */}\n {meta.description && <meta name=\"description\" content={meta.description} />}\n {meta.keywords?.length ? <meta name=\"keywords\" content={meta.keywords.join(', ')} /> : null}\n <meta name=\"robots\" content={robotsContent} />\n <meta name=\"googlebot\" content={robotsContent} />\n\n {/* ── Open Graph ── */}\n {meta.openGraph?.type && <meta property=\"og:type\" content={meta.openGraph.type} />}\n {meta.openGraph?.url && <meta property=\"og:url\" content={meta.openGraph.url} />}\n {meta.openGraph?.title && <meta property=\"og:title\" content={meta.openGraph.title} />}\n {meta.openGraph?.description && (\n <meta property=\"og:description\" content={meta.openGraph.description} />\n )}\n {meta.openGraph?.siteName && (\n <meta property=\"og:site_name\" content={meta.openGraph.siteName} />\n )}\n {meta.openGraph?.images?.map((img, i) => (\n <React.Fragment key={`og-img-${i}`}>\n <meta property=\"og:image\" content={img.url} />\n {img.width && <meta property=\"og:image:width\" content={String(img.width)} />}\n {img.height && <meta property=\"og:image:height\" content={String(img.height)} />}\n {img.alt && <meta property=\"og:image:alt\" content={img.alt} />}\n </React.Fragment>\n ))}\n\n {/* ── Twitter Card ── */}\n {meta.twitter?.card && <meta name=\"twitter:card\" content={meta.twitter.card} />}\n {meta.twitter?.site && <meta name=\"twitter:site\" content={meta.twitter.site} />}\n {meta.twitter?.creator && <meta name=\"twitter:creator\" content={meta.twitter.creator} />}\n {meta.twitter?.title && <meta name=\"twitter:title\" content={meta.twitter.title} />}\n {meta.twitter?.description && (\n <meta name=\"twitter:description\" content={meta.twitter.description} />\n )}\n {meta.twitter?.images?.map((url, i) => (\n <meta key={`tw-img-${i}`} name=\"twitter:image\" content={url} />\n ))}\n\n {/* ── Custom meta attributes ── */}\n {meta.other &&\n Object.entries(meta.other).map(([name, content]) => (\n <meta key={`custom-${name}`} name={name} content={content} />\n ))}\n\n {/* ── Canonical URL ── */}\n {meta.alternates?.canonical && <link rel=\"canonical\" href={meta.alternates.canonical} />}\n </>\n )\n}\n\nexport default SeoMetaTags\n"],"names":["jsxs","Fragment","jsx","React"],"mappings":";;;;;;;AAwJA,MAAM,iBAAiB;AAAA,EACrB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAOO,SAAS,eAAe,OAAwB;AACrD,SAAI,SAAU,eAAqC,SAAS,KAAK,IACxD,QAEF;AACT;AAEA,MAAM,sBAAsB,CAAC,WAAW,uBAAuB,OAAO,QAAQ;AAOvE,SAAS,oBAAoB,OAA6B;AAC/D,SAAI,SAAU,oBAA0C,SAAS,KAAK,IAC7D,QAEF;AACT;AA2BO,SAAS,aAAa,SAA2C;AACtE,QAAM,EAAC,KAAK,UAAU,IAAI,OAAO,IAAI,WAAW,CAAA,GAAI,iBAAA,IAAoB,SAElE,iBAAiB,QAAQ,QAAQ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AA,IAAC,QAAA,EAAK,MAAK,mBAAkB,SAAS,KAAK,QAAQ,QAAA,CAAS;AAAA,IACrF,KAAK,SAAS,SAASA,2BAAAA,IAAC,QAAA,EAAK,MAAK,iBAAgB,SAAS,KAAK,QAAQ,MAAA,CAAO;AAAA,IAC/E,KAAK,SAAS,eACbA,2BAAAA,IAAC,QAAA,EAAK,MAAK,uBAAsB,SAAS,KAAK,QAAQ,YAAA,CAAa;AAAA,IAErE,KAAK,SAAS,QAAQ,IAAI,CAAC,KAAK,MAC/BA,2BAAAA,IAAC,QAAA,EAAyB,MAAK,iBAAgB,SAAS,IAAA,GAA7C,UAAU,CAAC,EAAuC,CAC9D;AAAA,IAGA,KAAK,SACJ,OAAO,QAAQ,KAAK,KAAK,EAAE,IAAI,CAAC,CAAC,MAAM,OAAO,qCAC3C,QAAA,EAA4B,MAAY,WAA9B,UAAU,IAAI,EAAkC,CAC5D;AAAA,IAGF,KAAK,YAAY,aAAaA,2BAAAA,IAAC,QAAA,EAAK,KAAI,aAAY,MAAM,KAAK,WAAW,UAAA,CAAW;AAAA,EAAA,GACxF;AAEJ;;;;;"}
package/dist/next.mjs ADDED
@@ -0,0 +1,102 @@
1
+ import { jsxs, Fragment, jsx } from "react/jsx-runtime";
2
+ import r from "react";
3
+ const VALID_OG_TYPES = [
4
+ "website",
5
+ "article",
6
+ "profile",
7
+ "book",
8
+ "music",
9
+ "video",
10
+ "product"
11
+ ];
12
+ function sanitizeOGType(value) {
13
+ return value && VALID_OG_TYPES.includes(value) ? value : "website";
14
+ }
15
+ const VALID_TWITTER_CARDS = ["summary", "summary_large_image", "app", "player"];
16
+ function sanitizeTwitterCard(value) {
17
+ return value && VALID_TWITTER_CARDS.includes(value) ? value : "summary_large_image";
18
+ }
19
+ function buildSeoMeta(options) {
20
+ const { seo, baseUrl = "", path = "", defaults = {}, imageUrlResolver } = options, normalizedBase = baseUrl.replace(/\/+$/, ""), normalizedPath = path.replace(/^\/+/, ""), fullUrl = [normalizedBase, normalizedPath].filter(Boolean).join("/");
21
+ let ogImageURL = defaults.ogImage || "";
22
+ seo?.openGraph?.imageType === "url" && seo.openGraph.imageUrl ? ogImageURL = seo.openGraph.imageUrl : seo?.openGraph?.image && imageUrlResolver && (ogImageURL = imageUrlResolver(seo.openGraph.image) || ogImageURL);
23
+ let twitterImageURL = ogImageURL;
24
+ seo?.twitter?.imageType === "url" && seo.twitter.imageUrl ? twitterImageURL = seo.twitter.imageUrl : seo?.twitter?.image && imageUrlResolver && (twitterImageURL = imageUrlResolver(seo.twitter.image) || twitterImageURL);
25
+ const other = {};
26
+ if (Array.isArray(seo?.metaAttributes))
27
+ for (const attr of seo.metaAttributes)
28
+ attr.key && attr.value && (other[attr.key] = attr.value);
29
+ const ogUrl = seo?.openGraph?.url || fullUrl;
30
+ return {
31
+ title: seo?.title ?? defaults.title ?? null,
32
+ description: seo?.description ?? defaults.description ?? null,
33
+ keywords: seo?.keywords?.length ? seo.keywords : void 0,
34
+ robots: {
35
+ index: !seo?.robots?.noIndex,
36
+ follow: !seo?.robots?.noFollow,
37
+ googleBot: {
38
+ index: !seo?.robots?.noIndex,
39
+ follow: !seo?.robots?.noFollow
40
+ }
41
+ },
42
+ openGraph: {
43
+ type: sanitizeOGType(seo?.openGraph?.type ?? void 0),
44
+ url: ogUrl || void 0,
45
+ title: seo?.openGraph?.title ?? defaults.title,
46
+ description: seo?.openGraph?.description ?? defaults.description,
47
+ siteName: seo?.openGraph?.siteName ?? defaults.siteName,
48
+ images: ogImageURL ? [{ url: ogImageURL }] : []
49
+ },
50
+ twitter: {
51
+ card: sanitizeTwitterCard(seo?.twitter?.card ?? void 0),
52
+ site: seo?.twitter?.site ?? defaults.twitterSite,
53
+ creator: seo?.twitter?.creator ?? defaults.twitterCreator,
54
+ title: seo?.twitter?.title ?? defaults.title,
55
+ description: seo?.twitter?.description ?? defaults.description,
56
+ images: twitterImageURL ? [twitterImageURL] : []
57
+ },
58
+ alternates: {
59
+ canonical: fullUrl || void 0
60
+ },
61
+ ...Object.keys(other).length > 0 ? { other } : {}
62
+ };
63
+ }
64
+ function SeoMetaTags({ data, baseUrl, path, defaults, imageUrlResolver }) {
65
+ const meta = buildSeoMeta({ seo: data, baseUrl, path, defaults, imageUrlResolver }), robotsContent = [
66
+ meta.robots?.index === !1 ? "noindex" : "index",
67
+ meta.robots?.follow === !1 ? "nofollow" : "follow"
68
+ ].join(", ");
69
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
70
+ meta.title && /* @__PURE__ */ jsx("title", { children: meta.title }),
71
+ meta.description && /* @__PURE__ */ jsx("meta", { name: "description", content: meta.description }),
72
+ meta.keywords?.length ? /* @__PURE__ */ jsx("meta", { name: "keywords", content: meta.keywords.join(", ") }) : null,
73
+ /* @__PURE__ */ jsx("meta", { name: "robots", content: robotsContent }),
74
+ /* @__PURE__ */ jsx("meta", { name: "googlebot", content: robotsContent }),
75
+ meta.openGraph?.type && /* @__PURE__ */ jsx("meta", { property: "og:type", content: meta.openGraph.type }),
76
+ meta.openGraph?.url && /* @__PURE__ */ jsx("meta", { property: "og:url", content: meta.openGraph.url }),
77
+ meta.openGraph?.title && /* @__PURE__ */ jsx("meta", { property: "og:title", content: meta.openGraph.title }),
78
+ meta.openGraph?.description && /* @__PURE__ */ jsx("meta", { property: "og:description", content: meta.openGraph.description }),
79
+ meta.openGraph?.siteName && /* @__PURE__ */ jsx("meta", { property: "og:site_name", content: meta.openGraph.siteName }),
80
+ meta.openGraph?.images?.map((img, i) => /* @__PURE__ */ jsxs(r.Fragment, { children: [
81
+ /* @__PURE__ */ jsx("meta", { property: "og:image", content: img.url }),
82
+ img.width && /* @__PURE__ */ jsx("meta", { property: "og:image:width", content: String(img.width) }),
83
+ img.height && /* @__PURE__ */ jsx("meta", { property: "og:image:height", content: String(img.height) }),
84
+ img.alt && /* @__PURE__ */ jsx("meta", { property: "og:image:alt", content: img.alt })
85
+ ] }, `og-img-${i}`)),
86
+ meta.twitter?.card && /* @__PURE__ */ jsx("meta", { name: "twitter:card", content: meta.twitter.card }),
87
+ meta.twitter?.site && /* @__PURE__ */ jsx("meta", { name: "twitter:site", content: meta.twitter.site }),
88
+ meta.twitter?.creator && /* @__PURE__ */ jsx("meta", { name: "twitter:creator", content: meta.twitter.creator }),
89
+ meta.twitter?.title && /* @__PURE__ */ jsx("meta", { name: "twitter:title", content: meta.twitter.title }),
90
+ meta.twitter?.description && /* @__PURE__ */ jsx("meta", { name: "twitter:description", content: meta.twitter.description }),
91
+ meta.twitter?.images?.map((url, i) => /* @__PURE__ */ jsx("meta", { name: "twitter:image", content: url }, `tw-img-${i}`)),
92
+ meta.other && Object.entries(meta.other).map(([name, content]) => /* @__PURE__ */ jsx("meta", { name, content }, `custom-${name}`)),
93
+ meta.alternates?.canonical && /* @__PURE__ */ jsx("link", { rel: "canonical", href: meta.alternates.canonical })
94
+ ] });
95
+ }
96
+ export {
97
+ SeoMetaTags,
98
+ buildSeoMeta,
99
+ sanitizeOGType,
100
+ sanitizeTwitterCard
101
+ };
102
+ //# sourceMappingURL=next.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next.mjs","sources":["../src/helpers/seoMeta.ts","../src/helpers/SeoMetaTags.tsx"],"sourcesContent":["/**\n * Headless CMS integration helpers for sanity-plugin-seofields\n *\n * Provides framework-agnostic SEO metadata utilities for use with:\n * - Next.js App Router → buildSeoMeta() inside generateMetadata()\n * - Next.js Pages Router → <SeoMetaTags> inside Next.js <Head>\n * - Nuxt / Remix / any SSR → <SeoMetaTags> inside your <head> slot\n */\n\nimport type {SanityImage, SanityImageWithAlt, SeoFields} from '../types'\n\n// ─── Types ────────────────────────────────────────────────────────────────────\n\n/** Structured metadata returned by buildSeoMeta(). Compatible with Next.js Metadata (App Router). */\nexport interface SeoMetadata {\n title?: string | null\n description?: string | null\n keywords?: string[]\n robots?: {\n index?: boolean\n follow?: boolean\n googleBot?: {\n index?: boolean\n follow?: boolean\n }\n }\n openGraph?: {\n type?: string\n url?: string\n title?: string\n description?: string\n siteName?: string\n images?: Array<{url: string; width?: number; height?: number; alt?: string}>\n }\n twitter?: {\n card?: string\n site?: string\n creator?: string\n title?: string\n description?: string\n images?: string[]\n }\n alternates?: {\n canonical?: string\n }\n /** Any custom meta attributes from seo.metaAttributes */\n other?: Record<string, string>\n}\n\n/** Default values used when SEO fields are missing. */\nexport interface SeoMetaDefaults {\n title?: string\n description?: string\n siteName?: string\n twitterSite?: string\n twitterCreator?: string\n /** Fallback image URL when no OG / Twitter image is set. */\n ogImage?: string\n}\n\n/**\n * Permissive image shape accepted by buildSeoMeta — compatible with both the\n * plugin's SanityImage and Sanity's code-generated image type (where `asset`\n * and `alt` are optional).\n */\ninterface SeoImageInput {\n _type?: string\n asset?: {_ref: string; _type: string; _weak?: boolean; [key: string]: unknown}\n hotspot?: unknown\n crop?: unknown\n alt?: string\n}\n\n/**\n * Input-compatible variant of SeoFields. Structurally matches Sanity's\n * code-generated types (where `asset`, `alt`, `key`, and `type` are all\n * optional), so you can pass `data.seo` from a sanityFetch result directly\n * without any `as any` or manual casting.\n */\nexport interface SeoFieldsInput {\n _type?: string\n robots?: {noIndex?: boolean | null; noFollow?: boolean | null} | null\n title?: string | null\n description?: string | null\n metaImage?: SeoImageInput | null\n metaAttributes?: Array<{_key?: string; key?: string; value?: string; type?: string}> | null\n keywords?: string[] | null\n canonicalUrl?: string | null\n openGraph?: {\n _type?: string\n url?: string | null\n title?: string | null\n description?: string | null\n siteName?: string | null\n type?: string | null\n imageType?: string | null\n image?: SeoImageInput | null\n imageUrl?: string | null\n } | null\n twitter?: {\n _type?: string\n card?: string | null\n site?: string | null\n creator?: string | null\n title?: string | null\n description?: string | null\n imageType?: string | null\n image?: SeoImageInput | null\n imageUrl?: string | null\n } | null\n}\n\n/** Options accepted by buildSeoMeta(). */\nexport interface BuildSeoMetaOptions {\n /**\n * The raw SEO object from Sanity (_type excluded or included — both work).\n * Pass `null` or `undefined` to fall back entirely to `defaults`.\n *\n * Accepts both the strict plugin `SeoFields` type and Sanity's code-generated\n * type (which has all nested fields optional) without any `as any` cast.\n */\n seo?: SeoFieldsInput | null\n\n /**\n * The base URL of your site, e.g. \"https://example.com\".\n * Used for canonical URL and OpenGraph URL construction.\n */\n baseUrl?: string\n\n /**\n * The path for the current page, e.g. \"/about\".\n * Combined with baseUrl to produce the canonical + OG url.\n * Defaults to \"\".\n */\n path?: string\n\n /**\n * Default values used when the Sanity SEO fields are empty / missing.\n */\n defaults?: SeoMetaDefaults\n\n /**\n * Resolve a Sanity image asset to a plain URL string.\n *\n * @example (using @sanity/image-url)\n * imageUrlResolver: (img) => urlFor(img).width(1200).url()\n */\n imageUrlResolver?: (image: SanityImage | SanityImageWithAlt) => string | null | undefined\n}\n\n// ─── Internal helpers ─────────────────────────────────────────────────────────\n\nconst VALID_OG_TYPES = [\n 'website',\n 'article',\n 'profile',\n 'book',\n 'music',\n 'video',\n 'product',\n] as const\ntype OGType = (typeof VALID_OG_TYPES)[number]\n\n/**\n * Coerce an arbitrary string to a valid OpenGraph type.\n * Falls back to \"website\" when the value is invalid.\n */\nexport function sanitizeOGType(value?: string): OGType {\n if (value && (VALID_OG_TYPES as readonly string[]).includes(value)) {\n return value as OGType\n }\n return 'website'\n}\n\nconst VALID_TWITTER_CARDS = ['summary', 'summary_large_image', 'app', 'player'] as const\ntype TwitterCard = (typeof VALID_TWITTER_CARDS)[number]\n\n/**\n * Coerce an arbitrary string to a valid Twitter card type.\n * Falls back to \"summary_large_image\" when the value is invalid.\n */\nexport function sanitizeTwitterCard(value?: string): TwitterCard {\n if (value && (VALID_TWITTER_CARDS as readonly string[]).includes(value)) {\n return value as TwitterCard\n }\n return 'summary_large_image'\n}\n\n// ─── Core builder ─────────────────────────────────────────────────────────────\n\n/**\n * Convert a Sanity SEO object into a structured metadata object.\n *\n * The return value is structurally compatible with Next.js App Router's\n * `Metadata` type, so you can return it directly from `generateMetadata()`.\n *\n * @example Next.js App Router\n * ```ts\n * import { buildSeoMeta } from 'sanity-plugin-seofields'\n * import { urlFor } from '@/sanity/lib/image'\n *\n * export async function generateMetadata(): Promise<Metadata> {\n * const { seo } = await sanityFetch({ query: PAGE_SEO_QUERY })\n * return buildSeoMeta({\n * seo,\n * baseUrl: process.env.NEXT_PUBLIC_SITE_URL,\n * path: '/about',\n * defaults: { title: 'My Site', siteName: 'My Site' },\n * imageUrlResolver: (img) => urlFor(img).width(1200).url(),\n * })\n * }\n * ```\n */\nexport function buildSeoMeta(options: BuildSeoMetaOptions): SeoMetadata {\n const {seo, baseUrl = '', path = '', defaults = {}, imageUrlResolver} = options\n\n const normalizedBase = baseUrl.replace(/\\/+$/, '') // remove trailing /\n const normalizedPath = path.replace(/^\\/+/, '') // remove leading /\n\n const fullUrl = [normalizedBase, normalizedPath].filter(Boolean).join('/')\n\n // ── OG image resolution ──\n let ogImageURL: string = defaults.ogImage || ''\n if (seo?.openGraph?.imageType === 'url' && seo.openGraph.imageUrl) {\n ogImageURL = seo.openGraph.imageUrl\n } else if (seo?.openGraph?.image && imageUrlResolver) {\n ogImageURL = imageUrlResolver(seo.openGraph.image as SanityImage) || ogImageURL\n }\n\n // ── Twitter image resolution ──\n let twitterImageURL: string = ogImageURL // reuse OG image as fallback\n if (seo?.twitter?.imageType === 'url' && seo.twitter.imageUrl) {\n twitterImageURL = seo.twitter.imageUrl\n } else if (seo?.twitter?.image && imageUrlResolver) {\n twitterImageURL = imageUrlResolver(seo.twitter.image as SanityImage) || twitterImageURL\n }\n\n // ── Custom meta attributes → `other` map ──\n const other: Record<string, string> = {}\n if (Array.isArray(seo?.metaAttributes)) {\n for (const attr of seo!.metaAttributes!) {\n if (attr.key && attr.value) {\n other[attr.key] = attr.value\n }\n }\n }\n\n const ogUrl = seo?.openGraph?.url || fullUrl\n\n return {\n title: seo?.title ?? defaults.title ?? null,\n description: seo?.description ?? defaults.description ?? null,\n keywords: seo?.keywords?.length ? (seo.keywords as string[]) : undefined,\n robots: {\n index: !seo?.robots?.noIndex,\n follow: !seo?.robots?.noFollow,\n googleBot: {\n index: !seo?.robots?.noIndex,\n follow: !seo?.robots?.noFollow,\n },\n },\n openGraph: {\n type: sanitizeOGType(seo?.openGraph?.type ?? undefined),\n url: ogUrl || undefined,\n title: seo?.openGraph?.title ?? defaults.title,\n description: seo?.openGraph?.description ?? defaults.description,\n siteName: seo?.openGraph?.siteName ?? defaults.siteName,\n images: ogImageURL ? [{url: ogImageURL}] : [],\n },\n twitter: {\n card: sanitizeTwitterCard(seo?.twitter?.card ?? undefined),\n site: seo?.twitter?.site ?? defaults.twitterSite,\n creator: seo?.twitter?.creator ?? defaults.twitterCreator,\n title: seo?.twitter?.title ?? defaults.title,\n description: seo?.twitter?.description ?? defaults.description,\n images: twitterImageURL ? [twitterImageURL] : [],\n },\n alternates: {\n canonical: fullUrl || undefined,\n },\n ...(Object.keys(other).length > 0 ? {other} : {}),\n }\n}\n","/**\n * <SeoMetaTags> — Framework-agnostic React SEO meta tag renderer.\n *\n * Renders all SEO meta tags as plain React elements.\n * Place it inside your framework's <Head> component:\n *\n * @example Next.js Pages Router\n * ```tsx\n * import Head from 'next/head'\n * import { SeoMetaTags } from 'sanity-plugin-seofields'\n *\n * export default function Page({ seo }) {\n * return (\n * <>\n * <Head>\n * <SeoMetaTags\n * data={seo}\n * baseUrl=\"https://example.com\"\n * path=\"/about\"\n * defaults={{ title: 'My Site', siteName: 'My Site' }}\n * imageUrlResolver={(img) => urlFor(img).width(1200).url()}\n * />\n * </Head>\n * <main>...</main>\n * </>\n * )\n * }\n * ```\n *\n * @example Nuxt 3 / generic SSR (inside <Head> slot)\n * ```tsx\n * <Head>\n * <SeoMetaTags data={seo} baseUrl=\"https://example.com\" path=\"/\" />\n * </Head>\n * ```\n */\nimport React from 'react'\n\nimport type {SanityImage, SanityImageWithAlt, SeoFields} from '../types'\nimport {buildSeoMeta, type BuildSeoMetaOptions} from './seoMeta'\n\n// ─── Props ────────────────────────────────────────────────────────────────────\n\nexport interface SeoMetaTagsProps {\n /**\n * The raw SEO object from Sanity.\n * Pass `null` / `undefined` to render only the defaults.\n */\n data?: Partial<SeoFields> | null\n\n /**\n * Base URL of your site, e.g. \"https://example.com\".\n * Used for canonical link, og:url fallback.\n */\n baseUrl?: string\n\n /**\n * Current page path, e.g. \"/about\".\n * Defaults to \"\".\n */\n path?: string\n\n /**\n * Default values used when SEO fields are missing.\n */\n defaults?: BuildSeoMetaOptions['defaults']\n\n /**\n * Resolve a Sanity image asset reference to a full URL string.\n *\n * @example\n * imageUrlResolver={(img) => urlFor(img).width(1200).url()}\n */\n imageUrlResolver?: (image: SanityImage | SanityImageWithAlt) => string | null | undefined\n}\n\n// ─── Component ────────────────────────────────────────────────────────────────\n\n/**\n * Renders all SEO meta tags for a page as plain React elements.\n * Intended to be placed inside your framework's <Head> / <head> component.\n *\n * Renders:\n * - `<title>`\n * - `<meta name=\"description\">`\n * - `<meta name=\"keywords\">`\n * - `<meta name=\"robots\">`\n * - OpenGraph meta tags (`og:*`)\n * - Twitter Card meta tags (`twitter:*`)\n * - Any custom `seo.metaAttributes` as `<meta name=\"...\" content=\"...\">`\n */\nexport function SeoMetaTags({data, baseUrl, path, defaults, imageUrlResolver}: SeoMetaTagsProps) {\n const meta = buildSeoMeta({seo: data, baseUrl, path, defaults, imageUrlResolver})\n\n const robotsContent = [\n meta.robots?.index === false ? 'noindex' : 'index',\n meta.robots?.follow === false ? 'nofollow' : 'follow',\n ].join(', ')\n\n return (\n <>\n {/* ── Title ── */}\n {meta.title && <title>{meta.title}</title>}\n\n {/* ── Basic meta ── */}\n {meta.description && <meta name=\"description\" content={meta.description} />}\n {meta.keywords?.length ? <meta name=\"keywords\" content={meta.keywords.join(', ')} /> : null}\n <meta name=\"robots\" content={robotsContent} />\n <meta name=\"googlebot\" content={robotsContent} />\n\n {/* ── Open Graph ── */}\n {meta.openGraph?.type && <meta property=\"og:type\" content={meta.openGraph.type} />}\n {meta.openGraph?.url && <meta property=\"og:url\" content={meta.openGraph.url} />}\n {meta.openGraph?.title && <meta property=\"og:title\" content={meta.openGraph.title} />}\n {meta.openGraph?.description && (\n <meta property=\"og:description\" content={meta.openGraph.description} />\n )}\n {meta.openGraph?.siteName && (\n <meta property=\"og:site_name\" content={meta.openGraph.siteName} />\n )}\n {meta.openGraph?.images?.map((img, i) => (\n <React.Fragment key={`og-img-${i}`}>\n <meta property=\"og:image\" content={img.url} />\n {img.width && <meta property=\"og:image:width\" content={String(img.width)} />}\n {img.height && <meta property=\"og:image:height\" content={String(img.height)} />}\n {img.alt && <meta property=\"og:image:alt\" content={img.alt} />}\n </React.Fragment>\n ))}\n\n {/* ── Twitter Card ── */}\n {meta.twitter?.card && <meta name=\"twitter:card\" content={meta.twitter.card} />}\n {meta.twitter?.site && <meta name=\"twitter:site\" content={meta.twitter.site} />}\n {meta.twitter?.creator && <meta name=\"twitter:creator\" content={meta.twitter.creator} />}\n {meta.twitter?.title && <meta name=\"twitter:title\" content={meta.twitter.title} />}\n {meta.twitter?.description && (\n <meta name=\"twitter:description\" content={meta.twitter.description} />\n )}\n {meta.twitter?.images?.map((url, i) => (\n <meta key={`tw-img-${i}`} name=\"twitter:image\" content={url} />\n ))}\n\n {/* ── Custom meta attributes ── */}\n {meta.other &&\n Object.entries(meta.other).map(([name, content]) => (\n <meta key={`custom-${name}`} name={name} content={content} />\n ))}\n\n {/* ── Canonical URL ── */}\n {meta.alternates?.canonical && <link rel=\"canonical\" href={meta.alternates.canonical} />}\n </>\n )\n}\n\nexport default SeoMetaTags\n"],"names":["React"],"mappings":";;AAwJA,MAAM,iBAAiB;AAAA,EACrB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAOO,SAAS,eAAe,OAAwB;AACrD,SAAI,SAAU,eAAqC,SAAS,KAAK,IACxD,QAEF;AACT;AAEA,MAAM,sBAAsB,CAAC,WAAW,uBAAuB,OAAO,QAAQ;AAOvE,SAAS,oBAAoB,OAA6B;AAC/D,SAAI,SAAU,oBAA0C,SAAS,KAAK,IAC7D,QAEF;AACT;AA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W,oBAAC,QAAA,EAAK,MAAK,mBAAkB,SAAS,KAAK,QAAQ,QAAA,CAAS;AAAA,IACrF,KAAK,SAAS,SAAS,oBAAC,QAAA,EAAK,MAAK,iBAAgB,SAAS,KAAK,QAAQ,MAAA,CAAO;AAAA,IAC/E,KAAK,SAAS,eACb,oBAAC,QAAA,EAAK,MAAK,uBAAsB,SAAS,KAAK,QAAQ,YAAA,CAAa;AAAA,IAErE,KAAK,SAAS,QAAQ,IAAI,CAAC,KAAK,MAC/B,oBAAC,QAAA,EAAyB,MAAK,iBAAgB,SAAS,IAAA,GAA7C,UAAU,CAAC,EAAuC,CAC9D;AAAA,IAGA,KAAK,SACJ,OAAO,QAAQ,KAAK,KAAK,EAAE,IAAI,CAAC,CAAC,MAAM,OAAO,0BAC3C,QAAA,EAA4B,MAAY,WAA9B,UAAU,IAAI,EAAkC,CAC5D;AAAA,IAGF,KAAK,YAAY,aAAa,oBAAC,QAAA,EAAK,KAAI,aAAY,MAAM,KAAK,WAAW,UAAA,CAAW;AAAA,EAAA,GACxF;AAEJ;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanity-plugin-seofields",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "description": "A Sanity Studio plugin to manage SEO fields like meta titles, descriptions, and Open Graph tags for structured, search-optimized content.",
5
5
  "keywords": [
6
6
  "sanity",
@@ -23,10 +23,21 @@
23
23
  "import": "./dist/index.mjs",
24
24
  "default": "./dist/index.js"
25
25
  },
26
+ "./next": {
27
+ "source": "./src/next.ts",
28
+ "import": "./dist/next.mjs",
29
+ "require": "./dist/next.js",
30
+ "default": "./dist/next.js"
31
+ },
26
32
  "./package.json": "./package.json"
27
33
  },
28
34
  "main": "./dist/index.js",
29
35
  "types": "./dist/index.d.ts",
36
+ "typesVersions": {
37
+ "*": {
38
+ "next": ["./dist/next.d.ts"]
39
+ }
40
+ },
30
41
  "files": [
31
42
  "dist",
32
43
  "sanity.json",
@@ -57,6 +68,7 @@
57
68
  "@types/react": "^19.1.13",
58
69
  "@typescript-eslint/eslint-plugin": "^8.44.0",
59
70
  "@typescript-eslint/parser": "^8.44.0",
71
+ "baseline-browser-mapping": "^2.10.8",
60
72
  "eslint": "^8.57.1",
61
73
  "eslint-config-prettier": "^10.1.8",
62
74
  "eslint-config-sanity": "^7.1.4",
@@ -0,0 +1,154 @@
1
+ /**
2
+ * <SeoMetaTags> — Framework-agnostic React SEO meta tag renderer.
3
+ *
4
+ * Renders all SEO meta tags as plain React elements.
5
+ * Place it inside your framework's <Head> component:
6
+ *
7
+ * @example Next.js Pages Router
8
+ * ```tsx
9
+ * import Head from 'next/head'
10
+ * import { SeoMetaTags } from 'sanity-plugin-seofields'
11
+ *
12
+ * export default function Page({ seo }) {
13
+ * return (
14
+ * <>
15
+ * <Head>
16
+ * <SeoMetaTags
17
+ * data={seo}
18
+ * baseUrl="https://example.com"
19
+ * path="/about"
20
+ * defaults={{ title: 'My Site', siteName: 'My Site' }}
21
+ * imageUrlResolver={(img) => urlFor(img).width(1200).url()}
22
+ * />
23
+ * </Head>
24
+ * <main>...</main>
25
+ * </>
26
+ * )
27
+ * }
28
+ * ```
29
+ *
30
+ * @example Nuxt 3 / generic SSR (inside <Head> slot)
31
+ * ```tsx
32
+ * <Head>
33
+ * <SeoMetaTags data={seo} baseUrl="https://example.com" path="/" />
34
+ * </Head>
35
+ * ```
36
+ */
37
+ import React from 'react'
38
+
39
+ import type {SanityImage, SanityImageWithAlt, SeoFields} from '../types'
40
+ import {buildSeoMeta, type BuildSeoMetaOptions} from './seoMeta'
41
+
42
+ // ─── Props ────────────────────────────────────────────────────────────────────
43
+
44
+ export interface SeoMetaTagsProps {
45
+ /**
46
+ * The raw SEO object from Sanity.
47
+ * Pass `null` / `undefined` to render only the defaults.
48
+ */
49
+ data?: Partial<SeoFields> | null
50
+
51
+ /**
52
+ * Base URL of your site, e.g. "https://example.com".
53
+ * Used for canonical link, og:url fallback.
54
+ */
55
+ baseUrl?: string
56
+
57
+ /**
58
+ * Current page path, e.g. "/about".
59
+ * Defaults to "".
60
+ */
61
+ path?: string
62
+
63
+ /**
64
+ * Default values used when SEO fields are missing.
65
+ */
66
+ defaults?: BuildSeoMetaOptions['defaults']
67
+
68
+ /**
69
+ * Resolve a Sanity image asset reference to a full URL string.
70
+ *
71
+ * @example
72
+ * imageUrlResolver={(img) => urlFor(img).width(1200).url()}
73
+ */
74
+ imageUrlResolver?: (image: SanityImage | SanityImageWithAlt) => string | null | undefined
75
+ }
76
+
77
+ // ─── Component ────────────────────────────────────────────────────────────────
78
+
79
+ /**
80
+ * Renders all SEO meta tags for a page as plain React elements.
81
+ * Intended to be placed inside your framework's <Head> / <head> component.
82
+ *
83
+ * Renders:
84
+ * - `<title>`
85
+ * - `<meta name="description">`
86
+ * - `<meta name="keywords">`
87
+ * - `<meta name="robots">`
88
+ * - OpenGraph meta tags (`og:*`)
89
+ * - Twitter Card meta tags (`twitter:*`)
90
+ * - Any custom `seo.metaAttributes` as `<meta name="..." content="...">`
91
+ */
92
+ export function SeoMetaTags({data, baseUrl, path, defaults, imageUrlResolver}: SeoMetaTagsProps) {
93
+ const meta = buildSeoMeta({seo: data, baseUrl, path, defaults, imageUrlResolver})
94
+
95
+ const robotsContent = [
96
+ meta.robots?.index === false ? 'noindex' : 'index',
97
+ meta.robots?.follow === false ? 'nofollow' : 'follow',
98
+ ].join(', ')
99
+
100
+ return (
101
+ <>
102
+ {/* ── Title ── */}
103
+ {meta.title && <title>{meta.title}</title>}
104
+
105
+ {/* ── Basic meta ── */}
106
+ {meta.description && <meta name="description" content={meta.description} />}
107
+ {meta.keywords?.length ? <meta name="keywords" content={meta.keywords.join(', ')} /> : null}
108
+ <meta name="robots" content={robotsContent} />
109
+ <meta name="googlebot" content={robotsContent} />
110
+
111
+ {/* ── Open Graph ── */}
112
+ {meta.openGraph?.type && <meta property="og:type" content={meta.openGraph.type} />}
113
+ {meta.openGraph?.url && <meta property="og:url" content={meta.openGraph.url} />}
114
+ {meta.openGraph?.title && <meta property="og:title" content={meta.openGraph.title} />}
115
+ {meta.openGraph?.description && (
116
+ <meta property="og:description" content={meta.openGraph.description} />
117
+ )}
118
+ {meta.openGraph?.siteName && (
119
+ <meta property="og:site_name" content={meta.openGraph.siteName} />
120
+ )}
121
+ {meta.openGraph?.images?.map((img, i) => (
122
+ <React.Fragment key={`og-img-${i}`}>
123
+ <meta property="og:image" content={img.url} />
124
+ {img.width && <meta property="og:image:width" content={String(img.width)} />}
125
+ {img.height && <meta property="og:image:height" content={String(img.height)} />}
126
+ {img.alt && <meta property="og:image:alt" content={img.alt} />}
127
+ </React.Fragment>
128
+ ))}
129
+
130
+ {/* ── Twitter Card ── */}
131
+ {meta.twitter?.card && <meta name="twitter:card" content={meta.twitter.card} />}
132
+ {meta.twitter?.site && <meta name="twitter:site" content={meta.twitter.site} />}
133
+ {meta.twitter?.creator && <meta name="twitter:creator" content={meta.twitter.creator} />}
134
+ {meta.twitter?.title && <meta name="twitter:title" content={meta.twitter.title} />}
135
+ {meta.twitter?.description && (
136
+ <meta name="twitter:description" content={meta.twitter.description} />
137
+ )}
138
+ {meta.twitter?.images?.map((url, i) => (
139
+ <meta key={`tw-img-${i}`} name="twitter:image" content={url} />
140
+ ))}
141
+
142
+ {/* ── Custom meta attributes ── */}
143
+ {meta.other &&
144
+ Object.entries(meta.other).map(([name, content]) => (
145
+ <meta key={`custom-${name}`} name={name} content={content} />
146
+ ))}
147
+
148
+ {/* ── Canonical URL ── */}
149
+ {meta.alternates?.canonical && <link rel="canonical" href={meta.alternates.canonical} />}
150
+ </>
151
+ )
152
+ }
153
+
154
+ export default SeoMetaTags