/gatsby-functions-fauna

A demo site to show how the Fauna data API can be used with Gatsby Cloud Functions.

Primary LanguageJavaScriptBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Rodney Lab gatsby-functions-fauna Github banner

Rodney Lab logo

Gatsby Functions Fauna

Open in Visual Studio Code

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.

πŸš€ Quick start

  1. 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
  2. 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
  3. 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 edit src/pages/index.jsx. Save your changes and the browser will update in real time!

  4. Customise Config

    You will want to update the site siteTitle and siteLanguage in config/website.js. These data are fed through to site meta tags are helpful to site users with accessibility needs.

🧐 What's inside?

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
  1. /config: This directory contains site specific configuration. Run through the two files to check the language matches your locale. Also update the site name.

  2. /cypress: This directory contains Cypress axe accessibility end-to-end tests. You can expand the tests to suit your needs.

  3. /node_modules: This directory contains all of the modules of code that your project depends on (npm packages) are automatically installed.

  4. /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”.

  5. .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.

  6. .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.

  7. .gitignore: This file tells git which files it should not track / not maintain a version history for.

  8. .prettierrc: This is a configuration file for Prettier. Prettier is a tool to help keep the formatting of your code consistent.

  9. .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.

  10. commitlint.config.js: Configuration for commitlint which is used to enforce conventional git commits. This works in tandem with husky configuration in the package.json file.

  11. 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.

  12. 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:

  1. 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.

  2. 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.

  3. LICENSE: This Gatsby 3 starter is licensed under the BSD-3-Clause license.

  4. package-lock.json (See package.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).

  5. 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.

  6. README.md: A text file containing useful reference information about your project.

☁️ Getting Started with Fauna

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:

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.

πŸŽ“ Learning Gatsby 3

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.

πŸ’« Deploy

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:

Deploy to Gatsby Cloud

Be sure to supply all of the build variables listed in .env.EXAMPLE to Gastby Cloud (see Site Settings -> Environment Variables).