diff --git a/examples/sitemap/gatsby-config.js b/examples/sitemap/gatsby-config.js index 39cd906d29328..8b2187e566568 100644 --- a/examples/sitemap/gatsby-config.js +++ b/examples/sitemap/gatsby-config.js @@ -15,6 +15,9 @@ module.exports = { `gatsby-transformer-remark`, { resolve: `gatsby-plugin-sitemap`, + options: { + exclude: [`/secret`], + }, }, ], } diff --git a/examples/sitemap/gatsby-node.js b/examples/sitemap/gatsby-node.js index edab7e1ee4a4d..7c3b752d9679b 100644 --- a/examples/sitemap/gatsby-node.js +++ b/examples/sitemap/gatsby-node.js @@ -1,4 +1,49 @@ const path = require(`path`) +const slash = require(`slash`) + +exports.createPages = ({ graphql, boundActionCreators }) => { + const { createPage } = boundActionCreators + + return new Promise((resolve, reject) => { + const blogPostTemplate = path.resolve(`src/templates/template-blog-post.js`) + graphql( + ` + { + allMarkdownRemark( + limit: 1000 + filter: { frontmatter: { draft: { ne: true } } } + ) { + edges { + node { + fields { + slug + } + } + } + } + } + ` + ).then(result => { + if (result.errors) { + console.log(result.errors) + reject(result.errors) + } + + // Create blog posts pages. + result.data.allMarkdownRemark.edges.forEach(edge => { + createPage({ + path: edge.node.fields.slug, // required + component: slash(blogPostTemplate), + context: { + slug: edge.node.fields.slug, + }, + }) + }) + + resolve() + }) + }) +} exports.onCreateNode = ({ node, boundActionCreators, getNode }) => { const { createNodeField } = boundActionCreators diff --git a/examples/sitemap/package.json b/examples/sitemap/package.json index 87965d9ba2730..57620aa08af4c 100644 --- a/examples/sitemap/package.json +++ b/examples/sitemap/package.json @@ -10,7 +10,9 @@ "gatsby-transformer-remark": "latest", "gatsby-plugin-sitemap": "latest" }, - "keywords": ["gatsby"], + "keywords": [ + "gatsby" + ], "license": "MIT", "main": "index.js", "scripts": { diff --git a/examples/sitemap/src/html.js b/examples/sitemap/src/html.js deleted file mode 100644 index ecd3580eea08d..0000000000000 --- a/examples/sitemap/src/html.js +++ /dev/null @@ -1,44 +0,0 @@ -import React, { Component } from "react" -import * as PropTypes from "prop-types" - -const propTypes = { - headComponents: PropTypes.node.isRequired, - body: PropTypes.node.isRequired, - postBodyComponents: PropTypes.node.isRequired, -} - -class Html extends Component { - render() { - return ( - - - {this.props.headComponents} - - - - - - - Gatsby - No Plugins - - -
- {this.props.postBodyComponents} - - - ) - } -} - -Html.propTypes = propTypes - -module.exports = Html diff --git a/examples/sitemap/src/pages/index.js b/examples/sitemap/src/pages/index.js index 496ae5d9ef96b..d87ec68cec241 100644 --- a/examples/sitemap/src/pages/index.js +++ b/examples/sitemap/src/pages/index.js @@ -4,10 +4,14 @@ const IndexRoute = () => (

Welcome to the GatsbyJS Sitemap Demo. Visit{` `} - + to see the generated sitemap.

+

+ Note: gatsby-plugin-sitemap uses siteMetadata.siteUrl{` `} + defined in gatsby-config.js to construct absolute URLs! +

) diff --git a/examples/sitemap/src/pages/page-2.js b/examples/sitemap/src/pages/page-2.js new file mode 100644 index 0000000000000..465862060a65d --- /dev/null +++ b/examples/sitemap/src/pages/page-2.js @@ -0,0 +1,10 @@ +import React from 'react' + +const Page = () => ( +
+

Not so secret page

+

This page should be included in sitemap.xml

+
+) + +export default Page diff --git a/examples/sitemap/src/pages/secret.js b/examples/sitemap/src/pages/secret.js new file mode 100644 index 0000000000000..90c445f3c515d --- /dev/null +++ b/examples/sitemap/src/pages/secret.js @@ -0,0 +1,10 @@ +import React from 'react' + +const Secret = () => ( +
+

Secret page

+

This page should be excluded from sitemap.xml

+
+) + +export default Secret diff --git a/examples/sitemap/src/templates/template-blog-post.js b/examples/sitemap/src/templates/template-blog-post.js new file mode 100644 index 0000000000000..72bc539bbf0ef --- /dev/null +++ b/examples/sitemap/src/templates/template-blog-post.js @@ -0,0 +1,33 @@ +import React from 'react' + +class BlogPost extends React.Component { + render() { + const { html, frontmatter } = this.props.data.markdownRemark + return ( +
+

{frontmatter.title}

+
+
+
+
+ ) + } +} + +export default BlogPost + +export const PageQuery = graphql` + query blogPostBySlug($slug: String!) { + markdownRemark(fields: { slug: { eq: $slug } }) { + html + frontmatter { + title + } + } + site { + siteMetadata { + title + } + } + } +`