These integration guides are not official documentation and the Strapi Support Team will not provide assistance with them.
What Is Gatsby?
Gatsby creates fast websites by combining React with GraphQL to pull data from various sources like Strapi. It pre-renders pages during build time, delivering static HTML files that load quickly while maintaining security through Jamstack principles.
Gatsby's plugin ecosystem extends functionality while preserving performance. It automatically handles technical optimizations like code splitting, image optimization, and resource prefetching to improve Core Web Vitals.
Want to dive deeper? Check out the official Gatsby documentation.
Why Integrate Gatsby With Strapi?
The Strapi-Gatsby combination delivers exceptional value for developers seeking performance and flexibility. Integrating Gatsby with Strapi continues to gain popularity for good reason.
- Optimized performance: Strapi powers Gatsby’s static site generation with dynamic content, delivering lightning-fast load times.
- Flexible content modeling: Build exactly the data structures your site needs using Strapi’s customizable content types.
- Seamless GraphQL integration: Strapi’s GraphQL API fits naturally into Gatsby’s data layer for clean, efficient querying.
- Secure by design: With no runtime backend connection, your site surface area for attacks is drastically reduced.
- Real-time content updates: Use Strapi webhooks to trigger Gatsby rebuilds automatically when content changes.
- Scalable architecture: Serve static files globally through CDNs to handle massive spikes in traffic with zero backend strain.
- Cost-effective hosting: Static sites are cheaper to host, especially for media-rich, high-traffic projects.
- Powerful plugin ecosystem: Extend Strapi’s functionality with plugins, including tools tailored for Gatsby workflows.
- Modern authoring experience: Strapi’s admin panel is intuitive for editors while giving developers full control.
- Future-ready with Strapi Cloud & Strapi v5: Skip DevOps setup and leverage the latest performance, security, and Gatsby-friendly upgrades.
This isn't just theoretical.
ESC Nasa built a lightning-fast platform for showcasing projects with this combo.
Keep in touch with the latest Strapi and Gatsby updates
How to Integrate Gatsby With Strapi
Combining Strapi with Gatsby gives you speed and flexibility without the headaches. Here's how to integrate these powerful tools.
Prerequisites and Setup
Before diving in, you'll need:
- Node.js (LTS version recommended)
- npm or yarn
- Git
Setting Up a Strapi Project
- Create a new Strapi project:
1npx create-strapi@latest my-strapi-project
- Navigate to your project directory and start the Strapi server:
1cd my-strapi-project
2npm run develop
- Access the Strapi admin panel at
http://localhost:1337/admin
and create your content types and sample content.
Setting Up a Gatsby Project
- Create a new Gatsby project:
1npx gatsby new my-gatsby-site
- Navigate to your Gatsby project directory:
1cd my-gatsby-site
- Install necessary plugins:
1npm install gatsby-source-strapi gatsby-plugin-image gatsby-plugin-sharp gatsby-source-filesystem gatsby-transformer-remark gatsby-transformer-sharp
Connecting Gatsby to Strapi
- Create a
.env.development
file in your Gatsby project root:
1STRAPI_TOKEN=<YOUR_STRAPI_API_TOKEN>
2GATSBY_STRAPI_API_URL=http://localhost:1337
- Replace
<YOUR_STRAPI_API_TOKEN>
with the API token generated in Strapi. - Configure the
gatsby-source-strapi
plugin in yourgatsby-config.js
:
1require("dotenv").config({
2 path: `.env.${process.env.NODE_ENV}`,
3});
4
5module.exports = {
6 plugins: [
7 {
8 resolve: `gatsby-source-strapi`,
9 options: {
10 apiURL: process.env.GATSBY_STRAPI_API_URL,
11 accessToken: process.env.STRAPI_TOKEN,
12 collectionTypes: ['article', 'category'],
13 singleTypes: ['homepage'],
14 },
15 },
16 // Other plugins...
17 ],
18}
Querying and Displaying Content
- Start your Gatsby development server:
1gatsby develop
- Access the GraphiQL explorer at
http://localhost:8000/___graphql
to test your queries. For example:
1{
2 allStrapiArticle {
3 nodes {
4 title
5 content
6 category {
7 name
8 }
9 }
10 }
11}
- Create a new page in your Gatsby project to display the content. For example,
src/pages/articles.js
:
1import React from "react"
2import { graphql } from "gatsby"
3
4const ArticlesPage = ({ data }) => (
5 <div>
6 <h1>Articles</h1>
7 {data.allStrapiArticle.nodes.map(article => (
8 <div key={article.id}>
9 <h2>{article.title}</h2>
10 <p>{article.content}</p>
11 </div>
12 ))}
13 </div>
14)
15
16export const query = graphql`
17 query ArticlesQuery {
18 allStrapiArticle {
19 nodes {
20 id
21 title
22 content
23 }
24 }
25 }
26`
27
28export default ArticlesPage
Handling Media and Images
To optimize images from Strapi, use gatsby-plugin-image
with gatsby-plugin-sharp
. Here's how to display an image from Strapi:
1import React from "react"
2import { graphql } from "gatsby"
3import { GatsbyImage, getImage } from "gatsby-plugin-image"
4
5const ArticlePage = ({ data }) => {
6 const article = data.strapiArticle
7 const image = getImage(article.cover.localFile)
8
9 return (
10 <div>
11 <h1>{article.title}</h1>
12 <GatsbyImage image={image} alt={article.title} />
13 <p>{article.content}</p>
14 </div>
15 )
16}
17
18export const query = graphql`
19 query ArticleQuery($id: String!) {
20 strapiArticle(id: { eq: $id }) {
21 title
22 content
23 cover {
24 localFile {
25 childImageSharp {
26 gatsbyImageData(width: 800)
27 }
28 }
29 }
30 }
31 }
32`
33
34export default ArticlePage
Build and Deploy
When ready to deploy your Gatsby site:
- Build your Gatsby project:
gatsby build
- Deploy the contents of the
public
directory to your hosting platform (Netlify, Vercel, GitHub Pages, etc.).
Remember to set up environment variables on your hosting platform to match those in your .env.development
file.
This setup gives you the best of both worlds—flexible content management through Strapi with Gatsby's static site generation for blazing speed and SEO benefits.
Want your site to update automatically when content changes? Set up webhooks to trigger Gatsby builds when Strapi content updates. Find details on setting up webhooks in the Strapi documentation.
Keep in touch with the latest Strapi and Gatsby updates
Project Example: Build a Portfolio Website with Gatsby and Strapi
Let's look at something real—a portfolio website built with Gatsby and Strapi by coding educator John Smilga. It's the perfect jumping-off point if you're building your own developer or freelancer portfolio.
Project Overview
This portfolio project shows how integrating Gatsby with Strapi creates a dynamic, high-performance website that's also dead simple to manage. It's like having your cake and eating it too.
What makes it special:
- Responsive Design: Makes your work shine on any device.
- Easy Content Management: Update content without needing a CS degree.
- Lightning-Fast Pages: Pages load before visitors finish blinking.
- Customizable Options: Personalize without pulling your hair out.
Technical Implementation
Under the hood, this project runs on Gatsby v3 and Strapi with some clever implementation choices:
- Content Modeling in Strapi: Custom content types for portfolio items, skills, and blog posts let you update content without bugging a developer.
- Gatsby Data Fetching: The GraphQL data layer pulls exactly what's needed—no bloat, no waste.
- Dynamic Page Generation: Each portfolio item and blog post gets its own page, created automatically from your Strapi content.
- Image Optimization: Images load fast and look great on every device—from phones to giant monitors.
Benefits and Outcomes
What makes this combo so powerful?
- Ease of Use: Non-tech team members can update content without breaking things.
- Performance: Visitors don't bounce because they're waiting for slow pages.
- SEO Optimization: Search engines love fast, well-structured sites like this.
- Scalability: Add hundreds of portfolio items without slowing down your site.
Developer Quote
"Integrating Gatsby with Strapi allows developers to quickly build very fast static websites and apps. Both use JavaScript and integrate smoothly. This combination empowers developers to create high-performance sites while giving content creators an easy-to-use interface for updates."
Getting Started
Want to build something similar?
- Start with a basic Gatsby project.
- Set up a Strapi instance for your content management.
- Configure the Gatsby source plugin for Strapi.
- Customize the design and content to fit your personal brand.
This approach lets you focus on what matters—showcasing your work, not fighting with your website.
For step-by-step guidance, check out the official Strapi documentation.
Strapi Open Office Hours
If you have any questions about Strapi 5 or just would like to stop by and say hi, you can join us at Strapi's Discord Open Office Hours Monday through Friday at 12:30 pm - 1:30 pm CST: Strapi Discord Open Office Hours
For more details, visit the Strapi documentation and Gatsby documentation.