-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: extended sitemap functionality * docs: del samples * docs: readme * feat: new sitemap * feat: createLinkInHead removed * docs: updated changeset text * refactor: 'zod' function() instead of self made refine() * Revert "refactor: 'zod' function() instead of self made refine()" This reverts commit 036bac7. undo function()
- Loading branch information
Showing
15 changed files
with
607 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
'@astrojs/sitemap': minor | ||
--- | ||
|
||
# Key features | ||
|
||
- Split up your large sitemap into multiple sitemaps by custom limit. | ||
- Ability to add sitemap specific attributes such as `lastmod` etc. | ||
- Final output customization via JS function. | ||
- Localization support. | ||
- Reliability: all config options are validated. | ||
|
||
## Important changes | ||
|
||
The integration always generates at least two files instead of one: | ||
|
||
- `sitemap-index.xml` - index file; | ||
- `sitemap-{i}.xml` - actual sitemap. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { SitemapOptions } from './index'; | ||
|
||
export const SITEMAP_CONFIG_DEFAULTS: SitemapOptions & any = { | ||
entryLimit: 45000, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const changefreqValues = [ | ||
'always', | ||
'hourly', | ||
'daily', | ||
'weekly', | ||
'monthly', | ||
'yearly', | ||
'never', | ||
] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { SitemapItemLoose } from 'sitemap'; | ||
|
||
import type { SitemapOptions } from './index'; | ||
import { parseUrl } from './utils/parse-url'; | ||
|
||
const STATUS_CODE_PAGE_REGEXP = /\/[0-9]{3}\/?$/; | ||
|
||
/** Construct sitemap.xml given a set of URLs */ | ||
export function generateSitemap(pages: string[], finalSiteUrl: string, opts: SitemapOptions) { | ||
const { changefreq, priority: prioritySrc, lastmod: lastmodSrc, i18n } = opts || {}; | ||
// TODO: find way to respect <link rel="canonical"> URLs here | ||
const urls = [...pages].filter((url) => !STATUS_CODE_PAGE_REGEXP.test(url)); | ||
urls.sort((a, b) => a.localeCompare(b, 'en', { numeric: true })); // sort alphabetically so sitemap is same each time | ||
|
||
const lastmod = lastmodSrc?.toISOString(); | ||
const priority = typeof prioritySrc === 'number' ? prioritySrc : undefined; | ||
|
||
const { locales, defaultLocale } = i18n || {}; | ||
const localeCodes = Object.keys(locales || {}); | ||
|
||
const getPath = (url: string) => { | ||
const result = parseUrl(url, i18n?.defaultLocale || '', localeCodes, finalSiteUrl); | ||
return result?.path; | ||
}; | ||
const getLocale = (url: string) => { | ||
const result = parseUrl(url, i18n?.defaultLocale || '', localeCodes, finalSiteUrl); | ||
return result?.locale; | ||
}; | ||
|
||
const urlData = urls.map((url) => { | ||
let links; | ||
if (defaultLocale && locales) { | ||
const currentPath = getPath(url); | ||
if (currentPath) { | ||
const filtered = urls.filter((subUrl) => getPath(subUrl) === currentPath); | ||
if (filtered.length > 1) { | ||
links = filtered.map((subUrl) => ({ | ||
url: subUrl, | ||
lang: locales[getLocale(subUrl)!], | ||
})); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
url, | ||
links, | ||
lastmod, | ||
priority, | ||
changefreq, // : changefreq as EnumChangefreq, | ||
} as SitemapItemLoose; | ||
}); | ||
|
||
return urlData; | ||
} |
Oops, something went wrong.