GraphCommerce is a React frontend framework that allows developers to create ecommerce storefronts connected with Magento.
By combining the ecommerce capabilities of Magento and the convenience of React, developers get access to many features such as:
In this tutorial, you will learn how to create an ecommerce store with GraphCommerce. You will discover where the PWA configuration and the payment methods are set up. Finally, you will create your own GraphCMS project and connect it to your GraphCommerce project. You can jump to any of these steps:
The starter project for GraphCommerce is in the GraphCommerce GitHub repository under the examples folder.
To get the repository, start by cloning it:
git clone https://github.com/graphcommerce-org/graphcommerce.git
Once that’s done, you should have a folder called graphcommerce
. To get the demo project, you should create a project beside your GraphCommerce one (not inside of it as you will be deleting the graphcommerce
folder).
To create that folder, run the following command:
mkdir pwa-graphcommerce
Then, copy the starter project inside graphcommerce/examples/magento-graphcms/
into the folder you previously created. You can also delete the graphcommerce
folder as you no longer need it.
cp -R graphcommerce/examples/magento-graphcms/. pwa-graphcommerce && rm -rf graphcommerce
With your new folder filled with all the necessary files, you can move inside of it.
cd pwa-graphcommerce
When inside your pwa-graphcommerce
folder, you can create the .env
necessary to run your application. You can copy it directly from the .env.exampl
e file supplied to you.
cp -R .env.example .env
You should also remove some of the unnecessary files and folders in your project.
rm CHANGELOG.md rm -rf node_modules && rm -rf .next
Once you have finished step 1, you are ready to launch the application. To do so, you should install all the dependencies with:
yarn
Once the dependencies are installed, you need to also generate your client code based on your GraphQL schema and query documents. You can accomplish that by running the command:
yarn codegen
Finally, you can start your development server.
yarn dev
If you visit http://localhost:3000, you should see this:
Below are some things to note as you launch your GraphCommerce project.
The PWA functionalities are offered through the library next-pwa. When you start your development server, you should see this in the server log:
[next] > [PWA] PWA support is disabled
You can see the configuration in your next.config.js
. Inside you should see:
const nextConfig = { ... pwa: { dest: 'public', disable: process.env.NODE_ENV === 'development', }, ... }
Remove the disable
line to enable PWA support on your local server.
The GraphCommerce project offers 2 payments methods for you to use:
If you would like to remove one of them, you will need to remove it from your package.json
first.
Finally, head into your pages/checkout/payment.tsx
and remove their references in this file.
Your project is currently using a demo Magento and GraphCMS. You can change that by configuring the API Keys in the .env
file. In this section, you will discover how to create your own GraphCMS and link it to your project.
To start with, register to Hygraph and clone the default starter project. To do it, go to this address to clone the project: https://app.hygraph.com/clone/caddaa93cfa9436a9e76ae9c0F34d257
Once created, you can get the necessary API endpoint from your Project Settings (the cog in the left-hand side navigation) and then in API Access. In this page, you will see the endpoints section. From there, you can copy the Content API endpoint.
Then, head to your .env
file and replace the GRAPHCMS_URL
demo URL with your new URL.
You will have to kill your server and regenerate the files with yarn codegen
.
Once that is set up, you can change your content and see the changes reflected. For example, head to Content > Blogposts. Change the title of one of your posts to Test
.
Once you go back to your app, you should see the changes reflected.
GraphCommerce is a frontend framework built in React. Its purpose is to offer a PWA for Magento 2 developers. With GraphCommerce, developers can create and customize their storefront quickly and easily.
In this tutorial, you learned how to set up and launched a GraphCommerce app using demo data. Then, you discovered how to create your own GraphCMS instance and link it to your GraphCommerce application. Enjoy testing out your new application.
Debugging Next applications can be difficult, especially when users experience issues that are difficult to reproduce. If you’re interested in monitoring and tracking state, automatically surfacing JavaScript errors, and tracking slow network requests and component load time, try LogRocket.
LogRocket is like a DVR for web and mobile apps, recording literally everything that happens on your Next.js app. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. LogRocket also monitors your app's performance, reporting with metrics like client CPU load, client memory usage, and more.
The LogRocket Redux middleware package adds an extra layer of visibility into your user sessions. LogRocket logs all actions and state from your Redux stores.
Modernize how you debug your Next.js apps — start monitoring for free.
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowDing! You got a notification, but does it cause a little bump of dopamine or a slow drag of cortisol? […]
A guide for using JWT authentication to prevent basic security issues while understanding the shortcomings of JWTs.
Auth.js makes adding authentication to web apps easier and more secure. Let’s discuss why you should use it in your projects.
Compare Auth.js and Lucia Auth for Next.js authentication, exploring their features, session management differences, and design paradigms.
2 Replies to "Build a PWA with GraphCommerce and Next.js"
Good Read. Thank you for this
Thank you for your comment! I am happy you liked it.