mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-03 00:39:45 +02:00
feat(v2): generate sitemap (#1065)
* feat(v2): generate sitemap * cannot snapshot sitemap because xml generated in different order
This commit is contained in:
parent
8663a63e7d
commit
fd780f19a2
18 changed files with 127 additions and 1185 deletions
|
@ -4,6 +4,7 @@ const chalk = require('chalk');
|
|||
const fs = require('fs-extra');
|
||||
const globby = require('globby');
|
||||
const load = require('../load');
|
||||
const createSitemap = require('../core/sitemap');
|
||||
const createServerConfig = require('../webpack/server');
|
||||
const createClientConfig = require('../webpack/client');
|
||||
const {applyConfigureWebpack} = require('../webpack/utils');
|
||||
|
@ -67,6 +68,11 @@ module.exports = async function build(siteDir) {
|
|||
}),
|
||||
);
|
||||
|
||||
// generate sitemap
|
||||
const sitemap = await createSitemap(props);
|
||||
const sitemapPath = path.join(outDir, 'sitemap.xml');
|
||||
await fs.writeFile(sitemapPath, sitemap);
|
||||
|
||||
const relativeDir = path.relative(process.cwd(), outDir);
|
||||
console.log(
|
||||
`\n${chalk.green('Success!')} Generated static files in ${chalk.cyan(
|
||||
|
|
38
v2/lib/core/sitemap.js
Normal file
38
v2/lib/core/sitemap.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
const sitemap = require('sitemap');
|
||||
|
||||
module.exports = async function createSitemap({
|
||||
siteConfig = {},
|
||||
docsMetadatas = {},
|
||||
pagesMetadatas = [],
|
||||
blogMetadatas = [],
|
||||
}) {
|
||||
const allMetadatas = [
|
||||
...blogMetadatas,
|
||||
...Object.values(docsMetadatas),
|
||||
...pagesMetadatas,
|
||||
];
|
||||
|
||||
const {url: siteUrl} = siteConfig;
|
||||
|
||||
if (!siteUrl) {
|
||||
throw new Error('Url in siteConfig.js cannot be empty/undefined');
|
||||
}
|
||||
|
||||
const urls = [];
|
||||
|
||||
allMetadatas.forEach(metadata => {
|
||||
urls.push({
|
||||
url: metadata.permalink,
|
||||
changefreq: 'weekly',
|
||||
priority: 0.5,
|
||||
});
|
||||
});
|
||||
|
||||
const sm = sitemap.createSitemap({
|
||||
hostname: siteUrl,
|
||||
cacheTime: 600 * 1000, // 600 sec - cache purge period
|
||||
urls,
|
||||
});
|
||||
|
||||
return sm.toString();
|
||||
};
|
|
@ -17,6 +17,7 @@ module.exports = function loadConfig(siteDir, deleteCache = true) {
|
|||
'organizationName',
|
||||
'projectName',
|
||||
'baseUrl',
|
||||
'url',
|
||||
];
|
||||
const optionalFields = [
|
||||
'customDocsPath',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue