@astrolib/seo

中文文档
1.0.0-beta.8发布于 2024/10/04MITTypeScriptES Module零依赖近 30 天下载 6.3万

Managing your SEO easier in Astro projects

原始使用文档
文档目录4 节
  1. Usage
  2. Setup
  3. Add SEO to Page
  4. Options

Astro SEO - @astrolib/seo

Astro SEO is an integration that makes managing your SEO easier in Astro projects. It is fully based on the excellent Next SEO library

Usage

Astro SEO works by including it on pages where you would like SEO attributes to be added. Once included on the page you pass it a configuration object with the page's SEO properties. This can be dynamically generated at a page level or in some cases your API may return an SEO object.

Setup

First, install it:

npm install @astrolib/seo

or

yarn add @astrolib/seo

Add SEO to Page

Then you need to import @astrolib/seo and add the desired properties. This will render out the tags in the <head> for SEO. At a bare minimum, you should add a title and description.

Example with just title and description:

---
import { AstroSeo } from '@astrolib/seo';
---

<AstroSeo
  title="Simple Usage Example"
  description="A short description goes here."
/>

But Astro SEO gives you many more options that you can add. See below for a typical example of a page.

Typical page example:

---
import { AstroSeo } from '@astrolib/seo';
---

<AstroSeo
  title="Using More of Config"
  description="This example uses more of the available config options."
  canonical="https://www.canonical.ie/"
  openGraph={{
    url: 'https://www.url.ie/a',
    title: 'Open Graph Title',
    description: 'Open Graph Description',
    images: [
      {
        url: 'https://www.example.ie/og-image-01.jpg',
        width: 800,
        height: 600,
        alt: 'Og Image Alt',
        type: 'image/jpeg',
      },
      {
        url: 'https://www.example.ie/og-image-02.jpg',
        width: 900,
        height: 800,
        alt: 'Og Image Alt Second',
        type: 'image/jpeg',
      },
      { url: 'https://www.example.ie/og-image-03.jpg' },
      { url: 'https://www.example.ie/og-image-04.jpg' },
    ],
    site_name: 'SiteName',
  }}
  twitter={{
    handle: '@handle',
    site: '@site',
    cardType: 'summary_large_image',
  }}
/>

Options

PropertyTypeDescription
titleTemplatestringAllows you to set default title template that will be added to your title
titlestringSet the meta title of the page
noindexbooleanSets whether page should be indexed or not
nofollowbooleanSets whether page should be followed or not
robotsPropsObjectSet the more meta information for the X-Robots-Tag
descriptionstringSet the page meta description
canonicalstringSet the page canonical url
mobileAlternate.mediastringSet what screen size the mobile website should be served from
mobileAlternate.hrefstringSet the mobile page alternate url
languageAlternatesarraySet the language of the alternate urls. Expects array of objects with the shape: { hrefLang: string, href: string }
additionalMetaTagsarrayAllows you to add a meta tag that is not documented here.
additionalLinkTagsarrayAllows you to add a link tag that is not documented here.
twitter.cardTypestringThe card type, which will be one of summary, summary_large_image, app, or player
twitter.sitestring@username for the website used in the card footer
twitter.handlestring@username for the content creator / author (outputs as twitter:creator)
facebook.appIdstringUsed for Facebook Insights, you must add a facebook app ID to your page to for it
openGraph.urlstringThe canonical URL of your object that will be used as its permanent ID in the graph
openGraph.typestringThe type of your object. Depending on the type you specify, other properties may also be required
openGraph.titlestringThe open graph title, this can be different than your meta title.
openGraph.descriptionstringThe open graph description, this can be different than your meta description.
openGraph.imagesarrayAn array of images (object) to be used by social media platforms, slack etc as a preview. If multiple supplied you can choose one when sharing.
openGraph.videosarrayAn array of videos (object)
openGraph.localestringThe locale the open graph tags are marked up in. Of the format language_TERRITORY. Default is en_US.
openGraph.site_namestringIf your object is part of a larger web site, the name which should be displayed for the overall site.
openGraph.profile.firstNamestringPerson's first name.
openGraph.profile.lastNamestringPerson's last name.
openGraph.profile.usernamestringPerson's username.
openGraph.profile.genderstringPerson's gender.
openGraph.book.authorsstring[]Writers of the article.
openGraph.book.isbnstringThe ISBN
openGraph.book.releaseDatedatetimeThe date the book was released.
openGraph.book.tagsstring[]Tag words associated with this book.
openGraph.article.publishedTimedatetimeWhen the article was first published.
openGraph.article.modifiedTimedatetimeWhen the article was last changed.
openGraph.article.expirationTimedatetimeWhen the article is out of date after.
openGraph.article.authorsstring[]Writers of the article.
openGraph.article.sectionstringA high-level section name. E.g. Technology
openGraph.article.tagsstring[]Tag words associated with this article.

版本与发布记录

@astrolib/seo 共发布 27 个版本,首次发布于 2022/09/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0-beta.8latest2024/10/042 年前
1.0.0-beta.72024/10/042 年前
1.0.0-beta.62024/09/172 年前
1.0.0-beta.52023/12/092 年前
1.0.0-beta.4beta2023/08/233 年前
1.0.0-beta.32023/08/233 年前

关于 @astrolib/seo 的常见问题

@astrolib/seo 是什么?

Managing your SEO easier in Astro projects

如何安装 @astrolib/seo?

在项目目录运行 npm install @astrolib/seo,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:astro(^1.2.1 || ^2.0.0 || ^3.0.0-beta.0 || ^3.0.0 || ^4.0.0 || ^5.0.0-beta.0 || ^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@astrolib/seo 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@astrolib/seo 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@astrolib/seo 可以商用吗?

@astrolib/seo 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@astrolib/seo 的下载量有多少?

根据 npm 官方下载统计,@astrolib/seo 在 2026/10/07 – 2026/11/05 这 30 天内被下载 63,169 次,日均约 2,106 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@astrolib/seo 还在维护吗?

最新版本 1.0.0-beta.8 发布于 2024/10/04(2 年前);至今共发布 27 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/09/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。