![Rodney Lab gatsby-functions-fauna Github banner](./images/rodneylab-github-gatsby-functions-fauna.png)
This is a demo site for showing how the Fauna data API can be used with brand new Gatsby Serverless Functions.
Open the test site at gatsbyfunctionsfaunamain.gatsbyjs.io. Open up one of the blog posts and then add a comment to the post. If your comment passes spam detection, a brand new static site, including your comment will build automatically. Keeping everything static keeps the page fast. We eliminate the need for using Java Script heavy, external services for managing the comments. API calls to Fauna are only invoked by the build server and serverless functions, freeing up resources on the user's device and creating a top notch user experience.
Features:
- Comments are stored using Fauna data API as a database. These are ingested into the Gatsby GraphQL data layer at build, meaning all comments are static. This innovation brings comments to a JAMStack site without the need for Java Script heavy external services which may slow the site down, impacting user experience,
- Forms use React Hook Form with added accessibility attributes,
- Integrated spam check using Akismet, a long-standing, trusted anti-spam solution, used in millions of sites in the WordPress space. The spam stop feature is triggered by a Gatsby Function. The function also fires off a rebuild on comment submission.
-
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-functions-fauna gatsby fauna-blog-comments https://github.com/rodneylab/gatsby-functions-fauna
-
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 fauna-blog-comments 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
fauna-blog-comments
directory in your code editor of choice and editsrc/pages/index.jsx
. Save your changes and the browser will update in real time! -
Customise Config
You will want to update the site
siteTitle
andsiteLanguage
inconfig/website.js
. These data are fed through to site meta tags are helpful to site users with accessibility needs.
A quick look at the top-level files and directories you'll see in a Gatsby 3 project.
.
βββ config/website.js
βββ cypress
βββ node_modules
βββ src
βββ .env.EXAMPLE
βββ .eslintrc.js
βββ .gitignore
βββ .prettierrc
βββ commitlint.config.js
βββ gatsby-browser.js
βββ gatsby-config.js
βββ gatsby-node.js
βββ gatsby-ssr.js
βββ LICENSE
βββ netlify.toml
βββ package-lock.json
βββ package.json
βββ README.md
-
/config
: This directory contains site specific configuration. Run through the two files to check the language matches your locale. Also update the site name. -
/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. -
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. -
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.
Fauna provides transaction databases which are accessible via a cloud API. It is easy to customise the Fauna functionality used in the demo for your own needs. To get started with fauna see this guide. Also take a look at how fauna is used in this demo site:
-
to read data from the fauna database in a query run at site build time in
gatsby-node.js
. -
to create records in the fauna database in
src/api/submit-comment.js
.
Get in touch if I can help with specific questions. Also let me know if you would like to see a blog post on creating a site using fauna.
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 The Only Cloud Built For Gatsby
Gatsby Cloud is an end-to-end cloud platform specifically built for the Gatsby framework that combines a modern developer experience with an optimised, global edge network.
Deploy this starter with one click on Gatsby Cloud:
Be sure to supply all of the build variables listed in .env.EXAMPLE
to Gastby Cloud (see Site Settings -> Environment Variables).