From d45f6b8aaf45a4d7fb4f701917d30958f790c75a Mon Sep 17 00:00:00 2001 From: Piotr Kwiecinski Date: Sat, 2 Jun 2018 14:26:20 +0100 Subject: [PATCH] Update Sitemap example for Gatsby v2 --- examples/sitemap/gatsby-config.js | 3 ++ examples/sitemap/gatsby-node.js | 45 +++++++++++++++++++ examples/sitemap/package.json | 14 +++--- examples/sitemap/src/components/layout.js | 7 +++ examples/sitemap/src/html.js | 44 ------------------ examples/sitemap/src/layouts/index.js | 7 --- examples/sitemap/src/pages/index.js | 23 ++++++---- examples/sitemap/src/pages/page-2.js | 13 ++++++ examples/sitemap/src/pages/secret.js | 13 ++++++ .../src/templates/template-blog-post.js | 33 ++++++++++++++ 10 files changed, 138 insertions(+), 64 deletions(-) create mode 100644 examples/sitemap/src/components/layout.js delete mode 100644 examples/sitemap/src/html.js delete mode 100644 examples/sitemap/src/layouts/index.js create mode 100644 examples/sitemap/src/pages/page-2.js create mode 100644 examples/sitemap/src/pages/secret.js create mode 100644 examples/sitemap/src/templates/template-blog-post.js 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 cdfa517634871..6f9388501acaf 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, actions }) => { + const { createPage } = actions + + 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, actions, getNode }) => { const { createNodeField } = actions diff --git a/examples/sitemap/package.json b/examples/sitemap/package.json index 87965d9ba2730..8440d25785601 100644 --- a/examples/sitemap/package.json +++ b/examples/sitemap/package.json @@ -5,12 +5,16 @@ "version": "1.0.0", "author": "Nicholas Young ", "dependencies": { - "gatsby": "latest", - "gatsby-source-filesystem": "latest", - "gatsby-transformer-remark": "latest", - "gatsby-plugin-sitemap": "latest" + "gatsby": "next", + "gatsby-plugin-sitemap": "next", + "gatsby-source-filesystem": "next", + "gatsby-transformer-remark": "next", + "react": "^16.3.2", + "react-dom": "^16.3.2" }, - "keywords": ["gatsby"], + "keywords": [ + "gatsby" + ], "license": "MIT", "main": "index.js", "scripts": { diff --git a/examples/sitemap/src/components/layout.js b/examples/sitemap/src/components/layout.js new file mode 100644 index 0000000000000..bcdb2d6ea6897 --- /dev/null +++ b/examples/sitemap/src/components/layout.js @@ -0,0 +1,7 @@ +import React from "react" + +export default ({ children }) => ( +
+ {children} +
+) 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/layouts/index.js b/examples/sitemap/src/layouts/index.js deleted file mode 100644 index 967e17974cd00..0000000000000 --- a/examples/sitemap/src/layouts/index.js +++ /dev/null @@ -1,7 +0,0 @@ -import React from "react" - -export default class DefaultLayout extends React.Component { - render() { - return
{this.props.children()}
- } -} diff --git a/examples/sitemap/src/pages/index.js b/examples/sitemap/src/pages/index.js index 496ae5d9ef96b..7fe50dcc55b31 100644 --- a/examples/sitemap/src/pages/index.js +++ b/examples/sitemap/src/pages/index.js @@ -1,14 +1,21 @@ import React from "react" +import Layout from "../components/layout" const IndexRoute = () => ( -
-

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

-
+ +
+

+ 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! +

+
+
) export default IndexRoute diff --git a/examples/sitemap/src/pages/page-2.js b/examples/sitemap/src/pages/page-2.js new file mode 100644 index 0000000000000..747b760342ab7 --- /dev/null +++ b/examples/sitemap/src/pages/page-2.js @@ -0,0 +1,13 @@ +import React from 'react' +import Layout from "../components/layout" + +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..3d0843a4ae842 --- /dev/null +++ b/examples/sitemap/src/pages/secret.js @@ -0,0 +1,13 @@ +import React from 'react' +import Layout from "../components/layout" + +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 + } + } + } +`