Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sitemap example to include posts and exclude sample page #5723

Merged
merged 4 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/sitemap/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module.exports = {
`gatsby-transformer-remark`,
{
resolve: `gatsby-plugin-sitemap`,
options: {
exclude: [`/secret`],
},
},
],
}
45 changes: 45 additions & 0 deletions examples/sitemap/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion examples/sitemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"gatsby-transformer-remark": "latest",
"gatsby-plugin-sitemap": "latest"
},
"keywords": ["gatsby"],
"keywords": [
"gatsby"
],
"license": "MIT",
"main": "index.js",
"scripts": {
Expand Down
44 changes: 0 additions & 44 deletions examples/sitemap/src/html.js

This file was deleted.

6 changes: 5 additions & 1 deletion examples/sitemap/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ const IndexRoute = () => (
<div>
<p>
Welcome to the GatsbyJS Sitemap Demo. Visit{` `}
<a href="https://www.gatsbyjs.org/sitemap.xml">
<a href="/sitemap.xml">
to see the generated sitemap.
</a>
</p>
<p>
Note: gatsby-plugin-sitemap uses <code>siteMetadata.siteUrl</code>{` `}
defined in gatsby-config.js to construct absolute URLs!
</p>
</div>
)

Expand Down
10 changes: 10 additions & 0 deletions examples/sitemap/src/pages/page-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'

const Page = () => (
<div>
<h1>Not so secret page</h1>
<p>This page should be included in sitemap.xml</p>
</div>
)

export default Page
10 changes: 10 additions & 0 deletions examples/sitemap/src/pages/secret.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'

const Secret = () => (
<div>
<h1>Secret page</h1>
<p>This page should be excluded from sitemap.xml</p>
</div>
)

export default Secret
33 changes: 33 additions & 0 deletions examples/sitemap/src/templates/template-blog-post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react'

class BlogPost extends React.Component {
render() {
const { html, frontmatter } = this.props.data.markdownRemark
return (
<div>
<h1>{frontmatter.title}</h1>
<div className="container content">
<div dangerouslySetInnerHTML={{ __html: html }} />
</div>
</div>
)
}
}

export default BlogPost

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