Boilerplate code for a Gatsby 3 blog using Netlify for hosting. Based on principles of:
- Accessibility
- Security
- Speed
Features:
- Optimised for Gatsby 3 - using File Route API, new Gatsby 3 Image Plugin
- 100% score of Lighthouse accessibility
- No accessibility warning on axe check
- High lighthouse speed scores
- A or higher security rating for headers from securityheaders.com
- Correct use of semantic HTML (only one
<h1>
per page, sections and posts use<section>
and<article>
correctly etc.).
-
Create a Gatsby 3 site.
Use the Gatsby 3 CLI to create a new site, specifying the hello-world starter.
# create a new Gatsby 3 MDX blog site using gatsby-starter-climate gatsby new my-mdx-blog-starter https://github.com/rodneylab/gatsby-starter-climate
-
Start developing.
Navigate into your new siteβs directory, copy the example environment variables (and customise them to suit your needs) and start it up.
cd my-mdx-blog-starter/ cp .env.EXAMPLE .env.development cp .env.EXAMPLE .env.production gatsby develop
-
Open the source code and start editing!
Your site is now running at
http://localhost:8000
!Note: You'll also see a second link:
http://localhost:8000/___graphql
. This is a tool you can use to experiment with querying your data. Learn more about using this tool in the Gatsby 3 tutorial.Open the
my-mdx-blog-starter
directory in your code editor of choice and editsrc/pages/index.jsx
. Save your changes and the browser will update in real time!
A quick look at the top-level files and directories you'll see in a Gatsby 3 project.
.
βββ cypress
βββ node_modules
βββ src
βββ .env.EXAMPLE
βββ .eslintrc.js
βββ .gitignore
βββ .prettierrc
βββ commitlint.config.js
βββ csp-util
βββ gatsby-browser.js
βββ gatsby-config.js
βββ gatsby-node.js
βββ gatsby-ssr.js
βββ LICENSE
βββ netlify.toml
βββ package-lock.json
βββ package.json
βββ README.md
-
/cypress
: This directory contains Cypress axe accessibility end-to-end tests. You can expand the tests to suit your needs. -
/node_modules
: This directory contains all of the modules of code that your project depends on (npm packages) are automatically installed. -
/src
: This directory will contain all of the code related to what you will see on the front-end of your site (what you see in the browser) such as your site header or a page template.src
is a convention for βsource codeβ. -
.env.EXAMPLE
: This file contains example environment variables. Make two copies to this same root folder:.env.development
and.env.production
and add your personal environment variables. These two files will be excluded from git commits by default. -
.eslintrc.js
: This file tells ESLint which linting rules to apply. Set to use Airbnb rules by default. Adjust to suit your taste. As well as code style rules, it applies a11y recommended accessibility checks. -
.gitignore
: This file tells git which files it should not track / not maintain a version history for. -
.prettierrc
: This is a configuration file for Prettier. Prettier is a tool to help keep the formatting of your code consistent. -
.stylelintrc.json
: This is a configuration file for StyleLint. Stylelint is a tool to help keep the formatting of your code consistent. It can be used in teams to enforce a consistent coding style. -
commitlint.config.js
: Configuration for commitlint which is used to enforce conventional git commits. This works in tandem with husky configuration in thepackage.json
file. -
csp-util
: This is a script for placing CSP hashes in Netlify headers, improving site security when running on Netlify. -
gatsby-browser.js
: This file is where Gatsby 3 expects to find any usage of the Gatsby 3 browser APIs (if any). These allow customisation/extension of default Gatsby settings affecting the browser. -
gatsby-config.js
: This is the main configuration file for a Gatsby 3 site. This is where you can specify information about your site (metadata) like the site title and description, which Gatsby 3 plugins youβd like to include, etc. (Check out the config docs for more detail). To improve security, fairly strict security header parameters are set. You will almost certainly need to tweak these for your own use case. For help use the following resources: -
gatsby-node.js
: This file is where Gatsby 3 expects to find any usage of the Gatsby 3 Node APIs (if any). These allow customisation/extension of default Gatsby 3 settings affecting pieces of the site build process. -
gatsby-ssr.js
: This file is where Gatsby 3 expects to find any usage of the Gatsby 3 server-side rendering APIs (if any). These allow customisation of default Gatsby 3 settings affecting server-side rendering. -
LICENSE
: This Gatsby 3 starter is licensed under the BSD-3-Clause license. -
netlify.toml
This file contains configuration for Netlify builds. It includes an instruction to use the Netlify@netlify/plugin-gatsby
plugin which helps improve build speed on Netlify and adds support for Gatsby Functions. It also includes the custom build command, defined inpackage.json
, which generates Netlify headers with CSP hashes. -
package-lock.json
(Seepackage.json
below, first). This is an automatically generated file based on the exact versions of your npm dependencies that were installed for your project. (You wonβt change this file directly). -
package.json
: A manifest file for Node.js projects, which includes things like metadata (the projectβs name, author, etc). This manifest is how npm knows which packages to install for your project. -
README.md
: A text file containing useful reference information about your project.
Looking for more guidance? Full documentation for Gatsby 3 lives on the website. Here are some places to start:
-
For most developers, we recommend starting with our in-depth tutorial for creating a site with Gatsby 3. It starts with zero assumptions about your level of ability and walks through every step of the process.
-
To dive straight into code samples, head to our documentation. In particular, check out the Guides, API Reference, and Advanced Tutorials sections in the sidebar.
Build, Deploy, and Host On Netlify
Netlify offers an easy to configure environment and a generous free tier. With baked-in CDN and automated TLS certificates it is a great choice for getting your Gatsby 3 MDX blog up and running at warp speed.
Run the custom build script to ensure CSP hashes are generated and added to Netlify headers:
npm run build
Netlify CMS can run in any frontend web environment, but the quickest way to try it out is by running it on a pre-configured starter site with Netlify. Use the button below to build and deploy your own copy of the repository:
After clicking that button, youβll authenticate with GitHub and choose a repository name. Netlify will then automatically create a repository in your GitHub account with a copy of the files from the template. Next, it will build and deploy the new site on Netlify, bringing you to the site dashboard when the build is complete. Next, youβll need to set up Netlifyβs Identity service to authorise users to log in to the CMS.