next-seo
Version:
SEO plugin for Next.js projects
755 lines • 21.2 kB
JavaScript
// src/pages/core/buildTags.tsx
import { jsx } from "react/jsx-runtime";
var defaults = {
templateTitle: "",
noindex: false,
nofollow: false,
norobots: false,
defaultOpenGraphImageWidth: 0,
defaultOpenGraphImageHeight: 0,
defaultOpenGraphVideoWidth: 0,
defaultOpenGraphVideoHeight: 0
};
var buildOpenGraphMediaTags = (mediaType, media = [], {
defaultWidth,
defaultHeight
} = {}) => {
return media.reduce((tags, medium, index) => {
tags.push(
/* @__PURE__ */ jsx(
"meta",
{
property: `og:${mediaType}`,
content: medium.url
},
`og:${mediaType}:0${index}`
)
);
if (medium.alt) {
tags.push(
/* @__PURE__ */ jsx(
"meta",
{
property: `og:${mediaType}:alt`,
content: medium.alt
},
`og:${mediaType}:alt0${index}`
)
);
}
if (medium.secureUrl) {
tags.push(
/* @__PURE__ */ jsx(
"meta",
{
property: `og:${mediaType}:secure_url`,
content: medium.secureUrl.toString()
},
`og:${mediaType}:secure_url0${index}`
)
);
}
if (medium.type) {
tags.push(
/* @__PURE__ */ jsx(
"meta",
{
property: `og:${mediaType}:type`,
content: medium.type.toString()
},
`og:${mediaType}:type0${index}`
)
);
}
if (medium.width) {
tags.push(
/* @__PURE__ */ jsx(
"meta",
{
property: `og:${mediaType}:width`,
content: medium.width.toString()
},
`og:${mediaType}:width0${index}`
)
);
} else if (defaultWidth) {
tags.push(
/* @__PURE__ */ jsx(
"meta",
{
property: `og:${mediaType}:width`,
content: defaultWidth.toString()
},
`og:${mediaType}:width0${index}`
)
);
}
if (medium.height) {
tags.push(
/* @__PURE__ */ jsx(
"meta",
{
property: `og:${mediaType}:height`,
content: medium.height.toString()
},
`og:${mediaType}:height${index}`
)
);
} else if (defaultHeight) {
tags.push(
/* @__PURE__ */ jsx(
"meta",
{
property: `og:${mediaType}:height`,
content: defaultHeight.toString()
},
`og:${mediaType}:height${index}`
)
);
}
return tags;
}, []);
};
var generateSeoTags = (config) => {
const tagsToRender = [];
if (config.titleTemplate) {
defaults.templateTitle = config.titleTemplate;
}
let updatedTitle = "";
if (config.title) {
updatedTitle = config.title;
if (defaults.templateTitle) {
updatedTitle = defaults.templateTitle.replace(/%s/g, () => updatedTitle);
}
} else if (config.defaultTitle) {
updatedTitle = config.defaultTitle;
}
if (updatedTitle) {
tagsToRender.push(/* @__PURE__ */ jsx("title", { children: updatedTitle }, "title"));
}
const noindex = config.noindex === void 0 ? defaults.noindex || config.dangerouslySetAllPagesToNoIndex : config.noindex;
const nofollow = config.nofollow === void 0 ? defaults.nofollow || config.dangerouslySetAllPagesToNoFollow : config.nofollow;
const norobots = config.norobots || defaults.norobots;
let robotsParams = "";
if (config.robotsProps) {
const {
nosnippet,
maxSnippet,
maxImagePreview,
maxVideoPreview,
noarchive,
noimageindex,
notranslate,
unavailableAfter
} = config.robotsProps;
robotsParams = `${nosnippet ? ",nosnippet" : ""}${maxSnippet ? `,max-snippet:${maxSnippet}` : ""}${maxImagePreview ? `,max-image-preview:${maxImagePreview}` : ""}${noarchive ? ",noarchive" : ""}${unavailableAfter ? `,unavailable_after:${unavailableAfter}` : ""}${noimageindex ? ",noimageindex" : ""}${maxVideoPreview ? `,max-video-preview:${maxVideoPreview}` : ""}${notranslate ? ",notranslate" : ""}`;
}
if (config.norobots) {
defaults.norobots = true;
}
if (noindex || nofollow) {
if (config.dangerouslySetAllPagesToNoIndex) {
defaults.noindex = true;
}
if (config.dangerouslySetAllPagesToNoFollow) {
defaults.nofollow = true;
}
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
name: "robots",
content: `${noindex ? "noindex" : "index"},${nofollow ? "nofollow" : "follow"}${robotsParams}`
},
"robots"
)
);
} else if (!norobots || robotsParams) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
name: "robots",
content: `index,follow${robotsParams}`
},
"robots"
)
);
}
if (config.description) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
name: "description",
content: config.description
},
"description"
)
);
}
if (config.themeColor) {
tagsToRender.push(
/* @__PURE__ */ jsx("meta", { name: "theme-color", content: config.themeColor }, "theme-color")
);
}
if (config.mobileAlternate) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"link",
{
rel: "alternate",
media: config.mobileAlternate.media,
href: config.mobileAlternate.href
},
"mobileAlternate"
)
);
}
if (config.languageAlternates && config.languageAlternates.length > 0) {
config.languageAlternates.forEach((languageAlternate) => {
tagsToRender.push(
/* @__PURE__ */ jsx(
"link",
{
rel: "alternate",
hrefLang: languageAlternate.hrefLang,
href: languageAlternate.href
},
`languageAlternate-${languageAlternate.hrefLang}`
)
);
});
}
if (config.twitter) {
if (config.twitter.cardType) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
name: "twitter:card",
content: config.twitter.cardType
},
"twitter:card"
)
);
}
if (config.twitter.site) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
name: "twitter:site",
content: config.twitter.site
},
"twitter:site"
)
);
}
if (config.twitter.handle) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
name: "twitter:creator",
content: config.twitter.handle
},
"twitter:creator"
)
);
}
}
if (config.facebook) {
if (config.facebook.appId) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "fb:app_id",
content: config.facebook.appId
},
"fb:app_id"
)
);
}
}
if (config.openGraph?.title || updatedTitle) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "og:title",
content: config.openGraph?.title || updatedTitle
},
"og:title"
)
);
}
if (config.openGraph?.description || config.description) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "og:description",
content: config.openGraph?.description || config.description
},
"og:description"
)
);
}
if (config.openGraph) {
if (config.openGraph.url || config.canonical) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "og:url",
content: config.openGraph.url || config.canonical
},
"og:url"
)
);
}
if (config.openGraph.type) {
const type = config.openGraph.type.toLowerCase();
tagsToRender.push(
/* @__PURE__ */ jsx("meta", { property: "og:type", content: type }, "og:type")
);
if (type === "profile" && config.openGraph.profile) {
if (config.openGraph.profile.firstName) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "profile:first_name",
content: config.openGraph.profile.firstName
},
"profile:first_name"
)
);
}
if (config.openGraph.profile.lastName) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "profile:last_name",
content: config.openGraph.profile.lastName
},
"profile:last_name"
)
);
}
if (config.openGraph.profile.username) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "profile:username",
content: config.openGraph.profile.username
},
"profile:username"
)
);
}
if (config.openGraph.profile.gender) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "profile:gender",
content: config.openGraph.profile.gender
},
"profile:gender"
)
);
}
} else if (type === "book" && config.openGraph.book) {
if (config.openGraph.book.authors && config.openGraph.book.authors.length) {
config.openGraph.book.authors.forEach((author, index) => {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "book:author",
content: author
},
`book:author:0${index}`
)
);
});
}
if (config.openGraph.book.isbn) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "book:isbn",
content: config.openGraph.book.isbn
},
"book:isbn"
)
);
}
if (config.openGraph.book.releaseDate) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "book:release_date",
content: config.openGraph.book.releaseDate
},
"book:release_date"
)
);
}
if (config.openGraph.book.tags && config.openGraph.book.tags.length) {
config.openGraph.book.tags.forEach((tag, index) => {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "book:tag",
content: tag
},
`book:tag:0${index}`
)
);
});
}
} else if (type === "article" && config.openGraph.article) {
if (config.openGraph.article.publishedTime) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "article:published_time",
content: config.openGraph.article.publishedTime
},
"article:published_time"
)
);
}
if (config.openGraph.article.modifiedTime) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "article:modified_time",
content: config.openGraph.article.modifiedTime
},
"article:modified_time"
)
);
}
if (config.openGraph.article.expirationTime) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "article:expiration_time",
content: config.openGraph.article.expirationTime
},
"article:expiration_time"
)
);
}
if (config.openGraph.article.authors && config.openGraph.article.authors.length) {
config.openGraph.article.authors.forEach((author, index) => {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "article:author",
content: author
},
`article:author:0${index}`
)
);
});
}
if (config.openGraph.article.section) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "article:section",
content: config.openGraph.article.section
},
"article:section"
)
);
}
if (config.openGraph.article.tags && config.openGraph.article.tags.length) {
config.openGraph.article.tags.forEach((tag, index) => {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "article:tag",
content: tag
},
`article:tag:0${index}`
)
);
});
}
} else if ((type === "video.movie" || type === "video.episode" || type === "video.tv_show" || type === "video.other") && config.openGraph.video) {
if (config.openGraph.video.actors && config.openGraph.video.actors.length) {
config.openGraph.video.actors.forEach((actor, index) => {
if (actor.profile) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "video:actor",
content: actor.profile
},
`video:actor:0${index}`
)
);
}
if (actor.role) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "video:actor:role",
content: actor.role
},
`video:actor:role:0${index}`
)
);
}
});
}
if (config.openGraph.video.directors && config.openGraph.video.directors.length) {
config.openGraph.video.directors.forEach((director, index) => {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "video:director",
content: director
},
`video:director:0${index}`
)
);
});
}
if (config.openGraph.video.writers && config.openGraph.video.writers.length) {
config.openGraph.video.writers.forEach((writer, index) => {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "video:writer",
content: writer
},
`video:writer:0${index}`
)
);
});
}
if (config.openGraph.video.duration) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "video:duration",
content: config.openGraph.video.duration.toString()
},
"video:duration"
)
);
}
if (config.openGraph.video.releaseDate) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "video:release_date",
content: config.openGraph.video.releaseDate
},
"video:release_date"
)
);
}
if (config.openGraph.video.tags && config.openGraph.video.tags.length) {
config.openGraph.video.tags.forEach((tag, index) => {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "video:tag",
content: tag
},
`video:tag:0${index}`
)
);
});
}
if (config.openGraph.video.series) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "video:series",
content: config.openGraph.video.series
},
"video:series"
)
);
}
}
}
if (config.defaultOpenGraphImageWidth) {
defaults.defaultOpenGraphImageWidth = config.defaultOpenGraphImageWidth;
}
if (config.defaultOpenGraphImageHeight) {
defaults.defaultOpenGraphImageHeight = config.defaultOpenGraphImageHeight;
}
if (config.openGraph.images && config.openGraph.images.length) {
tagsToRender.push(
...buildOpenGraphMediaTags("image", config.openGraph.images, {
defaultWidth: defaults.defaultOpenGraphImageWidth,
defaultHeight: defaults.defaultOpenGraphImageHeight
})
);
}
if (config.defaultOpenGraphVideoWidth) {
defaults.defaultOpenGraphVideoWidth = config.defaultOpenGraphVideoWidth;
}
if (config.defaultOpenGraphVideoHeight) {
defaults.defaultOpenGraphVideoHeight = config.defaultOpenGraphVideoHeight;
}
if (config.openGraph.videos && config.openGraph.videos.length) {
tagsToRender.push(
...buildOpenGraphMediaTags("video", config.openGraph.videos, {
defaultWidth: defaults.defaultOpenGraphVideoWidth,
defaultHeight: defaults.defaultOpenGraphVideoHeight
})
);
}
if (config.openGraph.audio) {
tagsToRender.push(
...buildOpenGraphMediaTags("audio", config.openGraph.audio)
);
}
if (config.openGraph.locale) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "og:locale",
content: config.openGraph.locale
},
"og:locale"
)
);
}
if (config.openGraph.siteName || config.openGraph.site_name) {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
property: "og:site_name",
content: config.openGraph.siteName || config.openGraph.site_name
},
"og:site_name"
)
);
}
}
if (config.canonical) {
tagsToRender.push(
/* @__PURE__ */ jsx("link", { rel: "canonical", href: config.canonical }, "canonical")
);
}
if (config.additionalMetaTags && config.additionalMetaTags.length > 0) {
config.additionalMetaTags.forEach(({ keyOverride, ...tag }) => {
tagsToRender.push(
/* @__PURE__ */ jsx(
"meta",
{
...tag
},
`meta:${keyOverride ?? tag.name ?? tag.property ?? tag.httpEquiv}`
)
);
});
}
if (config.additionalLinkTags?.length) {
config.additionalLinkTags.forEach((tag) => {
const { crossOrigin: tagCrossOrigin, ...rest } = tag;
const crossOrigin = tagCrossOrigin === "anonymous" || tagCrossOrigin === "use-credentials" || tagCrossOrigin === "" ? tagCrossOrigin : void 0;
tagsToRender.push(
/* @__PURE__ */ jsx(
"link",
{
...rest,
crossOrigin
},
`link${rest.keyOverride ?? rest.href}${rest.rel}`
)
);
});
}
return tagsToRender;
};
function generateNextSeo(props) {
return generateSeoTags(props);
}
function generateDefaultSeo(props) {
const {
title,
titleTemplate,
defaultTitle,
themeColor,
dangerouslySetAllPagesToNoIndex = false,
dangerouslySetAllPagesToNoFollow = false,
description,
canonical,
facebook,
openGraph,
additionalMetaTags,
twitter,
defaultOpenGraphImageWidth,
defaultOpenGraphImageHeight,
defaultOpenGraphVideoWidth,
defaultOpenGraphVideoHeight,
mobileAlternate,
languageAlternates,
additionalLinkTags,
robotsProps,
norobots
} = props;
return generateSeoTags({
title,
titleTemplate,
defaultTitle,
themeColor,
dangerouslySetAllPagesToNoIndex,
dangerouslySetAllPagesToNoFollow,
description,
canonical,
facebook,
openGraph,
additionalMetaTags,
twitter,
defaultOpenGraphImageWidth,
defaultOpenGraphImageHeight,
defaultOpenGraphVideoWidth,
defaultOpenGraphVideoHeight,
mobileAlternate,
languageAlternates,
additionalLinkTags,
robotsProps,
norobots
});
}
export {
generateDefaultSeo,
generateNextSeo
};
//# sourceMappingURL=pages.mjs.map