Skip to content

Shopify app template on Next.js with app directory

License

Notifications You must be signed in to change notification settings

adamjdavies/shopify-next-app

 
 

Repository files navigation

Shopify App Template - Next.js App Router

This is a template for building a Shopify app using Next.js and Typescript. It contains the basics for building a Shopify app. This template was modified from the original example node app to use a single Next.js native application.

This template has been further altered to support the NextJS App Router.

Rather than cloning this repo, you can use your preferred package manager and the Shopify CLI with these steps.

Next.js and Shopify Embedded Apps

The goal of this template is to provide a quick and easy way to spin up a Shopify Embedded App that uses the Next.js app router platform. Some of the following information was previusly necessary for the pages router, so I am working on migrating some of the legacy code.

Providers

  • in _app.tsx there are a number of Providers which are needed to get everything established for an Embedded App
    • AppBridgeProvider: This sets up AppBridge and resolves the host (from next/router) and API Key (from environment variables).
    • APIProvider: This is just an optional helper for accessing the API routes with session tokens from Shopify.
    • ApolloProvider: Sets up the Apollo context for running Graphql queries and mutations. This runs through the /api/graphql Next.js route and is handled by the Shopify API library.
    • SessionProvider: This ensures that the user always has an active session and that the app is installed correctly. It basically redirects the user to authenticate when it needs to.
      • In order to accomplish this, a request is sent to /api/auth/verify on every page load (client side). This was done client side to preserve the Automatic Static Optimization features in Next.js (only for pages that aren't the install page)
      • This route checks for online and offline tokens existing, validating online tokens and scope mismatches.

OAuth

OAuth is handled using the /api/auth and /api/auth/callback routes. The app is setup to use both online and offline tokens, by default.

Note that in order to use the /api/graphql route out of the box, you need to use online tokens.

Environment Variables

There are a couple environment variables you need to set up in order for the app to run. Create a file called .env.local in the /web directory (or the root of your Next.js app) and add the following lines;

MONGODB_URI= # mongoDB uri goes here

The first two variables are automatically populated by the Shopify CLI.

Todo

  • ✅ Session saving using MongoDB
  • ✅ OAuth flow for online and offline tokens
  • ✅ GraphQl call using Apollo
  • ✅ New router config for Next.js and App Bridge
  • ✅ AppUninstalled webhook - cleanup and delete sessions
  • ⬜ Prune excess leftover unused code

Benefits

Shopify apps are built on a variety of Shopify tools to create a great merchant experience. The create an app tutorial in our developer documentation will guide you through creating a Shopify app using this template.

The Node app template comes with the following out-of-the-box functionality:

  • OAuth: Installing the app and granting permissions
  • GraphQL Admin API: Querying or mutating Shopify admin data
  • REST Admin API: Resource classes to interact with the API
  • Shopify-specific tooling:
    • AppBridge
    • Polaris
    • Webhooks

Tech Stack

This template combines a number of third party open-source tools:

The following Shopify tools complement these third-party tools to ease app development:

  • Shopify API library adds OAuth to the Express backend. This lets users install the app and grant scope permissions.
  • App Bridge React adds authentication to API requests in the frontend and renders components outside of the App’s iFrame.
  • Axios for simple http requests for interacting with the API (Shopify or Custom API routes through Next API Pages).
  • Apollo for interacting with the Shopify GraphQL API.
  • Polaris React is a powerful design system and component library that helps developers build high quality, consistent experiences for Shopify merchants.
  • Custom hooks make authenticated requests to the Admin API.

Getting started

Requirements

  1. You must download and install Node.js if you don't already have it.
  2. You must create a Shopify partner account if you don’t have one.
  3. You must create a development store if you don’t have one.

Installing the template

This template can be installed using your preferred package manager:

Using yarn:

yarn create @shopify/app --template https://github.com/ozzyonfire/shopify-next-app.git

Using npx:

npx @shopify/create-app@latest --template https://github.com/ozzyonfire/shopify-next-app.git

This will clone the template and install the required dependencies.

Local Development

The Shopify CLI connects to an app in your Partners dashboard. It provides environment variables, runs commands in parallel, and updates application URLs for easier development.

You can develop locally using your preferred package manager. Run one of the following commands from the root of your app.

Using yarn:

yarn dev

Using npm:

npm run dev

Using pnpm:

pnpm run dev

Open the URL generated in your console. Once you grant permission to the app, you can start development.

Deployment

Application Storage

This template uses MongoDB for accessing and managing sessions. It uses a custom written solution to handle connections to the MongoDB database in the Next.js development and production environment.

The database that works best for you depends on the data your app needs and how it is queried. You can run your database of choice on a server yourself or host it with a SaaS company. Here’s a short list of databases providers that provide a free tier to get started:

Database Type Hosters
MySQL SQL Digital Ocean, Planet Scale, Amazon Aurora, Google Cloud SQL
PostgreSQL SQL Digital Ocean, Amazon Aurora, Google Cloud SQL
Redis Key-value Digital Ocean, Amazon MemoryDB
MongoDB NoSQL / Document Digital Ocean, MongoDB Atlas

To use one of these, you need to change your session storage configuration. To help, here’s a list of SessionStorage adapters.

Build

The frontend is a single page app. It requires the SHOPIFY_API_KEY, which you can find on the page for your app in your partners dashboard. Paste your app’s key in the command for the package manager of your choice:

Using yarn:

cd web/frontend/ && SHOPIFY_API_KEY=REPLACE_ME yarn build

Using npm:

cd web/frontend/ && SHOPIFY_API_KEY=REPLACE_ME npm run build

Using pnpm:

cd web/frontend/ && SHOPIFY_API_KEY=REPLACE_ME pnpm run build

I can't get past the ngrok "Visit site" page

When you’re previewing your app or extension, you might see an ngrok interstitial page with a warning:

You are about to visit <id>.ngrok.io: Visit Site

If you click the Visit Site button, but continue to see this page, then you should run dev using an alternate tunnel URL that you run using tunneling software. We've validated that Cloudflare Tunnel works with this template.

To do that, you can install the cloudflared CLI tool, and run:

# Note that you can also use a different port
cloudflared tunnel --url http://localhost:3000

In a different terminal window, navigate to your app's root and call:

# Using yarn
yarn dev --tunnel-url https://tunnel-url:3000
# or using npm
npm run dev --tunnel-url https://tunnel-url:3000
# or using pnpm
pnpm dev --tunnel-url https://tunnel-url:3000

Developer resources

About

Shopify app template on Next.js with app directory

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 95.3%
  • CSS 4.2%
  • JavaScript 0.5%