astro-sitemap
Advanced tools
Comparing version 0.2.7 to 0.2.8
@@ -33,3 +33,3 @@ import type { AstroIntegration } from 'astro'; | ||
} | undefined; | ||
declare const createPlugin: (options?: SitemapOptions) => AstroIntegration; | ||
export default createPlugin; | ||
declare const createSitemapIntegration: (options?: SitemapOptions) => AstroIntegration; | ||
export default createSitemapIntegration; |
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
// src/index.ts | ||
@@ -345,3 +343,3 @@ import path from "path"; | ||
} | ||
var createPlugin = (options) => { | ||
var createSitemapIntegration = (options) => { | ||
let config; | ||
@@ -371,7 +369,7 @@ return { | ||
let pages = srcPages.map(({ pathname }) => pathname); | ||
if (exclude && exclude.length > 0) { | ||
if (exclude == null ? void 0 : exclude.length) { | ||
try { | ||
pages = excludeRoutes(exclude, pages); | ||
} catch (err) { | ||
logger.error(["Error exclude pages", getErrorMessage(err)]); | ||
logger.error(["Page exclusion error", getErrorMessage(err)]); | ||
return; | ||
@@ -384,3 +382,3 @@ } | ||
} catch (err) { | ||
logger.error(["Error filtering pages", getErrorMessage(err)]); | ||
logger.error(["Page filtering error", getErrorMessage(err)]); | ||
return; | ||
@@ -398,8 +396,6 @@ } | ||
if (typeof config.adapter !== "undefined") { | ||
logger.warn([ | ||
"No pages found!", | ||
'`We can only detect sitemap routes for "static" projects. Since you are using an SSR adapter, we recommend manually listing your sitemap routes using the "customPages" integration option.', | ||
"", | ||
"Example: `sitemap({ customPages: ['https://example.com/route'] })`" | ||
]); | ||
logger.warn(`No pages found! | ||
We can only detect sitemap routes for "static" projects. Since you are using an SSR adapter, we recommend manually listing your sitemap routes using the "customPages" integration option. | ||
Example: \`sitemap({ customPages: ['https://example.com/route'] })\``); | ||
} else { | ||
@@ -426,3 +422,3 @@ logger.warn("No pages found!"); | ||
} catch (err) { | ||
logger.error(["Error serializing pages", getErrorMessage(err)]); | ||
logger.error(["Page serialization error", getErrorMessage(err)]); | ||
return; | ||
@@ -458,5 +454,5 @@ } | ||
}; | ||
var src_default = createPlugin; | ||
var src_default = createSitemapIntegration; | ||
export { | ||
src_default as default | ||
}; |
{ | ||
"name": "astro-sitemap", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"description": "Generate a sitemap for Astro with more control", | ||
@@ -39,13 +39,13 @@ "keywords": [ | ||
"sitemap": "^7.1.1", | ||
"zod": "^3.17.3" | ||
"zod": "^3.17.10" | ||
}, | ||
"devDependencies": { | ||
"@types/minimatch": "^3.0.5", | ||
"@types/node": "^18.0.0", | ||
"astro": "^1.0.0-beta.54", | ||
"@types/node": "^18.0.6", | ||
"astro": "^1.0.0-beta.73", | ||
"at-scripts": "0.0.4", | ||
"c8": "^7.11.3", | ||
"c8": "^7.12.0", | ||
"typescript": "^4.7.4", | ||
"vite": "^2.9.12", | ||
"vitest": "^0.16.0" | ||
"vite": "^3.0.2", | ||
"vitest": "^0.18.1" | ||
}, | ||
@@ -52,0 +52,0 @@ "publishConfig": { |
748
README.md
@@ -9,32 +9,45 @@ [![Help Ukraine now!](https://raw.githubusercontent.com/alextim/help-ukraine-win-flag/master/stop-russian-agressian-help-ukraine-now-link.svg 'Help Ukraine now!')](https://bank.gov.ua/en/about/support-the-armed-forces) | ||
- <strong>[Why astro-sitemap](#why-astro-sitemap)</strong> | ||
- <strong>[Installation](#installation)</strong> | ||
- <strong>[Usage](#usage)</strong> | ||
- <strong>[Configuration](#configuration)</strong> | ||
- <strong>[Localization](#localization)</strong> | ||
- <strong>[External config file](#external-config-file)</strong> | ||
- <strong>[Examples](#examples)</strong> | ||
- <strong>[Contributing](#contributing)</strong> | ||
- <strong>[Changelog](#changelog)</strong> | ||
- <strong>[Inspirations](#inspirations)</strong> | ||
--- | ||
## Why astro-sitemap? | ||
The _sitemap.xml_ file provides information about structure of your website, about its content: pages, images, videos and relations between them. [See Google's advice on sitemap](https://developers.google.com/search/docs/advanced/sitemaps/overview) to learn more. | ||
## Why astro-sitemap? | ||
The **astro-sitemap** integration does everything the official [@astrojs/sitemap](https://github.com/withastro/astro/tree/main/packages/integrations/sitemap) integration does but much more. | ||
Some of functionality from **astro-sitemap v0.2.2** became an update for the official integration [@astrojs/sitemap](https://github.com/withastro/astro/tree/main/packages/integrations/sitemap) from v0.1.2 to v0.2.0. | ||
Advantages of **astro-sitemap** over [@astrojs/sitemap](https://github.com/withastro/astro/tree/main/packages/integrations/sitemap): | ||
From now you can use the official integration in most cases. | ||
- Exclude pages from a sitemap by glob patterns. | ||
- More control on the sitemap output: | ||
- manage XML namespaces; | ||
- `lastmod` format option; | ||
- possibility to add a link to custom XSL. | ||
- Automatically creates a link to sitemap in `<head>` section of generated pages. | ||
- Flexible configuration: configure the integration with an external config, `astro.config.*` or a combination of both. | ||
- Better logging. | ||
Part of the functionality from **astro-sitemap** became an update for the official integration [@astrojs/sitemap](https://github.com/withastro/astro/tree/main/packages/integrations/sitemap) from v0.1.2 to v0.2.0. | ||
Shared functionality with the official **@astrojs/sitemap**: | ||
- Split up your large sitemap into multiple sitemaps by custom limit. | ||
- Split up your large sitemap into multiple sitemaps by a custom limit. | ||
- Ability to add sitemap specific attributes such as `changefreq`, `lastmod`, `priority`. | ||
- Final output customization via JS function (sync or async). | ||
- The most important: localization support. In a build time the integration analyses the pages urls for presence of locale signatures in paths to establish relations between pages. | ||
- Localization support. In a build time the integration analyses the pages urls for presence of locale signatures in paths to establish relations between pages. | ||
- Reliability: all config options are validated. | ||
**astro-sitemap** key extras: | ||
:exclamation: Both official and **astro-sitemap** integrations don't support SSR. | ||
- Exclude pages from sitemap by glob patterns. | ||
- More control on sitemap output: | ||
- manage xml namespaces; | ||
- `lastmod` format option; | ||
- possibility to add a link to custom xsl. | ||
- Automatically creates a link to sitemap in `<head>` section of generated pages. | ||
- Flexible configuration: configure the integration with external config, astro.config or combine both. | ||
:exclamation: This integration uses `astro:build:done` hook (the official [@astrojs/sitemap](https://github.com/withastro/astro/tree/main/packages/integrations/sitemap) does the same). This hook exposes only generated page paths. Thus, in the current version of Astro, both integrations don't have the ability to analyze the page source, frontmatter, etc. They can add `changefreq`, `lastmod` and `priority` attributes only in a batch or nothing. | ||
:exclamation: Both integrations, the official and **astro-sitemap** don't support SSR. | ||
--- | ||
@@ -44,8 +57,7 @@ | ||
There are two ways to add **astro-sitemap** integration to your Astro project. | ||
<details> | ||
<summary>Quick Install</summary> | ||
### Astro CLI tool | ||
The experimental `astro add` command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren't sure which package manager you're using, run the first command.) Then, follow the prompts, and type "y" in the terminal (meaning "yes") for each one. | ||
You should run `astro add` command in your project directory. This command after prompt will install required dependencies and apply changes to _astro.config.\*_. | ||
```sh | ||
@@ -62,33 +74,44 @@ # Using NPM | ||
If you run into any troubles, [feel free to log an issue on my GitHub](https://github.com/alextim/astro-lib/issues). | ||
Then, restart the dev server by typing `CTRL-C` and then `npm run astro dev` in the terminal window that was running Astro. | ||
Because this command is new, it might not properly set things up. If that happens, [log an issue on Astro GitHub](https://github.com/withastro/astro/issues) and try the manual installation steps below. | ||
### Install dependencies manually | ||
</details> | ||
First, install the **astro-sitemap** integration like so: | ||
<details> | ||
<summary>Manual Install</summary> | ||
First, install the `astro-sitemap` package using your package manager. If you're using npm or aren't sure, run this in the terminal: | ||
```sh | ||
# Using NPM | ||
npm install --save-dev astro-sitemap | ||
``` | ||
# Using Yarn | ||
yarn add -D astro-sitemap | ||
Then, apply this integration to your `astro.config.*` file using the `integrations` property: | ||
# Using PNPM | ||
pnpm add -D astro-sitemap | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
// ... | ||
integrations: [sitemap()], | ||
} | ||
``` | ||
Then, restart the dev server. | ||
</details> | ||
Then apply this integration to your _astro.config.\*_. All details below in **Getting started**. | ||
## Usage | ||
## Getting started | ||
The `astro-sitemap` integration requires a deployment / site URL for generation. Add your site's URL under your _astro.config.\*_ using the `site` property. | ||
:exclamation: Provide the `experimental` property to your _astro.config.\*_, because only official **@astrojs/\*** integrations are currently supported by Astro. Set the `experimental.integrations` value to `true`. | ||
:exclamation: Provide the `experimental` property to your _astro.config.\*_, because only official **@astrojs/\*** integrations are currently supported by Astro. Set the `experimental.integrations` value to `true` or use the `--experimental-integrations` flag for build command. | ||
Then, apply this integration to your _astro.config.\*_ file using the `integrations` property. | ||
**astro.config.mjs** | ||
__`astro.config.mjs`__ | ||
```js | ||
// astro.config.mjs | ||
import { defineConfig } from 'astro/config'; | ||
@@ -113,5 +136,6 @@ import sitemap from 'astro-sitemap'; | ||
Generated sitemap content for two pages website: | ||
<details> | ||
<summary>Example of generated sitemap content</summary> | ||
**sitemap-index.xml** | ||
**`sitemap-index.xml`** | ||
@@ -127,3 +151,3 @@ ```xml | ||
**sitemap-0.xml** | ||
**`sitemap-0.xml`** | ||
@@ -142,4 +166,9 @@ ```xml | ||
All pages generated during build will contain in `<head>` section a link to sitemap: | ||
</details> | ||
<details> | ||
<summary>Example of inserted HTML</summary> | ||
All pages generated at build time will contain a link to the sitemap in the `<head>` section: | ||
```html | ||
@@ -149,29 +178,266 @@ <link rel="sitemap" type="application/xml" href="/sitemap-index.xml"> | ||
You can also check [Astro Integration Documentation](https://docs.astro.build/en/guides/integrations-guide/) for more on integrations. | ||
</details> | ||
## Configuration | ||
### Options shared with the official integration | ||
To configure this integration, pass an object to the `sitemap()` function call in `astro.config.mjs`. | ||
| Name | Type | Required | Default | Description | | ||
| :------------: | :------------------------: | :------: | :-----: | :---------------------------------------------------------------------------------------------- | | ||
| `filter` | `(page: String):`<br/>`Boolean`| No | | Function to filter generated pages to exclude some paths from a sitemap. | | ||
| `customPages` | `String[]` | No | | Absolute URL list. It will be merged with generated pages urls.<br/>You should also use `customPages` to manually list sitemap pages when using an SSR adapter. Currently, integration cannot detect your site's pages unless you are building statically. To avoid an empty sitemap, list all pages (including the base origin) with this configuration option! | | ||
| `canonicalURL` | `String` | No | | Absolute URL. The integration needs `site` from astro.config or `canonicalURL`. If both values are provided then only `canonicalURL` will be used by the integration. | | ||
| `entryLimit` | `Number` | No | 45000 | Number of entries per sitemap file, a sitemap index and multiple sitemaps are created if you have more entries. See more on [Google](https://developers.google.com/search/docs/advanced/sitemaps/large-sitemaps/) | | ||
| `changefreq` | `ChangeFreq` | No | | Sitemap specific. Ignored by Google.<br/>How frequently the page is likely to change.<br/>Available values: `always`\|`hourly`\|`daily`\|`weekly`\|`monthly`\| `yearly`\|`never` | | ||
| `lastmod` | `Date` | No | | Sitemap specific. The date of page last modification. | | ||
| `priority` | `Number` | No | | Sitemap specific. Ignored by Google.<br/>The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0 | | ||
| `serialize` | `(item: SitemapItem):`<br>`SitemapItemLoose`\|`undefined`\|<br/>Promise<`SitemapItemLoose`\|`undefined`>| No | | Function to process an array of sitemap entries just before writing them to disk. Async or sync.<br/>**Modified behavior** compared to the official integration: `undefined` return value excludes the passed entry from sitemap. | | ||
__`astro.config.mjs`__ | ||
| **i18n** | `object` | No | | | | ||
| `defaultLocale`| `String` | Yes | | Its value has to be exist as one the `locales` keys. | | ||
| `locales` | `Record<String, String>` | Yes | | Key/value - pairs.<br/>The key is used to look for a locale part in a page path.<br/> The value is a language attribute, only English alphabet and hyphen allowed. See more on [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang) | | ||
```js | ||
... | ||
export default defineConfig({ | ||
integrations: [sitemap({ | ||
filter: ... | ||
})] | ||
}); | ||
``` | ||
:bulb: See detailed explanation of sitemap specific options on [sitemap.org](https://www.sitemaps.org/protocol.html). | ||
<details> | ||
<summary><strong>canonicalURL</strong></summary> | ||
:exclamation: This integration uses 'astro:build:done' hook (official @astrojs/sitemap does the same). The hook exposes only generated page paths. So with present version of Astro the integration has no abilities to analyze a page source, frontmatter etc. The integration can add `changefreq`, `lastmod` and `priority` attributes only in a batch or nothing. | ||
| Type | Required | Default value | | ||
| :-----: | :------: | :-------------: | | ||
| `String`| No | `undefined` | | ||
#### SitemapItem | ||
Absolute URL. The integration needs `canonicalURL` or `site` from astro.config. If both values are provided, only `canonicalURL` will be used by the integration. | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
canonicalURL: 'https://another-domain.com', | ||
})], | ||
}; | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>filter</strong></summary> | ||
| Type | Required | Default value | | ||
| :-------------------------: | :------: | :-------------: | | ||
| (page: `String`): `Boolean` | No | `undefined` | | ||
Function to filter generated pages to exclude some paths from the sitemap. | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
filter(page) { | ||
return !/exclude-this/.test(page); | ||
}, | ||
})], | ||
}; | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>exclude</strong></summary> | ||
| Type | Required | Default value | | ||
| :--------: | :------: | :-------------: | | ||
| `String[]` | No | `undefined` | | ||
The `exclude` option is an array of [glob patterns](https://github.com/isaacs/minimatch#features) to exclude static routes from the generated sitemap. | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
exclude: ['404', 'blog-*/'], | ||
})], | ||
}; | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>customPages</strong></summary> | ||
| Type | Required | Default value | | ||
| :--------: | :------: | :-------------: | | ||
| `String[]` | No | `undefined` | | ||
Absolute URL list. It will be merged with generated pages urls. | ||
You should also use `customPages` to manually list sitemap pages when using an SSR adapter. Currently, integration cannot detect your site's pages unless you are building statically. To avoid an empty sitemap, list all pages (including the base origin) with this configuration option. | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
customPages: [ | ||
'https://example.com/virtual-one.html', | ||
'https://example.com/virtual-two.html', | ||
], | ||
})], | ||
}; | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>entryLimit</strong></summary> | ||
| Type | Required | Default value | | ||
| :------: | :------: | :-------------: | | ||
| `Number` | No | 45000 | | ||
Number of entries per one sitemap file. | ||
The integration creates a separate `sitemap-${i}.xml` file for each batch of 45000 and adds this file to index - `sitemap-index.xml`. | ||
See more on [Google](https://developers.google.com/search/docs/advanced/sitemaps/large-sitemaps/). | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
entryLimit: 10000, | ||
})], | ||
}; | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>changefreq</strong></summary> | ||
| Type | Required | Default value | | ||
| :----------: | :------: | :-------------: | | ||
| `ChangeFreq` | No | `undefined` | | ||
Sitemap specific. How frequently the page is likely to change. Ignored by Google. | ||
Available values: `always` | `hourly` | `daily` | `weekly` | `monthly` | `yearly` | `never`. | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
changefreq: 'weekly', | ||
})], | ||
}; | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>lastmod</strong></summary> | ||
| Type | Required | Default value | | ||
| :----------: | :------: | :-------------: | | ||
| `Date` | No | `undefined` | | ||
Sitemap specific. The date of page last modification. | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
lastmod: Date(), | ||
})], | ||
}; | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>priority</strong></summary> | ||
| Type | Required | Default value | | ||
| :----------: | :------: | :-------------: | | ||
| `Number` | No | `undefined` | | ||
Sitemap specific. The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. | ||
Ignored by Google. | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
priority: 0.9, | ||
})], | ||
}; | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>serialize</strong></summary> | ||
| Type | Required | Default value | | ||
| :----------: | :------: | :-------------: | | ||
| (item: `SitemapItem`): `SitemapItemLoose` | `undefined` |<br/>Promise<`SitemapItemLoose` | `undefined`> | No | `undefined` | | ||
Function to process an array of sitemap entries just before writing them to disk. Async or sync. | ||
The `undefined` return value excludes the passed entry from the sitemap. | ||
<details> | ||
<summary><strong>Type `SitemapItem`*</strong></summary> | ||
| Name | Type | Required | Description | | ||
@@ -184,22 +450,100 @@ | :------------: | :----------: | :------: | :----------------- | | ||
| `links` | `LinkItem[]` | No | for localization | | ||
</details> | ||
#### LinkItem | ||
<details> | ||
<summary><strong>Type `LinkItem`</strong></summary> | ||
| Name | Type | Required | Description | | ||
| :------------: | :-------------: | :------: | :------------------------ | | ||
| `url` | `String` | Yes | Absolute url | | ||
| `url` | `String` | Yes | Absolute URL | | ||
| `lang` | `String` | Yes | hreflag, example: 'en-US' | | ||
</details> | ||
### Extra options | ||
<details> | ||
<summary><strong>Interface `SitemapItemLoose`</strong></summary> | ||
| Name | Type | Required | Default | Description | | ||
| :------------: | :----------: | :------: | :-----: | :---------------------------------------------------------------------------------------------- | | ||
| `exclude` | `String[]` | No | | The `exclude` option is an array of [glob patterns](https://github.com/isaacs/minimatch#features) to exclude static routes from the generated sitemap. | | ||
| `xslUrl` | `String` | No | | Absolute URL of XSL file to style XML or transform it to other format. Ignored by search engines. | | ||
| `xmlns` | `NSArgs` | No | | Set the XML namespaces by xmlns attributes in `<urlset>` element. | | ||
| `lastmodDateOnly` | `Boolean` | No | | If it's `true` the XML output will contain a date part only. | | ||
| `createLinkInHead`| `Boolean` | No | true | Create a link on the sitemap in `<head>` of generated pages.<br/>The final output reprocessing is used for this. It could impact on a build time for large sites. | | ||
The `SitemapItemLoose` interface is a base for the `SitemapItem`. | ||
#### NSArgs | ||
It has the properties `video`, `img` and many more. | ||
More details about `SitemapItemLoose` interface see in the **sitemap.js** repo [readme](https://github.com/ekalinin/sitemap.js/blob/master/README.md) and types source [code](https://github.com/ekalinin/sitemap.js/blob/master/lib/types.ts). | ||
</details> | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
serialize(item) { | ||
if (/exclude-this/.test(item.url)) { | ||
return undefined; | ||
} | ||
if (/special-page/.test(item.url)) { | ||
item.changefreq = 'daily'; | ||
item.lastmod = new Date(); | ||
item.priority = 0.9; | ||
} | ||
return item; | ||
}, | ||
})], | ||
}; | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>xslUrl</strong></summary> | ||
| Type | Required | Default value | | ||
| :----------: | :------: | :-------------: | | ||
| `String` | No | `undefined` | | ||
Absolute URL of XSL file to style XML or transform it to other format. Ignored by search engines. | ||
__`astro.config.mjs`__ | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
xslUrl: 'https://example.com/style.xsl', | ||
})], | ||
}; | ||
``` | ||
**Example of XML output** | ||
```xml | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<?xml-stylesheet type="text/xsl" href="https://example/style.xsl"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> | ||
... | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>xmlns</strong></summary> | ||
| Type | Required | Default value | | ||
| :----------: | :------: | :-------------: | | ||
| `NSArgs` | No | `undefined` | | ||
Set the XML namespaces by xmlns attributes in `<urlset>` element. | ||
<details> | ||
<summary><strong>Type `NSArgs`</strong></summary> | ||
| Name | Type | Required | Default | Description | | ||
@@ -212,147 +556,157 @@ | :------: | :-------: | :------: | :-----: | :----------------------------------------------------------------------- | | ||
| `custom` | `String[]`| No | | Any custom namespace. Elements of array'll be used `as is` without any validation. | | ||
</details> | ||
#### SitemapItemLoose | ||
__`astro.config.mjs`__ | ||
The `SitemapItemLoose` interface is a base for the `SitemapItem`. | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
It has properties `video`, `img` and many others. | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
xmlns: { | ||
xhtml: true, | ||
news: true, | ||
image: true, | ||
video: true, | ||
custom: [ | ||
'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"', | ||
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"', | ||
], | ||
}, | ||
})], | ||
}; | ||
``` | ||
More details about `SitemapItemLoose` interface see in the **sitemap.js** repo [readme](https://github.com/ekalinin/sitemap.js/blob/master/README.md) and types source [code](https://github.com/ekalinin/sitemap.js/blob/master/lib/types.ts). | ||
__Example of XML output__ | ||
**Sample of _astro.config.mjs_** | ||
```xml | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<?xml-stylesheet type="text/xsl" href="https://example/style.xsl"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
... | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>lastmodDateOnly</strong></summary> | ||
| Type | Required | Default value | | ||
| :-----------: | :------: | :-------------: | | ||
| `Boolean` | No | `undefined` | | ||
If its value is `true`, the `lastmod` field in the XML output will contain a date part only. | ||
__`astro.config.mjs`__ | ||
```js | ||
// astro.config.ts | ||
import { defineConfig } from 'astro/config'; | ||
import sitemap from 'astro-sitemap'; | ||
export default defineConfig({ | ||
site: 'https://your-awesome-site.com', | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [ | ||
sitemap({ | ||
/** | ||
* These options are the same with the official integration `@astrojs/sitemap` | ||
*/ | ||
// exclude pages from sitemap | ||
filter: (page: string) => !/exclude-this/.test(page), | ||
// Absolute urls of extra pages | ||
customPages: [ | ||
// extra pages for sitemap | ||
'https://example.com/virtual-one.html', | ||
'https://example.com/virtual-two.html', | ||
], | ||
integrations: [sitemap({ | ||
lastmodDateOnly: true, | ||
lastmod: Date(), | ||
})], | ||
}; | ||
``` | ||
// Here the `canonicalURL` is provided. | ||
// It will be used during sitemap generation instead of `site` value. | ||
canonicalURL: 'https://example.com', | ||
</details> | ||
// This function is called just before a sitemap writing to disk. | ||
// You have more control on resulting output. | ||
// sync or async | ||
serialize(item: SitemapItem): SitemapItem { | ||
if (/special-page/.test(item.url)) { | ||
item.changefreq = 'daily'; | ||
item.lastmod = new Date(); | ||
item.priority = 0.9; | ||
} | ||
return item; | ||
}, | ||
<details> | ||
<summary><strong>createLinkInHead</strong></summary> | ||
// The integration creates a separate `sitemap-${i}.xml` file for each batch of 45000 and adds this file to index - `sitemap-index.xml`. | ||
entryLimit: 1000, // default - 45000 | ||
| Type | Required | Default value | | ||
| :-----------: | :------: | :-------------: | | ||
| `Boolean` | No | `true` | | ||
// sitemap specific | ||
changefreq: 'yearly', | ||
lastmod: new Date('May 01, 2019 03:24:00'), | ||
priority: 0.2, | ||
Create a link on the sitemap in `<head>` section of generated pages. | ||
/** | ||
* `astro-sitemap` integration extra options | ||
*/ | ||
exclude: ['404', 'blog-*/'] | ||
// print date not time | ||
lastmodDateOnly: false, | ||
// style to transform to another format, ignored by search engines | ||
xslUrl: 'https://example.com/style.xsl', | ||
The final output reprocessing is used for this. It can impact build time for large sites. | ||
// set the xml namespace | ||
xmlns: { | ||
xhtml: true, | ||
news: true, | ||
image: true, | ||
video: true, | ||
custom: [ | ||
'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"', | ||
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"', | ||
], | ||
}, | ||
__`astro.config.mjs`__ | ||
// Create or not a link to sitemap in '<head>' section of generated pages | ||
createLinkInHead: true, // default - true | ||
}), | ||
], | ||
}); | ||
```js | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
createLinkInHead: false, | ||
})], | ||
}; | ||
``` | ||
Generated sitemap content for this configuration: | ||
</details> | ||
```xml | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<?xml-stylesheet type="text/xsl" href="https://example/style.xsl"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
... | ||
<url> | ||
<loc>https://example.com/</loc> | ||
<lastmod>2019-05-01</lastmod> | ||
<changefreq>yearly</changefreq> | ||
<priority>0.2</priority> | ||
</url> | ||
... | ||
<url> | ||
<loc>https://example.com/virtual-one.html</loc> | ||
<lastmod>2019-05-01</lastmod> | ||
<changefreq>yearly</changefreq> | ||
<priority>0.2</priority> | ||
</url> | ||
... | ||
<url> | ||
<loc>https://example.com/some-special-path.html</loc> | ||
<lastmod>2022-06-07</lastmod> | ||
<changefreq>daily</changefreq> | ||
<priority>0.9</priority> | ||
</url> | ||
... | ||
``` | ||
:bulb: See detailed explanation of sitemap specific options on [sitemap.org](https://www.sitemaps.org/protocol.html). | ||
## Localization | ||
Supply the integration config with the `i18n` options. The integration will check generated page paths on presence of locale keys in paths. | ||
Supply the integration config with the `i18n` option as an object with two required properties: | ||
Read more about localization on Google in [Advanced SEO](https://developers.google.com/search/docs/advanced/crawling/localized-versions#all-method-guidelines). | ||
<details> | ||
<summary><strong>locales</strong></summary> | ||
Let's have the following integration config: | ||
| Type | Required | | ||
| :----------: | :------: | | ||
| `Record<String, String>` | Yes | | ||
Key/value - pairs. The key is used to look up the locale part of the page path. The value is a language attribute, only English alphabet and hyphen allowed. | ||
See more on [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang). | ||
</details> | ||
<details> | ||
<summary><strong>defaultLocale</strong></summary> | ||
| Type | Required | | ||
| :----------: | :------: | | ||
| `String` | Yes | | ||
`defaultLocale` value must exist as one of the `locales` keys. | ||
</details> | ||
__`astro.config.mjs`__ | ||
```js | ||
... | ||
canonicalURL: 'https://example.com', // You could provide the `site` option in astro.config instead of `canonicalURL`. | ||
i18n: { | ||
defaultLocale: 'en', // All urls that don't contain `es` or `fr` after `https://example.com/` will be treated as default locale, i.e. `en` | ||
locales: { | ||
en: 'en-US', // The `defaultLocale` value must present in `locales` keys | ||
es: 'es-ES', | ||
fr: 'fr-CA', | ||
} | ||
} | ||
... | ||
import sitemap from 'astro-sitemap'; | ||
export default { | ||
site: 'https://example.com', | ||
experimental: { | ||
integrations: true, | ||
}, | ||
integrations: [sitemap({ | ||
i18n: { | ||
// All URLs that don't contain `es` or `fr` after `https://example.com/` will be treated as default locale, i.e. `en` | ||
defaultLocale: 'en', | ||
locales: { | ||
en: 'en-US', // The `defaultLocale` value must present in `locales` keys | ||
es: 'es-ES', | ||
fr: 'fr-CA', | ||
}, | ||
}, | ||
})], | ||
}; | ||
``` | ||
The sitemap content will be: | ||
__Example of XML output__ | ||
```xml | ||
... | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> | ||
<url> | ||
@@ -382,10 +736,12 @@ <loc>https://example.com/</loc> | ||
</url> | ||
... | ||
</urlset> | ||
``` | ||
## Using Configuration Files | ||
## External config file | ||
You could configure the integration with external file `sitemap.config.*` (`js`, `cjs`, `mjs`). Put it to the application `root` folder (see about `root` in official [docs](https://docs.astro.build/en/reference/configuration-reference/)). | ||
You can configure the integration using the external file `sitemap.config.*` (`js`, `cjs`, `mjs`). Put it in the application `root` folder (see about `root` in official [docs](https://docs.astro.build/en/reference/configuration-reference/)). | ||
The external config must contain a default export statement: | ||
The external config must contain the default export statement: | ||
@@ -408,6 +764,4 @@ ```js | ||
:exclamation: The current version of integration doesn't support typescript configs. | ||
**How does the integration internally resolve a config?** | ||
### How does the integration internally resolve a config? | ||
| Options parameter provided? | External config exists? | Result | | ||
@@ -422,25 +776,19 @@ | :-------------------------- | :---------------------: | :----------------------------------------------- | | ||
:exclamation: Important Notes | ||
:exclamation: The current version of the integration doesn't support typescript configs. | ||
Only official **@astrojs/\*** integrations are currently supported by Astro. | ||
## Examples | ||
There are two possibilities to make **astro-sitemap** integration working with current version of Astro. | ||
| Example | Source | Playground | | ||
| ------------- | -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | | ||
| basic | [GitHub](https://github.com/alextim/astro-lib/tree/main/examples/sitemap/basic) | [Play Online](https://stackblitz.com/fork/github/alextim/astro-lib/tree/main/examples/sitemap/basic) | | ||
| advanced | [GitHub](https://github.com/alextim/astro-lib/tree/main/examples/sitemap/advanced) | [Play Online](https://stackblitz.com/fork/github/alextim/astro-lib/tree/main/examples/sitemap/advanced) | | ||
| i18n | [GitHub](https://github.com/alextim/astro-lib/tree/main/examples/sitemap/i18n) | [Play Online](https://stackblitz.com/fork/github/alextim/astro-lib/tree/main/examples/sitemap/i18n) | | ||
Set the `experimental.integrations` option to `true` in your _astro.config.\*_. | ||
## Contributing | ||
```js | ||
// astro.config.mjs | ||
export default defineConfig({ | ||
// ... | ||
experimental: { | ||
integrations: true, | ||
}, | ||
}); | ||
``` | ||
You're welcome to submit an issue or PR! | ||
Or use the `--experimental-integrations` flag for build command. | ||
## Changelog | ||
```sh | ||
astro build --experimental-integrations | ||
``` | ||
See [CHANGELOG.md](CHANGELOG.md) for a history of changes to this integration. | ||
@@ -447,0 +795,0 @@ [astro-integration]: https://docs.astro.build/en/guides/integrations-guide/ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43662
788
467
Updatedzod@^3.17.10