These integration guides are not official documentation and the Strapi Support Team will not provide assistance with them.
What Is 11ty?
11ty (Eleventy) is a simple, powerful static site generator built on JavaScript. Unlike other generators like Gatsby, 11ty offers a minimalist, zero-config approach, allowing developers to create fast, efficient websites. It transforms templates, markdown files, and data into static HTML pages, resulting in incredibly fast-loading websites.
When integrated with Strapi, 11ty benefits from Strapi's robust content management system. 11ty supports multiple template languages, including Nunjucks, Liquid, and Handlebars, giving developers flexibility. Its focus on speed and simplicity makes 11ty ideal for projects of any size, providing power without complexity.
Why Integrate 11ty with Strapi
Integrating Strapi with 11ty creates a powerful Jamstack solution, combining both technologies' strengths to deliver fast, secure, and customizable websites. This integration is ideal for developers seeking complete control over content management and front-end performance. If you're new to Jamstack, its benefits include faster load times, enhanced security, and improved SEO.
Key Benefits
- Faster Loading Times: 11ty pre-renders static HTML files to ensure near-instant page loads that improve user experience and SEO rankings.
- Enhanced Security: Static sites have smaller attack surfaces, which can reduce vulnerabilities, especially with Strapi handling content management.
- Improved SEO: Fast-loading, pre-rendered pages are favored by search engines, increasing site visibility.
Seamless Integration
- Flexible Content Management: Strapi offers a user-friendly interface for managing content, while 11ty turns that content into static HTML.
- API Options: Strapi supports both REST and GraphQL APIs, giving you flexible methods to fetch content for your 11ty templates.
- Customization: Control both the back-end (Strapi) and front-end (11ty) to build tailored solutions.
Performance and Security
- Pre-rendered Static HTML: Served directly from a CDN, static HTML ensures lightning-fast page loads and reduces server load.
- Reduced Attack Surface: Static sites have fewer entry points for attackers, enhancing security.
- Scalability: 11ty’s efficient builds and Strapi’s flexible content models handle growth smoothly.
Keep in touch with the latest Strapi and 11ty updates
How to Integrate 11ty with Strapi
Integrating 11ty with Strapi creates a powerful Jamstack solution that combines the flexibility of a static site generator with the content management capabilities of a headless CMS. This guide will walk you through the entire process, from initial setup to deployment. This tutorial will provide the necessary steps if you want to create a blog with 11ty and Strapi.
Prerequisites
Before starting, ensure you have the following installed:
- Node.js (latest LTS version recommended)
- npm or yarn package manager
- Git (optional, but recommended for version control)
Setting Up Strapi
- Create a new directory for your project:
mkdir my-11ty-strapi-project
cd my-11ty-strapi-project
- Create a subdirectory for your Strapi back-end:
mkdir backend
cd backend
- Create a new Strapi project:
npx create-strapi@latest
- Follow the CLI prompts:
- Choose your installation type (Quickstart or Custom)
- Select your preferred database (SQLite is good for development)
- Once installed, start the Strapi server:
1npm run develop
- Navigate to http://localhost:1337/admin to create your admin user and access the Strapi dashboard.
- Create a simple blog content model:
- In the Strapi admin panel, navigate to "Content-Types Builder"
- Click "Create new collection type"
- Name it "Article"
- Add fields: Title (Text), Content (Rich Text), Slug (Text), Published date (Date), Featured image (Media), Author (Relation to Users)
- Save your collection type
- Navigate to "Settings" > "Roles" > "Public" and give read permission to the Article collection
- Create a few sample articles to test with
Setting Up 11ty
- Go back to your project root directory:
1cd ..
2mkdir frontend
3cd frontend
- Initialize a new npm project and install 11ty:
1npm init -y
2npm install @11ty/eleventy
- Create a basic project structure:
1mkdir _data
2mkdir _includes
3mkdir posts
- Create a basic configuration file,
eleventy.js,
in the root of your front-end directory:
1module.exports = function(eleventyConfig) {
2 return {
3 dir: {
4 input: ".",
5 output: "_site",
6 includes: "_includes",
7 data: "_data"
8 }
9 };
10};
Connecting 11ty to Strapi's API
- Install the node-fetch package to make API requests:
1npm install node-fetch
- Create a JavaScript file in the
_data
directory to fetch data from Strapi. Name itarticles.js
:
1const fetch = require('node-fetch');
2
3module.exports = async function() {
4 try {
5 const response = await fetch('http://localhost:1337/api/articles?populate=*');
6 const data = await response.json();
7 return data.data;
8 } catch (error) {
9 console.error('Error fetching articles:', error);
10 return [];
11 }
12};
- Create a base layout in
_includes/base.njk
:
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>{{ title }}</title>
7 <link rel="stylesheet" href="/css/style.css">
8</head>
9<body>
10 <header>
11 <h1>My 11ty Blog with Strapi</h1>
12 <nav>
13 <a href="/">Home</a>
14 </nav>
15 </header>
16 <main>
17 {{ content | safe }}
18 </main>
19 <footer>
20 <p>© {{ new Date().getFullYear() }} My Blog</p>
21 </footer>
22</body>
23</html>
- Create an index page
index.njk
in the root of your front-end directory:
1---
2layout: base.njk
3title: My 11ty Blog with Strapi
4---
5
6<h1>Recent Articles</h1>
7
8<ul class="articles-list">
9{% for article in articles %}
10 <li class="article-item">
11 <h2><a href="/posts/{{ article.slug }}">{{ article.title }}</a></h2>
12 <p>{{ article.publishedAt | date: "%Y-%m-%d" }}</p>
13 </li>
14{% endfor %}
15</ul>
- Create a template for individual articles in
posts/posts.njk
:
1---
2layout: base.njk
3pagination:
4 data: articles
5 size: 1
6 alias: article
7permalink: "posts/{{ article.slug }}/"
8---
9
10<article>
11 <h1>{{ article.title }}</h1>
12 <p>Published: {{ article.publishedAt | date: "%Y-%m-%d" }}</p>
13
14 <div class="article-content">
15 {{ article.content | safe }}
16 </div>
17</article>
Building and Testing Locally
- In the front-end directory, run:
1npx @11ty/eleventy --serve
- This will build your site and start a local development server, typically at http://localhost:8080.
- Make sure your Strapi server is still running (in a separate terminal):
1cd ../backend
2npm run develop
- Visit http://localhost:8080 in your browser to see your blog with content from Strapi.
Preparing for Deployment
- Create a
.env
file in your front-end directory:
1STRAPI_API_URL=http://localhost:1337
- Install the dotenv package:
1npm install dotenv
- Update your
articles.js
data file to use the environment variable:
1require('dotenv').config();
2const fetch = require('node-fetch');
3
4const API_URL = process.env.STRAPI_API_URL || 'http://localhost:1337';
5
6module.exports = async function() {
7 try {
8 const response = await fetch(`${API_URL}/api/articles?populate=*`);
9 const data = await response.json();
10 return data.data;
11 } catch (error) {
12 console.error('Error fetching articles:', error);
13 return [];
14 }
15};
- Add build scripts to your front-end
package.json
:
1"scripts": {
2 "dev": "eleventy --serve",
3 "build": "eleventy"
4}
Deployment Options
There are several ways to deploy your 11ty and Strapi application:
- Deploying Strapi on a VPS or PaaS
- Set up a VPS (like DigitalOcean, Linode) or use a PaaS (like Heroku, Render)
- Follow Strapi's deployment guides for your chosen platform
- Set up environment variables for production database, etc.
- Deploying 11ty on Netlify
- Push your front-end code to a GitHub repository
- Sign up for Netlify and connect your repository
- Configure the build settings:
- Build command:
npm run build
- Publish directory:
_site
- Build command:
- Add environment variables in Netlify's dashboard:
- STRAPI_API_URL=https://your-strapi-backend-url.com
- Deploying Both with Docker
- Create a
docker-compose.yml
file in your project root - Create Dockerfiles for both front-end and back-end
- Use Docker Compose to manage your multi-container application
- Create a
Following this guide on integrating 11ty with Strapi can help you build a fully functional blog or website powered by 11ty and Strapi, ready for deployment. This setup gives you the benefits of a static site (speed, security) with the convenience of a content management system. For a more detailed walkthrough on how to create a blog with 11ty and Strapi, you can refer to our dedicated tutorial.
We invite you to explore our resources to take your 11ty and Strapi project further. You can deploy Strapi now or join our community for support and insights.
Keep in touch with the latest Strapi and 11ty updates
Project Example: Photo Gallery App with Strapi and 11ty
To demonstrate the power of integrating 11ty with Strapi, let’s explore a real-world example: a photo gallery app built using Strapi and 11ty. This project illustrates how combining these technologies creates a visually appealing, fast, and functional web application.
Project Overview
The photo gallery app utilizes the following key components:
- Strapi: Handles content management and API creation
- 11ty: Generates static HTML from templates and data
- Tailwind CSS: Provides a responsive styling framework
- Cloudinary: Manages image storage and optimization
This combination ensures a performant, scalable, and easily maintainable photo
gallery website.
Implementation Highlights
- Content Modeling in Strapi: Strapi creates a custom content type for photo galleries with fields for title, description, and image uploads.
- API Integration: 11ty fetches data from Strapi’s API endpoints to dynamically generate content at build time.
- Static Site Generation: 11ty generates static HTML pages for each gallery and photo, optimizing page load times and SEO.
- Image Optimization: Cloudinary handles image optimization for faster web delivery.
- Responsive Design: Tailwind CSS ensures the photo gallery is responsive across various devices and screen sizes.
Benefits Realized
- Performance: The static site generates extremely fast load times.
- Scalability: New galleries and photos can be added easily without affecting performance.
- Content Management: Strapi’s user-friendly interface allows non-technical users to manage content easily.
- Developer Experience: The separation of content management (Strapi) and presentation (11ty) streamlines the development process..
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, from 12:30 pm to 1:30 pm CST: Strapi Discord Open Office Hours.
For more details, visit the Strapi documentation and the 11ty documentation.