GatsbyJS is a popular static site generator built on top of React. It allows you to create blazing fast websites and applications with minimal configuration. In this tutorial, we will explore the file structure and boilerplate code used in GatsbyJS projects.
File Structure:
Gatsby projects have a predictable file structure that includes a few key directories and files. Here is an overview of the standard file structure in a Gatsby project:
-
src/: This directory is where all of your website’s source code lives. It contains subdirectories for pages, components, styles, and other resources.
-
pages/: This directory is where you will create your website’s pages. Each JavaScript file in this directory represents a page on your website. For example, src/pages/index.js will be the homepage of your site.
-
components/: This directory is where you will store your React components. These components can be reused throughout your website to create consistent designs and functionality.
-
styles/: This directory is where you can store your CSS or SCSS files. You can import these styles into your components to style your website.
-
images/: This directory is where you can store images used in your website. You can import these images into your components to display them on your site.
-
static/: This directory is where you can store static assets like fonts, videos, or other files that need to be served as-is without processing.
-
gatsby-config.js: This file is the main configuration file for your Gatsby project. It contains metadata about your project and plugins that you want to use.
- gatsby-node.js: This file is where you can customize the build process of your Gatsby site. You can create pages dynamically, manipulate data, and more in this file.
Boilerplate Code:
Now that we have an understanding of the file structure in a Gatsby project, let’s look at some boilerplate code that you can use to kickstart your project. Here is an example of a simple Gatsby project structure:
my-gatsby-project/
âââ src/
â âââ pages/
â â âââ index.js
â âââ components/
â â âââ Layout.js
â âââ styles/
â â âââ styles.css
âââ static/
âââ gatsby-config.js
âââ gatsby-node.js
In the above structure, we have a basic Gatsby project with an index page, a layout component, and a styles file. Here is some example code to populate these files:
- index.js (src/pages/index.js):
import React from 'react'
import Layout from '../components/Layout'
const IndexPage = () => (
<Layout>
<h1>Welcome to my Gatsby site!</h1>
<p>This is a simple Gatsby project.</p>
</Layout>
)
export default IndexPage
- Layout.js (src/components/Layout.js):
import React from 'react'
import '../styles/styles.css'
const Layout = ({ children }) => (
<div className="container">
{children}
</div>
)
export default Layout
- styles.css (src/styles/styles.css):
.container {
max-width: 800px;
margin: 0 auto;
padding: 0 20px;
}
With this boilerplate code in place, you can start building your Gatsby site. You can add more pages, components, styles, and assets as needed to create a fully functional website or application.
In conclusion, Gatsby offers a powerful and flexible way to build static sites with React. By understanding the file structure and using boilerplate code, you can quickly get started with Gatsby and create stunning websites. Happy coding!