Simply copy and paste the following command line in your terminal to create your first Strapi project.
npx create-strapi-app
my-project
Headless Content Management Systems are powerful in many ways; they give us the flexibility to do whatever we want to do with our preferred frontend technology. Strapi is one of the most popular headless CMSes out there, and it makes working with the backend side of things a breeze.
This tutorial will discuss how to make a photo gallery with Strapi and Next.js, using Cloudinary to store our images.
To follow along with this tutorial, you should have the following:
A Cloudinary account
One of the first things you will need to do is to create a free account on Cloudinary. Once you have successfully created your free account, you will be redirected to the management dashboard of your account. On the dashboard page, you will find your Account Details, which you will need to save for later:
Make sure to keep these details secret and do not share them with anyone.
After creating a Cloudinary account, it is time to install your Strapi instance. Run the following command:
1
2
3
yarn create strapi-app strapi-photo --quickstart
#OR
npm create strapi-app strapi-photo --quickstart
This command will create a folder named strapi-photo
and install the Strapi instance to it.
After installation, Strapi will automatically run a build command at http://localhost:1337/admin
, redirecting you immediately http://localhost:1337/admin/auth/register-admin
because it is the first time you are starting it. You will need to register yourself as a superuser
.
Now, it is time to create your first collection. Click on Content-Types Builder and then click on Create new collection type.
Type Photos for your Display name and click on the Continue button to add fields.
We will be adding four fields, which are name, date, location, and img. Follow the instructions below:
Before adding data to the Photos collection we have created, we need to connect our Cloudinary account to the Strapi instance. It would help if you stopped the server before you run the command below.
yarn add @strapi/provider-upload-cloudinary
#OR
npm install @strapi/provider-upload-cloudinary
yarn run develop
#OR
npm run develop
*plugins.js*
inside the ***config***
folder, and paste the following code into it:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module.exports = ({ env }) => ({
upload: {
config: {
provider: 'cloudinary',
providerOptions: {
cloud_name: env('CLOUDINARY_NAME'),
api_key: env('CLOUDINARY_KEY'),
api_secret: env('CLOUDINARY_SECRET'),
},
actionOptions: {
upload: {},
delete: {},
},
},
},
});
Add the following variables in the .env
file. Fill the missing values with the corresponding values found in your Cloudinary dashboard under Account Details, and make sure to restart your server.
CLOUDINARY_NAME=xxxxxxxxxxxxxxxxxxxxxx CLOUDINARY_KEY=xxxxxxxxxxxxxxxxxx CLOUDINARY_SECRET=xxxxxxxxxxxxxxxx
If you using Strapi v3, you might likely run into an error when you restart your server:
1
2
3
2022-03-22 15:23:23.455] debug: Server wasn’t able to start properly.
[2022-03-22 15:23:23.456] error: Middleware “strapi::session”: App keys are required. Please set app.keys in config/server.js (ex: keys: [‘myKeyA’, ‘myKeyB’])
Error: Middleware “strapi::session”: App keys are required. Please set app.keys in config/server.js (ex: keys: [‘myKeyA’, ‘myKeyB’])
To fix this error, ensure your .env
file has the necessary variables which can be random strings in place of the missing values. The content of the .env
file should be similar to the code snippets below:
1
2
3
4
5
6
7
8
9
HOST=0.0.0.0
PORT=1337
APP_KEYS=xxxxxxxxxxxxx,xxxxxxxxxxxxx #Note that this is comma seperated
ADMIN_JWT_SECRET=xxxxxxxxxxxx
API_TOKEN_SALT=xxxxxxxxxxxxxx
CLOUDINARY_NAME=xxxxxxxxxxxx
CLOUDINARY_API_KEY=xxxxxxxxxxxxxxxxx
CLOUDINARY_API_SECRET=xxxxxxxxxxxxxxxx
JWT_SECRET=xxxxxxxxxxxxxxxxxxxxx
After adding the missing variables, the server can be restarted. It is however best that you upgrade to Strapi v4 to avoid such errors in the future.
Go back to your Strapi project at http://localhost:1337/admin and click on Content Manager. Click on Photos, then Add New Photos.
I have decided to use J Cole’s and Vector’s pictures for this. You can use any image you want to follow along. Make sure you save and publish.
I have added four entries.
Log in to your Cloudinary to make sure the images are there.
To make these data available for consumption by any client-side technology, we need to set some roles and permissions — who has access to what and to what extent.
Now go to Settings→(USER & PERMISSION PLUGIN)→Roles→Public
Yes, we have successfully spun up the backend side of things in our application. Now let us use Next.js to consume its API. Exit your Strapi instance folder and run the following command to install Next.js.
yarn create next-app next-photo
#OR
npm create next-app next-photo
This command sets up everything automatically for us (next-photo is my folder name, you can name yours differently)
Move into next-photo
:
cd next-photo
yarn dev
#OR
npm run dev
One of the main benefits of Next.js applications is that everything is pre-rendered or built at first load. At http://localhost:3000, we should see a default Next.js instance:
Since we will be working with images from an external source, Cloudinary, we need to configure the *next.config.js*
file for image optimization that NextJS provides. Make sure to upload images greater than the sizes listed below for better optimization.
1
2
3
4
5
6
7
8
9
const nextConfig = {
//..
images: {
deviceSizes: [320, 420, 768, 1024, 1200],
loader: "default",
domains: ["res.cloudinary.com"],
},
}
module.exports = nextConfig
Now, we are going to create a components
folder and ImageDetail.js
file within it. Paste the following code inside:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import Image from "next/image";
import Link from "next/link";
export default function Gallery({ thumbnailUrl, title, id }) {
return (
<div>
<Link as={`/preview/${id}`} href="/preview/[id]">
<a>
<Image width={250} height={200} src={thumbnailUrl} />
<div className="photoid"> {title}</div>
</a>
</Link>
</div>
);
}
After importing Image
and Link
from next
, a gallery-component
has three props ( thumbnailUrl, title, id)
and returning a link
that will dynamically redirect to preview/$id
of each photo in our backend. I have decided to make the width and height 250px and 200px, respectively.
Create another folder named preview
in the pages
folder and create a file with square brackets like so [id].js
inside the just created folder.
We will come back to this file, but for now, go to your index.js
file in pages
folder and replace the existing code with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Head from "next/head";
import { useState } from "react";
import Gallery from "../components/ImageDetail";
import styles from "../styles/Home.module.css";
export default function Home({ stuff }) {
const [photos, setPhotos] = useState(stuff);
const [search, setSearch] = useState("");
return (
<div className={styles.container}>
<Head>
<title>Photo Gallery</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<div className={styles.fade}>
<div className={styles.gridContainer}>
{photos &&
photos.data.map((detail) => (
<Gallery
key={detail.id}
thumbnailUrl={detail.attributes.img.data.attributes.formats.thumbnail.url}
title={detail.attributes.name}
id={detail.id}
/>
))}
</div>
</div>
</main>
</div>
);
}
export async function getStaticProps() {
const results = await fetch("http://localhost:1337/api/photos?populate=*");
const stuff = await results.json();
return {
props: { stuff },
};
}
We imported and used Gallery
from the ImageDetail.js
in our components
folder. We mapped through every instance of photos states we created. Line 32
is essential here because it uses a Next.js, getStaticProps
, which fetches data at build time from our Strapi instance at http://localhost:1337/api/photos
.
Your application should look like this:
Let us make everything responsive with the following steps.
Home.module.css
in the styles
folder.global.css
in the styles
folder.Your application should now look like this:
We have gotten the home page up and running. It'd be nice to have a search input field where users can find a specific image by its name. This will be most useful when the photos get populated.
In your index.js
file add the following code immediately after the opening of the <main>
tag:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<input
onChange={(e) => setSearch(e.target.value)}
className={styles.searchInput}
type="text"
placeholder="Search for an image"
></input>
<button
className="button"
disabled={search === ""}
onClick={async () => {
const results = await fetch(
`http://localhost:1337/api/photos?populate=*&filters\[name\][$eq]=${search}`
);
const details = await results.json();
setPhotos(await details);
}}
>
Find
</button>
Line 1 to 6
takes care of the input. It targets the value in the input field. Pay attention to what is being fetched at Line 12
. It uses filtering techniques. You can read more about it here. Make sure you had set a search state. Your final index.js
file should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Head from "next/head";
import { useState } from "react";
import Gallery from "../components/ImageDetail";
import styles from "../styles/Home.module.css";
export default function Home({ stuff }) {
const [photos, setPhotos] = useState(stuff);
const [search, setSearch] = useState("");
return (
<div className={styles.container}>
<Head>
<title>Photo Gallery</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<input
onChange={(e) => setSearch(e.target.value)}
className={styles.searchInput}
type="text"
placeholder="Search for an image"
></input>
<button
className="button"
disabled={search === ""}
onClick={async () => {
const results = await fetch(
`http://localhost:1337/api/photos?populate=*&filters\[name\][$eq]=${search}`
);
const details = await results.json();
setPhotos(await details);
}}
>
Find
</button>
<div className={styles.fade}>
<div className={styles.gridContainer}>
{photos &&
photos.data.map((detail) => (
<Gallery
key={detail.id}
thumbnailUrl={detail.attributes.img.data.attributes.formats.thumbnail.url}
title={detail.attributes.name}
id={detail.id}
/>
))}
</div>
</div>
</main>
</div>
);
}
export async function getStaticProps() {
const results = await fetch("http://localhost:1337/api/photos?populate=*");
const stuff = await results.json();
return {
props: { stuff },
};
}
Your application should look like so with the search input and Find button:
When you do a Search and hit Find, this is how it should look:
Now, it is time to take care of what happens when a photo is clicked. Remember that our Gallery component in ImageDetail.js
inside the component
folder has a Link. Clicking on any photos right now will produce this error page:
This is because nothing has been done inside the [id].js
we created inside the preview
folder. Let us fix this. To fix the error, paste the following code inside [id].js
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { useRouter } from "next/router";
import Image from "next/image";
import Link from "next/link";
export default function photo({ photo, location, name, date }) {
const router = useRouter();
if (!router.isFallback && !photo) {
return <ErrorPage statusCode={404} />;
}
return (
<div>
<div className="Imagecontainer">
<Link className="homeButton" href="/">
<a className="homeButton">
<button className="button"> Home </button>
</a>
</Link>
</div>
<div className="Imagecontainer">
{router.isFallback ? (
<div>Loading…</div>
) : (
<>
<Image width={960} priority height={540} src={photo} />
</>
)}
</div>
<div className="Imagecontainer">Name : {name}</div>
<div className="Imagecontainer">Location {location}</div>
<div className="Imagecontainer">Date: {date}</div>
<div className="Imagecontainer">
<Link className="homeButton" href="/">
<a className="homeButton">
<button className="button"> Back </button>
</a>
</Link>
</div>
</div>
);
}
export async function getStaticProps({ params }) {
const photoid = params.id;
const results = await fetch(`http://localhost:1337/api/photos/${photoid}?populate=*`);
const previews = await results.json();
const photo = await previews.data.attributes.img.data.attributes.url;
const name = await previews.data.attributes.name;
const location = previews.data.attributes.location;
const date = await previews.data.attributes.createdAt.toString();
return {
props: { photo, name, location, date },
};
}
export async function getStaticPaths() {
const results = await fetch("http://localhost:1337/api/photos?populate=*");
const previews = await results.json();
return {
paths:
previews?.data.map((pic) => ({
params: { id: pic.id.toString() },
})) || [],
fallback: true,
};
}
I will explain what most parts of this code do.
The getStaticPaths
in from Line 52
is a Next.js primary data-fetching method required because of our application's dynamic routes. Read more about it static generation.
The getStaticProps
will fetch the params.id
defined in getStaticPaths
. Since that is available, we then fetch each id dynamically it JSON in Line 43
before accessing each of the things we need.
Line 27 to 29
displays all other fields (location, name, date) right below the image component showing each image detail in 960px x 540px. Note that we have already defined them as props in Line 4, our photo component.
If you did everything right, you should have yourself something like this yourself when you click any photo.
We set up and connected our Cloudinary account to the Strapi instance. In addition, we played around Strapi and its permissions and roles, thereby creating our collection to suit what we have in mind.
We talked about Next.js and some of its out-of-the-box methods like getStaticProps
and getStaticPaths
. Finally, we were able to put all these together to build our photo gallery app.
The repository for the frontend implementation and backend implementation can be found on Github.
Backend Developer 👩💻 | Technical Writer ✍️