Skip to content

Commit

Permalink
[v2] Update Sitemap example (#5767)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkwiecinski authored and pieh committed Jun 7, 2018
1 parent 6fbf176 commit 99d613e
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 64 deletions.
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, 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
Expand Down
14 changes: 9 additions & 5 deletions examples/sitemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
"version": "1.0.0",
"author": "Nicholas Young <nicholas@nicholaswyoung.com>",
"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": {
Expand Down
7 changes: 7 additions & 0 deletions examples/sitemap/src/components/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react"

export default ({ children }) => (
<div>
{children}
</div>
)
44 changes: 0 additions & 44 deletions examples/sitemap/src/html.js

This file was deleted.

7 changes: 0 additions & 7 deletions examples/sitemap/src/layouts/index.js

This file was deleted.

23 changes: 15 additions & 8 deletions examples/sitemap/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import React from "react"
import Layout from "../components/layout"

const IndexRoute = () => (
<div>
<p>
Welcome to the GatsbyJS Sitemap Demo. Visit{` `}
<a href="https://www.gatsbyjs.org/sitemap.xml">
to see the generated sitemap.
</a>
</p>
</div>
<Layout>
<div>
<p>
Welcome to the GatsbyJS Sitemap Demo. Visit{` `}
<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>
</Layout>
)

export default IndexRoute
13 changes: 13 additions & 0 deletions examples/sitemap/src/pages/page-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import Layout from "../components/layout"

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

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

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

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
}
}
}
`

0 comments on commit 99d613e

Please sign in to comment.