This tutorial is an adaptation of Pierre's Nuxt and Strapi Deliveroo clone.
Disclaimer You can find the updated version of this tutorial
This tutorial will be adapted to use Next.js (React) over Nuxt (Vue) on the front-end, complete with GraphQL, Stripe, Strapi and React Context.
Get ready to develop a Deliveroo clone, using amazing technologies: Next.js (React), GraphQL, Stripe and Strapi! From signup to order, you are going to let users discover restaurants, dishes and select their happy meal.
The demo of the final result should make you hungry:
Note: the source code is available on GitHub.
Screenshots of final product:
Strapi is the most advanced open-source Node.js Headless Content Management System used to build scalable, secure, production-ready API's quickly and efficiently saving developers countless hours of development.
With its extensible plugin system, it provides a large set of built-in features: Admin Panel, Authentication & Permissions management, Content Management, API Generator, etc. Unlike online CMS, Strapi is 100% open-source (take a look at the GitHub repository), which means:
Next is a lightweight development framework to create static, server-rendered applications in React. Next.js will take care of the heavy lifting of the application such as code-splitting, HMR (hot module replacement) and SSR (server-side rendering), and allow us to focus on the application.
React is one of the most popular front-end Javascript frameworks, developed by Facebook and used by countless tech companies including Netflix, Airbnb, and Github to build applications. React is a declarative library that makes it easy to create interactive user interfaces, keeping the code base organized through its component-based architecture.
GraphQL is a query language also developed by Facebook to allow the front end of an application to easily query an application's API. Each query requests only the data needed to be rendered by the current view. This allows the developer to craft a great user experience across multiple devices and screen sizes.
Stripe is one of (if not the largest) payment processors for application today. Stripe has developed the tools and SDKs to allow developers to craft and integrate secure, compliant payment processing into any app with ease.
To setup Next.js we will need an empty directory to install the library and host our project root.
We will split our project into two parts, one for the front end (Next.js code) and one for the backend (Strapi code).
1mkdir strapi-deliveroo
2cd strapi-deliveroo
3mkdir frontend
4cd frontend
5
6yarn add next react react-dom
Note: I am using yarn as my package manager, you can also use npm and execute npm install --save next react react-dom
.
Add the following to your package.json
file:
1"scripts": {
2 "dev": "next",
3 "build": "next build",
4 "start": "next start"
5}
So that your file looks like this (your package dependencies may have different versions depending on the time of install):
1{
2 "dependencies": {
3 "next": "^7.0.2",
4 "react": "^16.6.1",
5 "react-dom": "^16.6.1",
6 },
7 "scripts": {
8 "dev": "next",
9 "build": "next build",
10 "start": "next start"
11 }
12}
Next.js uses any JavaScript file in the /pages
directory as the route for the application. To set up simply create the /pages
directory and add an index.js
file with:
1mkdir pages
2cd pages
3touch index.js
Now that we have our main route (index.js), this will be loaded each time the browser URL is at the root (i.e. www.yourapp.com). To test this you can insert the following into the index.js file:
1export default () => <div>Welcome to next.js!</div>
To view the application running, start the local dev server using:
1yarn dev
Your application should now be visible at http://localhost:3000.
Be sure to create a .gitignore
in the project root and add .next
and node_modules
directories to it:
1cd ..
2touch .gitignore
1/* .gitignore */
2node_modules
3.next
Adding Bootstrap
For this tutorial, we will use react-strap to implement Bootstrap 4 into our application. For the CSS styling we will import directly from a CDN.
First, install Reactstrap:
1yarn add reactstrap bootstrap
reactstrap is simply a front end library to easily create Bootstrap components and styling.
To import the CSS and share a Layout component across all our pages we will use a custom _app.js
file inside the pages directory.
This file will serve to override the default App.js used by Next and be rendered on each page, allowing us to set global styles/shared components in one place.
You can read more about the _app.js
handling here: https://nextjs.org/docs/#custom-app.
This will allow us the ability to import a <Head>
component and globally set the stylesheet inside the header.
1cd pages
2touch _app.js
Path: /frontend/pages/_app.js
Now if we add in some reactstrap components inside of index.js
we should see the bootstrap styling applied once we restart our server.
Path: /frontend/pages/index.js
1<script src="https://gist.github.com/ryanbelke/0c14ca8b247d962e485d82b042c38828.js"></script>
Restart your server to apply the new changes.
Now that we have Bootstrap running inside of our Next.js project, we can begin to style the basic shared front end components like the navbar.
First create a folder to store our components and create our layout component:
1cd ..
2mkdir components
3cd components
4touch Layout.js
Next.js uses the <Link>
component to perform the client-side routing between pages. The Link component is just a Higher Order Component and can accept any HTML tag that can handle an onClick handler (<a>
,<button>
,<div>
etc.).
This will cause us to have to make a few modifications outside of the reactstrap documentation. To style our navbar we can use the built-in CSS in JS <style jsx>
shipped by default with Next.js.
Inserting CSS in JS as:
1<style jsx> {`
2 a { color: yellow }
3`}
4</style>
Allows us to scope CSS to the components the style tag is rendered in, you can also pass in the global option to set a global style: <style jsx global>
You can read more on CSS in JS in the Next documents here.
Open the Layout.js
file and create the shared layout components and insert the Stripe script (for later) as follows:
Path: /frontend/components/Layout.js
Edit the index.js
file to use the new Layout component:
Path: /frontend/pages/index.js
You should now have a shared header bar across all your pages:
We will create two additional pages to allow users to sign in and sign up at /signin
and /signup
respectively.
You will need to create the corresponding files inside the /pages
directory for next to recognize the routes.
1cd ..
2cd pages
3
4touch signin.js
5touch signup.js
Populate the files with the following code that we will come back to once our Strapi server is setup.
Path: /frontend/pages/signup.js
Path: /frontend/pages/signin.js
You should now see the routes at http://localhost:3000.
Having a frontend is good, but your app obviously needs a backend to manage users, restaurants, dishes and orders. To make the magic happen, let's create a Strapi API.
Requirements: please make sure to use Node 9 (or more) and have either MongoDB, Postgres or MySQL installed and running on your machine.
Install Strapi using npm:
1npm i strapi@alpha -g
Note: Strapi v3 is still an alpha version, but it will be fine for this tutorial.
Scaffold your API inside the strapi-deliveroo
through a single command line:
Install a Strapi server in a directory called backend
:
1cd ..
2cd ..
3strapi new backend
The CLI will ask you to choose your database: select MongoDB, Postgres, or MySQL. Then, fill the database information in. The default values should work if you correctly installed the database system on your machine.
Launch the Node.js server:
1cd backend
2strapi start
Starting now, you should be able to visit the admin panel of your project: http://localhost:1337/admin.
Add your first user from the registration page.
Good job, you successfully set up both Next.js and Strapi projects! 🎉
🏠 In the next section, you will learn how to display the list of restaurants: https://blog.strapi.io/strapi-next-restaurants/.
Please note: Since we initially published this blog, we released new versions of Strapi and tutorials may be outdated. Sorry for the inconvenience if it's the case, and please help us by reporting it.
Ryan is an active member of the Strapi community and he's been contributing at a very early stage by writing awesome tutorial series to help fellow Strapier grow and learn.