Skip to content

Commit

Permalink
fix: sitemap location to static folder
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Oct 17, 2020
1 parent 70fc17d commit b0421fa
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 28 deletions.
18 changes: 9 additions & 9 deletions core/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,15 @@ export type PagesOnlyRoutes = Record<
export interface SitemapConfigPage {
priority: number;
}
export interface SitemapConfig {
outputFolder: string;
pages?: {
home: SitemapConfigPage;
index: SitemapConfigPage;
doc: SitemapConfigPage;
};
}
export type SitemapConfig =
| {
pages?: {
home: SitemapConfigPage;
index: SitemapConfigPage;
doc: SitemapConfigPage;
};
}
| boolean;

/**
* global configuration used at build time
Expand Down Expand Up @@ -377,7 +378,6 @@ export const convertConfig = (config: RunConfiguration): RunConfiguration => {
export const defaultBuildConfig: BuildConfiguration = {
siteRoot: '/',
siteMap: {
outputFolder: 'public/',
pages: {
home: {
priority: 1,
Expand Down
10 changes: 8 additions & 2 deletions core/store/src/create-pages/pages-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TabConfiguration,
getDocPath,
getStoryPath,
dateToLocalString,
} from '@component-controls/core';

import { HomePageInfo } from '../types';
Expand All @@ -34,6 +35,7 @@ export const getIndexPage = (store: Store): HomePageInfo => {
? docStories[0]
: undefined;
return {
lastModified: dateToLocalString(homePage?.dateModified),
path: homePath,
storyId,
docId,
Expand All @@ -46,6 +48,7 @@ export interface DocHomePagesPath {
path: string;
docId?: string;
storyId?: string;
lastModified?: string;
}
export const getHomePages = (store: Store): DocHomePagesPath[] => {
const { pages = {} } = store?.config || {};
Expand All @@ -66,12 +69,13 @@ export const getHomePages = (store: Store): DocHomePagesPath[] => {
);
}) ||
docs.find(key => (store.docs[key].type || defDocType) === type);
const docStories: string[] =
docId && store.docs[docId] ? store.docs[docId].stories || [] : [];
const doc = docId ? store.docs[docId] : undefined;
const docStories: string[] = doc?.stories || [];
const storyId: string | undefined = docStories.length
? docStories[0]
: undefined;
return {
lastModified: dateToLocalString(doc?.dateModified),
type,
path,
docId,
Expand Down Expand Up @@ -129,6 +133,7 @@ export const getUniquesByField = (
export interface DocPagesPath {
type: DocType;
path: string;
lastModified?: string;
docId?: string | null;
storyId?: string | null;
category?: string | null;
Expand Down Expand Up @@ -160,6 +165,7 @@ export const getDocPages = (store: Store): DocPagesPath[] => {
stories.forEach((storyId?: string) => {
const path = getStoryPath(storyId, doc, store, route);
docPaths.push({
lastModified: dateToLocalString(doc.dateModified),
path,
type: docType,
activeTab: route,
Expand Down
36 changes: 27 additions & 9 deletions core/store/src/serialization/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,44 @@ import {

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

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

const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<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:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
${pages
.map(
({ path, priority }) =>
`<url> <loc>${path}</loc><changefreq>daily</changefreq> <priority>${priority}</priority> </url>`,
({ path, priority, lastModified }) =>
`<url><loc>${path}</loc>${
lastModified ? `<lastmod>${lastModified}</lastmod>` : ''
}<changefreq>daily</changefreq><priority>${priority}</priority></url>`,
)
.join('\n')}
</urlset>
Expand Down
2 changes: 2 additions & 0 deletions core/store/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export interface DocPageInfo {
docId?: string;
storyId?: string;
category?: string;
lastModified?: string;
}
export interface HomePageInfo {
type: string;
docId?: string;
storyId?: string;
path: string;
lastModified?: string;
}

export interface StoryStore {
Expand Down
1 change: 1 addition & 0 deletions integrations/gatsby-theme-stories/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"theme-ui": "^0.4.0-rc.1"
},
"devDependencies": {
"@component-controls/logger": "^1.22.0",
"@component-controls/ts-markdown-docs": "^1.21.0",
"@types/react": "^16.9.34",
"eslint-plugin-react-hooks": "^4.1.2",
Expand Down
11 changes: 6 additions & 5 deletions integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import sysPath from 'path';
import { log } from '@component-controls/logger';
import {
compile,
watch,
Expand Down Expand Up @@ -104,11 +105,11 @@ export const createPagesStatefully = async (
);
if (process.env.NODE_ENV === 'production' && store.config.siteMap) {
const sitemap = getSiteMap(store);
const sitemapname = sysPath.resolve(
process.cwd(),
store.config.siteMap.outputFolder,
'sitemap.xml',
);
const staticFolder =
config.staticFolder ||
sysPath.join(process.cwd(), 'public', 'static');
const sitemapname = sysPath.resolve(staticFolder, 'sitemap.xml');
log('creating sitemap', sitemapname);
fs.writeFileSync(sitemapname, sitemap, 'utf8');
}
}
Expand Down
2 changes: 2 additions & 0 deletions integrations/nextjs-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
"license": "MIT",
"dependencies": {
"@component-controls/app": "^1.32.2",

"@component-controls/webpack-compile": "^1.32.2",
"@component-controls/webpack-configs": "^1.32.2",
"next": "^9.5.0"
},
"devDependencies": {
"@component-controls/ts-markdown-docs": "^1.21.0",
"@component-controls/logger": "^1.22.0",
"typescript": "^3.8.3"
},
"publishConfig": {
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 @@ -5,6 +5,7 @@ import {
watch,
CompilerCallbackFn,
} from '@component-controls/webpack-compile';
import { log } from '@component-controls/logger';
import {
CompileProps,
defaultCompileProps,
Expand Down Expand Up @@ -48,10 +49,10 @@ module.exports = ({
if (process.env.NODE_ENV === 'production' && store.config.siteMap) {
const sitemap = getSiteMap(store);
const sitemapname = path.resolve(
store.config.siteMap.outputFolder,
'./',
config.staticFolder as string,
'sitemap.xml',
);
log('creating sitemap', sitemapname);
fs.writeFileSync(sitemapname, sitemap, 'utf8');
}
}
Expand Down
2 changes: 1 addition & 1 deletion ui/app/src/SEO/SEO.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const SEO = ({
<meta name="description" content={seo.description} />
{seo.image && <meta name="image" content={seo.image} />}
{siteMap && (
<link rel="sitemap" type="application/xml" href="/sitemap.xml" />
<link rel="sitemap" type="application/xml" href="/static/sitemap.xml" />
)}
<meta property="og:title" content={seo.title} />
<meta property="og:url" content={seo.url} />
Expand Down

0 comments on commit b0421fa

Please sign in to comment.