Strapi is an open-source headless CMS based on Node.js. It lets you develop and manage any content of your application. This allows you to build backend API faster with efficiency and customizability advantages.
On the other side, Remix is a good choice to consume your generated API endpoints. It offers data loading on the server side. Remix is a server-side rendered framework. It allows you to work seamlessly with the server and the client. Remix closely compare to Next.js framework.
Next.js is one of the popular React frameworks for server-side rendering. Traditionally, React apps are rendered on the client side. When the initial request is sent from the web app, the browser is sent a shell of a bare HTML page that has no pre-rendered content. The browser would finally fetch the JavaScript file responsible for rendering the actual HTML page.
With the introduction of Server-Side Rendering (SSR) frameworks such as Next.js and Remix, a page is fully rendered on-demand on the server. This approach fits dynamic content that changes frequently.
Some of the reasons Remix is a good choice include:
Remix and Next.js are closely related, and the difference between them is minimal. Check this guide to get a bigger picture of how is Remix different from Next.js.
To continue in this article, it is important to have the following:
Let's dive in and create a Strapi CMS backend. This will serve the blog posts to the users. To set up Strapi CMS, create a project folder. Then, open a command that points to this directory, preferably using a text editor such as the Visual Studio Code. Run the following command to install Strapi CMS on your local computer:
npx create-strapi-app@latest blog-app
Ensure to execute this command based on the following installation type:
This will create a blog-app
folder that will host all the project files and dependencies that Strapi CMS will need to run locally. Once the installation is done, you will be redirected to the Strapi admin page on your default browser at http://localhost:1337/admin/auth/register-admin
.
Provide the login credentials to access the Strapi admin. If you haven't created an account yet, provide Sign up details. Finally, click the Let's start button to access the admin dashboard.
Here, we are creating a blog app. Therefore, we need to model the blog app backend content with Strapi. The first step is to set up the content type. On the Strapi admin panel, click Content-Type Builder.
Then click the + Create new collection type button to set up the collection. Enter blog
as the content type name:
Click Continue and set up the following blog fields:
Finally, create some test content for the above collection and its fields. Go ahead and click the + Create new entry button:
Fill in all the fields, and then make sure you publish the added content:
Make sure you create at least three different blog posts using the same procedure as described above.
Strapi CMS runs a decoupled application structure. To access its content, you need to set up permissions and roles that define what data should be exposed to your user channels.
In this example, we need Remix to interact with the collection we have created. Thus, we need to set up access to this collection so that Remix can execute the right request and get the expected responses.
To access the created blog
collection, generate an API token that Remix will use to communicate with Strapi. To create the token:
Fill in the name and description in the respective fields. Finally, select a Read-only token type and then click save.
In this example, Remix will only read the content of our collection. If you need to add operations such as writing to Strapi CMS, ensure you modify your token type accordingly.
The API token will be generated. Copy the created token as we will use it with Remix.
Let's now create a frontend application to consume the content we have created with Strapi. On your project directory, you created earlier, run the following command to set up Remix:
npx create-remix@latest remix-blog-app
The above command will create a remix-blog-app
folder that will host all the project files and dependencies that Remix will need to run a local web app.
Once the installation is done, proceed to the newly created directory:
cd remix-blog-app
You can test if the scaffolded Remix application works correctly by running:
npm run dev
Open http://localhost:3000/
on a browser and you should be served with a hello world version of the Remix web app.
In the project Remix root directory (remix-blog-app
), create a .env
file and add in the following Remix configurations:
1STRAPI_API_TOKEN="your_access_token"
2STRAPI_URL_BASE="http://your_local_ip_address:1337"
Paste in your access token from the Strapi dashboard to replace the your_access_token
.
Components set the application user interface. Remix uses components to set up different sections of a web application. Here will create the following components:
To set up these components, create a components
folder in the app
directory of the Remix project. Inside the components
folder, create the following files and the respective code blocks to create the components:
Navbar.jsx
:1 export default function Navbar() {
2 return (
3 <nav className="navbar">
4 <div className="nav-wrapper">
5 <a href="/" className="brand-logo">Blog App</a>
6 </div>
7 </nav>
8 )
9 }
Here, we are creating a Navbar
that displays the application brand logo as a Blog App
text. A click on this brand logo will redirect the user to the application home page.
BlogCard.jsx
:1 import { Link } from '@remix-run/react';
2 import url from '../utils/url';
3
4 export default function BlogCard({ blog }) {
5 let data = blog.attributes;
6 return (
7 <div className="card">
8 <div className="card-content">
9 <div className="card-img">
10 <img src={`${url}${data.hero.data.attributes.url}`} alt={data.hero.data.attributes.alternativeText} />
11 </div>
12 <div className="card-details">
13
14 <Link to={`/posts/${blog.id}`} className="card-title">
15 {data.title}
16 </Link>
17
18 <p className="card-excerpt">{data.excerpt}</p>
19 </div>
20 </div>
21 </div>
22 )
23 }
This BlogCard
will display and arrange blogs in your application. Here, each post card will display the post title, hero image, and the post excerpt.
Layout.jsx
:1 import Navbar from './Navbar';
2
3 export default function Layout({ children }) {
4 return (
5 <div style={{ fontFamily: "system-ui, sans-serif", lineHeight: "1.4" }}>
6 <Navbar />
7 <div className="container">
8 {children}
9 </div>
10 </div>
11 );
12 }
The Layout
component runs a container that wraps the Navbar component to the main application.
style.css
To style the above components, add the following CSS code to a style.css
file:
1 /* Navbar */
2 .navbar {
3 box-shadow: 0 1px 0 rgba(0, 0, 0, 0.05);
4 margin-bottom: 0;
5 padding: 12px;
6 position: relative;
7 }
8
9 .navbar .nav-wrapper a {
10 text-decoration: none;
11 font-size: 20px;
12 }
13
14 /* Layout */
15 .container {
16 width: 50%;
17 margin: 0px auto;
18 }
19
20 /* Blog card */
21 .card {
22 width: 100%;
23 padding: 10px;
24 margin: 10px 0px;
25 border-radius: 5px;
26 box-shadow: 0 1px 3px 0 #d4d4d5, 0 0 0 1px #d4d4d5;
27 }
28
29 .card-content {
30 width: 100%;
31 display: flex;
32 justify-content: space-between;
33 }
34
35 .card-content .card-img {
36 width: 50%;
37 height: 100%;
38 }
39
40 .card-img img {
41 width: 90%;
42 height: 100%;
43 border-radius: 5px;
44 }
45
46 .card-details .card-title {
47 text-decoration: none;
48 }
index.jsx
:To execute the above components, create an index.jsx
file and export it, as shown in the following code block:
1 import BlogCard from "./BlogCard";
2 import Layout from "./Layout";
3 import Navbar from "./Navbar";
4 export { BlogCard, Layout, Navbar };
Exporting these components makes it easier for other application modules to access and use them according.
Create a utils
folder inside the Remix app
directory. Inside the utils
folder, create the following files:
errorHandling.js
: For handling exemptions when connecting to the Strapi backend API:1 // Custom error class for errors from Strapi API
2 class APIResponseError extends Error {
3 constructor(response) {
4 super(`API Error Response: ${response.status} ${response.statusText}`);
5 }
6 }
7
8 export const checkStatus = (response) => {
9 if (response.ok) {
10 // response.status >= 200 && response.status < 300
11 return response;
12 } else {
13 throw new APIResponseError(response);
14 }
15 }
16
17 class MissingEnvironmentVariable extends Error {
18 constructor(name) {
19 super(`Missing Environment Variable: The ${name} environment variable must be defined`);
20 }
21 }
22
23 export const checkEnvVars = () => {
24 const envVars = [
25 'STRAPI_URL_BASE',
26 'STRAPI_API_TOKEN'
27 ];
28
29 for (const envVar of envVars) {
30 if (!process.env[envVar]) {
31 throw new MissingEnvironmentVariable(envVar)
32 }
33 }
34 }
url.js
: Host Strapi localhost URL.1 export default "http://localhost:1337";
To access the posts, we'll create Remix routes. In this case, we will have two major routes:
Let's create these routes.
Navigate to the app/routes/index.jsx
file and fetch all the blog posts as follows:
1 import { useLoaderData } from '@remix-run/react';
2 import { checkEnvVars, checkStatus } from '../utils/errorHandling';
3 import {Layout, BlogCard} from '../components';
4 import styles from '../components/style.css';
1export const links = () => [
2 { rel: "stylesheet", href: styles },
3 ];
1 export async function loader() {
2 checkEnvVars(); // check environmental variables
3 const response = await fetch(`${process.env.STRAPI_URL_BASE}/api/blogs?populate=hero`, {
4 method: "GET",
5 headers: {
6 "Authorization": `Bearer ${process.env.STRAPI_API_TOKEN}`,
7 "Content-Type": "application/json"
8 }
9 }); // get the blogs
10
11 checkStatus(response); // check the status
12
13 const data = await response.json(); // get the json response
14
15 if (data.error) { // error check
16 throw new Response("Error loading data from strapi", { status: 500 });
17 }
18
19 return data.data; // return the data
20 }
1export default function Index() {
2 const blogs = useLoaderData();
3 return (
4 <Layout>
5 {
6 blogs.length > 0 ? (
7 blogs.map(blog => (
8 <BlogCard key={blog.id} blog={blog} />
9 ))
10 ) : (
11 <p>No blog posts found!</p>
12 )
13 }
14 </Layout>
15 );
16 }
This will be enough to display the blog posts on Remix. To test if it works as expected, ensure that the Remix development server is up and running using the following command:
npm run dev
This will serve your application on http://localhost:3000
. Use this link and open the application on a browser. Depending on the posts added, your page should be similar to:
At this point, Remix can serve the blog posts. However, we can't access the content on every single post. To do so:
posts
directory inside app/routes
directory.$postId.jsx
file in the posts
directory.$postId.jsx
file, import the necessary modules:1 import { useLoaderData } from '@remix-run/react';
2 import { checkEnvVars, checkStatus } from '../../utils/errorHandling';
3 import url from '../../utils/url';
4 import { Layout } from '../../components';
5 import styles from '../../components/style.css';
1 export const links = () => [
2 { rel: "stylesheet", href: styles },
3 ];
1 export async function loader({ params }) {
2 const { postId } = params; // get the post id
3 checkEnvVars(); // check the environmental variables
4 const response = await fetch(`${process.env.STRAPI_URL_BASE}/api/blogs/${postId}?populate=hero`, {
5 method: "GET",
6 headers: {
7 "Authorization": `Bearer ${process.env.STRAPI_API_TOKEN}`,
8 "Content-Type": "application/json"
9 }
10 }); // send a request to strapi backend to get the post
11
12 checkStatus(response); // check the response status
13
14 const data = await response.json(); // get the json data
15
16 if (data.error) {// check if we have an error
17 throw new Response("Error loading data from strapi", { status: 500 });
18 }
19
20 return data.data; // return the data
21 }
1 export default function Post() {
2 const blog = useLoaderData();
3 const blogData = blog.attributes;
4 return (
5 <Layout>
6 <div className="blog-post">
7 <div className="blog-post-hero">
8 <img src={`${url}${blogData.hero.data.attributes.url}`} alt={`${blogData.hero.data.attributes.alternativeText}`} />
9 </div>
10 <div className="blog-post-title">
11 <h1>{blogData.title}</h1>
12 </div>
13 <div className="blog-post-content">
14 <div dangerouslySetInnerHTML={{ __html: blogData.content }} />
15 </div>
16 </div>
17 </Layout>
18 )
19 }
Ensuring that the development server is up and running, click on any post on the home page. You will be directed to a page with the content of each specific blog as shown below:
Strapi CMS is used to develop and manage the content of any application. It helps you scaffold any API faster and consume the content via Restful APIs and GraphQL. In this guide, we have created a Strapi Restful API and then consumed it to build a minimalistic Remix blog application.
You can get the source code used on this project on the GitHub repository for the Remix Frontend part.
Joseph is fluent in Android Mobile Application Development and has a lot of passion for Fullstack web development. He loves to write code and document them in blogs to share ideas.