Gatsby is a React Framework that builds static PWA (Progressive Web App)
When a visitor lands on a Gatsby page, the page’s HTML file is loaded first, then the React JavaScript bundle.
yarn global add gatsby-cli
gatsby new [SITE_DIRECTORY_NAME] [URL_OF_STARTER_GITHUB_REPO]
# example
gatsby new hello-world https://github.com/gatsbyjs/gatsby-starter-hello-world
where:
cd hello-world
gatsby develop
# or to listen on both ‘localhost’ and your local IP
gatsby develop -- --host=0.0.0.0
./src
/pages
/components
Source plugins fetch data from their source.
Node is a an object in a graph
siteMetadata: {
title: `Title from siteMetadata`,
}
Graphiql - http://localhost:8000/___graphql
You can sort and filter nodes, set how many nodes to skip, and choose the limit of how many nodes to retrieve.
allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC })
Page queries live outside of the component definition — by convention at the end of a page component file — and are only available on page components.
Tuto: build-a-page-with-a-graphql-query
static-query allows non-page components (like our layout.js component), to retrieve data via GraphQL queries
Source plugins fetch data from their source.
Tuto: source-plugins
transformer plugins transform the raw content brought by source plugins. transformer plugins which take raw content from source plugins and transform it into something more usable.
yarn add gatsby-transformer-remark
Gatsby lets you use GraphQL to query your data and map the query results to pages—all at build time.
This is a two steps process:
Example: Gatsby - Markdown
// The API implementation for onCreateNode
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
console.log("onCreateNode for "+node.internal.type)
const fileNode = getNode(node.parent)
console.log(` * Create slug for `, fileNode.relativePath)
const slug = createFilePath({ node, getNode, basePath: `pages` })
createNodeField({
node,
name: `slug`,
value: slug,
})
}
}
query MkDownSlugQuery {
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
{
"data": {
"allMarkdownRemark": {
"edges": [
{
"node": {
"fields": {
"slug": "/sweet-pandas-eating-sweets/"
}
}
}
]
}
}
}
to the console for a template, see tuto
exports.createPages = ({ graphql, actions }) => {
// **Note:** The graphql function call returns a Promise
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise for more info
return graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`
).then(result => {
console.log(JSON.stringify(result, null, 4))
})
}