Skip to content

Commit

Permalink
fix: add sitemap configuration object
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Oct 17, 2020
1 parent 33f0661 commit 87f6288
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
29 changes: 27 additions & 2 deletions core/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ export type PagesOnlyRoutes = Record<
}
>;

export interface SitemapConfigPage {
priority: number;
}
export interface SitemapConfig {
outputFolder: string;
pages?: {
home: SitemapConfigPage;
index: SitemapConfigPage;
doc: SitemapConfigPage;
};
}

/**
* global configuration used at build time
* stored in a file named main.js/main.ts
Expand Down Expand Up @@ -163,7 +175,7 @@ export interface BuildConfiguration {
/**
* if false, disable automatic sitemap generation
*/
siteMap?: boolean;
siteMap?: SitemapConfig;

/**
* instrumentation configuration
Expand Down Expand Up @@ -364,7 +376,20 @@ export const convertConfig = (config: RunConfiguration): RunConfiguration => {

export const defaultBuildConfig: BuildConfiguration = {
siteRoot: '/',
siteMap: true,
siteMap: {
outputFolder: 'public/',
pages: {
home: {
priority: 1,
},
index: {
priority: 0.8,
},
doc: {
priority: 0.5,
},
},
},
categories: ['author', 'tags'],
ignore: [
'readme.md',
Expand Down
7 changes: 4 additions & 3 deletions core/store/src/serialization/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import {
} from '../create-pages';

export const getSiteMap = (store: Store): string => {
const config = store.config.siteMap;
const pages: { path?: string; priority: number }[] = [];
//home page
const { path } = getIndexPage(store) || {};
pages.push({ path, priority: 1 });
pages.push({ path, priority: config?.pages?.home.priority || 1 });
const homePages = getHomePages(store);
homePages.forEach(({ path }: DocHomePagesPath) => {
pages.push({ path, priority: 0.8 });
pages.push({ path, priority: config?.pages?.index.priority || 1 });
});

const docPages = getDocPages(store);
docPages.forEach(({ path }: DocPagesPath) => {
pages.push({ path, priority: 0.5 });
pages.push({ path, priority: config?.pages?.doc.priority || 1 });
});

const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
Expand Down
4 changes: 2 additions & 2 deletions integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export const createPagesStatefully = async (
);
if (process.env.NODE_ENV === 'production' && store.config.siteMap) {
const sitemap = getSiteMap(store);
const sitemapname = sysPath.join(
const sitemapname = sysPath.resolve(
process.cwd(),
'public',
store.config.siteMap.outputFolder,
'sitemap.xml',
);
fs.writeFileSync(sitemapname, sitemap, 'utf8');
Expand Down
5 changes: 3 additions & 2 deletions integrations/nextjs-plugin/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ module.exports = ({
const store: Store = loadStore(loadingStore, true);
if (process.env.NODE_ENV === 'production' && store.config.siteMap) {
const sitemap = getSiteMap(store);
const sitemapname = path.join(
path.resolve(config.staticFolder as string, '..'),
const sitemapname = path.resolve(
store.config.siteMap.outputFolder,
'./',
'sitemap.xml',
);
fs.writeFileSync(sitemapname, sitemap, 'utf8');
Expand Down

0 comments on commit 87f6288

Please sign in to comment.