Skip to content

Commit

Permalink
feat: auto generated sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Oct 17, 2020
1 parent 3cc1c64 commit bc209ff
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 50 deletions.
6 changes: 6 additions & 0 deletions core/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ export interface BuildConfiguration {
webpack?: WebpackConfig;
finalWebpack?: WebpackConfig;

/**
* if false, disable automatic sitemap generation
*/
siteMap?: boolean;

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

export const defaultBuildConfig: BuildConfiguration = {
siteRoot: '/',
siteMap: true,
categories: ['author', 'tags'],
ignore: [
'readme.md',
Expand Down
1 change: 1 addition & 0 deletions core/store/src/create-pages/pages-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const getIndexPage = (store: Store): HomePageInfo => {
? docStories[0]
: undefined;
return {
path: homePath,
storyId,
docId,
type: homePage?.type || defDocType,
Expand Down
1 change: 1 addition & 0 deletions core/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './types';
export * from './state';
export * from './create-pages';
export * from './serialization/load-store';
export * from './serialization/sitemap';
36 changes: 36 additions & 0 deletions core/store/src/serialization/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Store } from '@component-controls/core';
import {
getIndexPage,
getHomePages,
DocHomePagesPath,
getDocPages,
DocPagesPath,
} from '../create-pages';

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

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

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>`,
)
.join('\n')}
</urlset>
`;
return sitemap;
};
1 change: 1 addition & 0 deletions core/store/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface HomePageInfo {
type: string;
docId?: string;
storyId?: string;
path: string;
}

export interface StoryStore {
Expand Down
5 changes: 5 additions & 0 deletions core/webpack-configs/src/compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export interface CompileProps {
logOptions?: Partial<LogOptions>;
}

const defaultPresets = ['react', 'react-docgen-typescript'];

export const defaultCompileProps: CompileProps = {
presets: defaultPresets,
};
/**
* return type from compile and watch functions
*/
Expand Down
1 change: 0 additions & 1 deletion examples/docz-migration/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module.exports = {
siteUrl: 'https://component-controls.com',
},
plugins: [
`gatsby-plugin-sitemap`,
{
resolve: '@component-controls/gatsby-theme-stories',
options: {
Expand Down
1 change: 0 additions & 1 deletion examples/gatsby/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module.exports = {
siteUrl: 'https://component-controls.com',
},
plugins: [
`gatsby-plugin-sitemap`,
{
resolve: '@component-controls/gatsby-theme-stories',
options: {
Expand Down
1 change: 0 additions & 1 deletion examples/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"@component-controls/viewport-plugin": "^1.31.3",
"@theme-ui/presets": "^0.4.0-rc.1",
"gatsby": "^2.22.10",
"gatsby-plugin-sitemap": "^2.4.11",
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
Expand Down
24 changes: 18 additions & 6 deletions integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import fs from 'fs';
import sysPath from 'path';
import {
compile,
watch,
CompilerCallbackFn,
} from '@component-controls/webpack-compile';
import {
CompileProps,
defaultCompileProps,
getBundleName,
} from '@component-controls/webpack-configs';

Expand All @@ -14,28 +17,27 @@ import {
PluginCallback,
Page,
} from 'gatsby';
import { Store, getHomePath } from '@component-controls/core';
import { Store } from '@component-controls/core';
import {
getIndexPage,
getHomePages,
DocHomePagesPath,
getDocPages,
DocPagesPath,
loadStore,
getSiteMap,
} from '@component-controls/store';

const { StorePlugin } = require('@component-controls/store/plugin');

const defaultPresets = ['react', 'react-docgen-typescript'];

export const createPagesStatefully = async (
{ actions, store: gatsbyStore }: CreatePagesArgs,
options: CompileProps,
doneCb: PluginCallback,
) => {
const { createPage, deletePage } = actions;
const config: CompileProps = {
presets: defaultPresets,
...defaultCompileProps,
...options,
};
const onBundle: CompilerCallbackFn = ({ store: loadingStore }) => {
Expand All @@ -53,10 +55,10 @@ export const createPagesStatefully = async (
createPage(props);
};
//home page
const { docId = null, type = null, storyId = null } =
const { path, docId = null, type = null, storyId = null } =
getIndexPage(store) || {};
createGatsbyPage({
path: getHomePath(store),
path,
component: require.resolve(`../src/templates/DocPage.tsx`),
context: {
docId,
Expand Down Expand Up @@ -100,7 +102,17 @@ export const createPagesStatefully = async (
});
},
);
if (process.env.NODE_ENV === 'production' && store.config.siteMap) {
const sitemap = getSiteMap(store);
const sitemapname = sysPath.join(
process.cwd(),
'public',
'sitemap.xml',
);
fs.writeFileSync(sitemapname, sitemap, 'utf8');
}
}

doneCb(null, null);
};
const run = process.env.NODE_ENV === 'development' ? watch : compile;
Expand Down
51 changes: 38 additions & 13 deletions integrations/nextjs-plugin/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import fs from 'fs';
import path from 'path';
import { compile, watch } from '@component-controls/webpack-compile';
import { CompileProps } from '@component-controls/webpack-configs';

const defaultPresets = ['react', 'react-docgen-typescript'];
import {
compile,
watch,
CompilerCallbackFn,
} from '@component-controls/webpack-compile';
import {
CompileProps,
defaultCompileProps,
} from '@component-controls/webpack-configs';
import { Store } from '@component-controls/core';
import { loadStore, getSiteMap } from '@component-controls/store';

module.exports = ({
bundleName,
Expand All @@ -22,18 +30,35 @@ module.exports = ({
configPath,
webPack,
};
const options: CompileProps = {
presets: presets || defaultPresets,
distFolder: path.resolve(__dirname),
staticFolder:
staticFolder || path.join(process.cwd(), 'public', 'static'),
...userProps,
const config: CompileProps = {
...defaultCompileProps,
...{
distFolder: path.resolve(__dirname),
staticFolder:
staticFolder || path.join(process.cwd(), 'public', 'static'),
...userProps,
},
};
if (presets) {
config.presets = presets;
}
const onBundle: CompilerCallbackFn = ({ store: loadingStore }) => {
if (loadingStore) {
const store: Store = loadStore(loadingStore, true);
if (process.env.NODE_ENV === 'production' && store.config.siteMap) {
const sitemap = getSiteMap(store);
const sitemapname = path.join(
config.distFolder as string,
'sitemap.xml',
);
fs.writeFileSync(sitemapname, sitemap, 'utf8');
}
}
};

const compiler =
process.env.NODE_ENV === 'development'
? watch(options)
: compile(options);
? watch(config, onBundle)
: compile(config, onBundle);
await compiler;

return [];
Expand Down
4 changes: 4 additions & 0 deletions ui/app/src/SEO/SEO.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const SEO = ({
siteLanguage,
siteImage: defaultImage,
author,
siteMap,
} = config || {};
const image = propImage || defaultImage;
const seo = {
Expand All @@ -48,6 +49,9 @@ export const SEO = ({
<html lang={siteLanguage} />
<meta name="description" content={seo.description} />
{seo.image && <meta name="image" content={seo.image} />}
{siteMap && (
<link rel="sitemap" type="application/xml" href="/sitemap.xml" />
)}
<meta property="og:title" content={seo.title} />
<meta property="og:url" content={seo.url} />
<meta property="og:description" content={seo.description} />
Expand Down
28 changes: 0 additions & 28 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13069,16 +13069,6 @@ gatsby-plugin-root-import@^2.0.5:
resolved "https://registry.yarnpkg.com/gatsby-plugin-root-import/-/gatsby-plugin-root-import-2.0.5.tgz#04e520dc661d67f49aa7950f11b7c780fd2fdbd3"
integrity sha512-/yA6rFjfjiFb8D6nCjfFrrGqYQMkOt4J3u2o6s7VYEF/zpA5dw2C9ENJ5fDKkJSCbbwLiEIGVMMee3vMEip2zA==

gatsby-plugin-sitemap@^2.4.11:
version "2.4.12"
resolved "https://registry.yarnpkg.com/gatsby-plugin-sitemap/-/gatsby-plugin-sitemap-2.4.12.tgz#4564800ac0e8dfaad7edf7054d7625dad7ecb793"
integrity sha512-nQK3EyqECUpUcCoQr5ZCfGYPUCc+hI6tK+hvoqr4lkhMr0sjfXUvGkSlVarhMVLRNRe+e6Q39JFhVtsTXUp8Iw==
dependencies:
"@babel/runtime" "^7.10.3"
minimatch "^3.0.4"
pify "^3.0.0"
sitemap "^1.13.0"

gatsby-plugin-theme-ui@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/gatsby-plugin-theme-ui/-/gatsby-plugin-theme-ui-0.3.0.tgz#ab84216536ae45abe09a6edf24156b9dbf50d6a5"
Expand Down Expand Up @@ -23166,14 +23156,6 @@ sisteransi@^1.0.4:
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"
integrity sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==

sitemap@^1.13.0:
version "1.13.0"
resolved "https://registry.yarnpkg.com/sitemap/-/sitemap-1.13.0.tgz#569cbe2180202926a62a266cd3de09c9ceb43f83"
integrity sha1-Vpy+IYAgKSamKiZs094Jyc60P4M=
dependencies:
underscore "^1.7.0"
url-join "^1.1.0"

slash@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
Expand Down Expand Up @@ -25000,11 +24982,6 @@ underscore.string@^3.3.5:
sprintf-js "^1.0.3"
util-deprecate "^1.0.2"

underscore@^1.7.0:
version "1.11.0"
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.11.0.tgz#dd7c23a195db34267186044649870ff1bab5929e"
integrity sha512-xY96SsN3NA461qIRKZ/+qox37YXPtSBswMGfiNptr+wrt6ds4HaMw23TP612fEyGekRE6LNRiLYr/aqbHXNedw==

unescape-js@^1.1.1:
version "1.1.4"
resolved "https://registry.yarnpkg.com/unescape-js/-/unescape-js-1.1.4.tgz#4bc6389c499cb055a98364a0b3094e1c3d5da395"
Expand Down Expand Up @@ -25422,11 +25399,6 @@ urix@^0.1.0:
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"
integrity sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI=

url-join@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/url-join/-/url-join-1.1.0.tgz#741c6c2f4596c4830d6718460920d0c92202dc78"
integrity sha1-dBxsL0WWxIMNZxhGCSDQySIC3Hg=

url-loader@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/url-loader/-/url-loader-1.1.2.tgz#b971d191b83af693c5e3fea4064be9e1f2d7f8d8"
Expand Down

0 comments on commit bc209ff

Please sign in to comment.