|
| 1 | +# Shopify App Template - Ruby |
| 2 | + |
| 3 | +This is a template for building a [Shopify app](https://shopify.dev/apps/getting-started) using Ruby on Rails and React. It contains the basics for building a Shopify app. |
| 4 | + |
| 5 | +Rather than cloning this repo, you can use your preferred package manager and the Shopify CLI with [these steps](#installing-the-template). |
| 6 | + |
| 7 | +## Benefits |
| 8 | + |
| 9 | +Shopify apps are built on a variety of Shopify tools to create a great merchant experience. The [create an app](https://shopify.dev/apps/getting-started/create) tutorial in our developer documentation will guide you through creating a Shopify app using this template. |
| 10 | + |
| 11 | +The Ruby app template comes with the following out-of-the-box functionality: |
| 12 | + |
| 13 | +- OAuth: Installing the app and granting permissions |
| 14 | +- GraphQL Admin API: Querying or mutating Shopify admin data |
| 15 | +- REST Admin API: Resource classes to interact with the API |
| 16 | +- Shopify-specific tooling: |
| 17 | + - AppBridge |
| 18 | + - Polaris |
| 19 | + - Webhooks |
| 20 | + |
| 21 | +## Tech Stack |
| 22 | + |
| 23 | +This template combines a number of third party open source tools: |
| 24 | + |
| 25 | +- [Rails](https://rubyonrails.org/) builds the backend. |
| 26 | +- [Vite](https://vitejs.dev/) builds the [React](https://reactjs.org/) frontend. |
| 27 | +- [React Router](https://reactrouter.com/) is used for routing. We wrap this with file-based routing. |
| 28 | +- [React Query](https://react-query.tanstack.com/) queries the Admin API. |
| 29 | + |
| 30 | +These third party tools are complemented by Shopify specific tools to ease app development: |
| 31 | + |
| 32 | +- [Shopify API library](https://github.com/Shopify/shopify-api-ruby) adds OAuth to the Rails backend. This lets users install the app and grant scope permissions. |
| 33 | +- [App Bridge](https://shopify.dev/apps/tools/app-bridge) and [App Bridge React](https://shopify.dev/apps/tools/app-bridge/getting-started/using-react) add authentication to API requests in the frontend and renders components outside of the App’s iFrame. |
| 34 | +- [Polaris React](https://polaris.shopify.com/) is a powerful design system and component library that helps developers build high quality, consistent experiences for Shopify merchants. |
| 35 | +- [Custom hooks](https://github.com/Shopify/shopify-frontend-template-react/tree/main/hooks) make authenticated requests to the Admin API. |
| 36 | +- [File-based routing](https://github.com/Shopify/shopify-frontend-template-react/blob/main/Routes.jsx) makes creating new pages easier. |
| 37 | + |
| 38 | +## Getting started |
| 39 | + |
| 40 | +### Requirements |
| 41 | + |
| 42 | +1. You must [create a Shopify partner account](https://partners.shopify.com/signup) if you don’t have one. |
| 43 | +1. You must create a store for testing if you don't have one, either a [development store](https://help.shopify.com/en/partners/dashboard/development-stores#create-a-development-store) or a [Shopify Plus sandbox store](https://help.shopify.com/en/partners/dashboard/managing-stores/plus-sandbox-store). |
| 44 | +1. You must have [Ruby](https://www.ruby-lang.org/en/) installed. |
| 45 | +1. You must have [Bundler](https://bundler.io/) installed. |
| 46 | +1. You must have [Node.js](https://nodejs.org/) installed. |
| 47 | + |
| 48 | +### Installing the template |
| 49 | + |
| 50 | +This template runs on Shopify CLI 3.0, which is a node package that can be included in projects. You can install it using your preferred Node.js package manager: |
| 51 | + |
| 52 | +Using yarn: |
| 53 | + |
| 54 | +```shell |
| 55 | +yarn create @shopify/app --template=ruby |
| 56 | +``` |
| 57 | + |
| 58 | +Using npx: |
| 59 | + |
| 60 | +```shell |
| 61 | +npm init @shopify/app@latest --template=ruby |
| 62 | +``` |
| 63 | + |
| 64 | +Using pnpm: |
| 65 | + |
| 66 | +```shell |
| 67 | +pnpm create @shopify/app@latest --template=ruby |
| 68 | +``` |
| 69 | + |
| 70 | +This will clone the template and install the CLI in that project. |
| 71 | + |
| 72 | +### Setting up your Rails app |
| 73 | + |
| 74 | +Once the Shopify CLI clones the repo, you will be able to run commands on your app. |
| 75 | +However, the CLI will not manage your Ruby dependencies automatically, so you will need to go through some steps to be able to run your app. |
| 76 | +To make the process easier, the template provides a script to run the necessary steps: |
| 77 | + |
| 78 | +1. Start off by switching to the `web` folder: |
| 79 | + ```shell |
| 80 | + cd web |
| 81 | + ``` |
| 82 | +1. Install the ruby dependencies: |
| 83 | + ```shell |
| 84 | + bundle install |
| 85 | + ``` |
| 86 | +1. Run the [Rails template](https://guides.rubyonrails.org/rails_application_templates.html) script. |
| 87 | + It will guide you through setting up your database and set up the necessary keys for encrypted credentials. |
| 88 | + ```shell |
| 89 | + bin/rails app:template LOCATION=./template.rb |
| 90 | + ``` |
| 91 | + |
| 92 | +And your Rails app is ready to run! You can now switch back to your app's root folder to continue: |
| 93 | + |
| 94 | +```shell |
| 95 | +cd .. |
| 96 | +``` |
| 97 | + |
| 98 | +### Local Development |
| 99 | + |
| 100 | +[The Shopify CLI](https://shopify.dev/apps/tools/cli) connects to an app in your Partners dashboard. |
| 101 | +It provides environment variables, runs commands in parallel, and updates application URLs for easier development. |
| 102 | + |
| 103 | +You can develop locally using your preferred Node.js package manager. |
| 104 | +Run one of the following commands from the root of your app: |
| 105 | + |
| 106 | +Using yarn: |
| 107 | + |
| 108 | +```shell |
| 109 | +yarn dev |
| 110 | +``` |
| 111 | + |
| 112 | +Using npm: |
| 113 | + |
| 114 | +```shell |
| 115 | +npm run dev |
| 116 | +``` |
| 117 | + |
| 118 | +Using pnpm: |
| 119 | + |
| 120 | +```shell |
| 121 | +pnpm run dev |
| 122 | +``` |
| 123 | + |
| 124 | +Open the URL generated in your console. Once you grant permission to the app, you can start development. |
| 125 | + |
| 126 | +## Deployment |
| 127 | + |
| 128 | +### Application Storage |
| 129 | + |
| 130 | +This template uses [Rails' ActiveRecord framework](https://guides.rubyonrails.org/active_record_basics.html) to store Shopify session data. |
| 131 | +It provides migrations to create the necessary tables in your database, and it stores and loads session data from them. |
| 132 | + |
| 133 | +The database that works best for you depends on the data your app needs and how it is queried. |
| 134 | +You can run your database of choice on a server yourself or host it with a SaaS company. |
| 135 | +Once you decide which database to use, you can configure your app to connect to it, and this template will start using that database for session storage. |
| 136 | + |
| 137 | +### Build |
| 138 | + |
| 139 | +The frontend is a single page React app. |
| 140 | +It requires the `SHOPIFY_API_KEY` environment variable, which you can get by running `yarn run info --web-env`. |
| 141 | +The CLI will set up the necessary environment variables for the build if you run its `build` command from your app's root: |
| 142 | + |
| 143 | +Using yarn in your app's root folder: |
| 144 | + |
| 145 | +```shell |
| 146 | +yarn build --api-key=REPLACE_ME |
| 147 | +``` |
| 148 | + |
| 149 | +Using npm: |
| 150 | + |
| 151 | +```shell |
| 152 | +npm run build --api-key=REPLACE_ME |
| 153 | +``` |
| 154 | + |
| 155 | +Using pnpm: |
| 156 | + |
| 157 | +```shell |
| 158 | +pnpm run build --api-key=REPLACE_ME |
| 159 | +``` |
| 160 | + |
| 161 | +The app build command will build both the frontend and backend when running as above. |
| 162 | +If you're manually building (for instance when deploying the `web` folder to production), you'll need to build both of them: |
| 163 | + |
| 164 | +```shell |
| 165 | +cd web/frontend |
| 166 | +SHOPIFY_API_KEY=REPLACE_ME yarn build |
| 167 | +cd .. |
| 168 | +rake build:all |
| 169 | +``` |
| 170 | + |
| 171 | +## Hosting |
| 172 | + |
| 173 | +When you're ready to set up your app in production, you can follow [our deployment documentation](https://shopify.dev/apps/deployment/web) to host your app on a cloud provider like [Heroku](https://www.heroku.com/) or [Fly.io](https://fly.io/). |
| 174 | + |
| 175 | +When you reach the step for [setting up environment variables](https://shopify.dev/apps/deployment/web#set-env-vars), you also need to set the following variables: |
| 176 | + |
| 177 | +| Variable | Secret? | Required | Value | Description | |
| 178 | +| -------------------------- | :-----: | :------: | :------------: | ----------------------------------------------------------- | |
| 179 | +| `RAILS_MASTER_KEY` | Yes | Yes | string | Use value from `web/config/master.key` or create a new one. | |
| 180 | +| `RAILS_ENV` | | Yes | `"production"` | | |
| 181 | +| `RAILS_SERVE_STATIC_FILES` | | Yes | `1` | Tells rails to serve the React app from the public folder. | |
| 182 | +| `RAILS_LOG_TO_STDOUT` | | | `1` | Tells rails to print out logs. | |
| 183 | + |
| 184 | +## Known issues |
| 185 | + |
| 186 | +### Hot module replacement and Firefox |
| 187 | + |
| 188 | +When running the app with the CLI in development mode on Firefox, you might see your app constantly reloading when you access it. |
| 189 | +That happened in previous versions of the CLI, because of the way HMR websocket requests work. |
| 190 | + |
| 191 | +We fixed this issue with v3.4.0 of the CLI, so after updating it, you can make the following changes to your app's `web/frontend/vite.config.js` file: |
| 192 | + |
| 193 | +1. Change the definition `hmrConfig` object to be: |
| 194 | + |
| 195 | + ```js |
| 196 | + const host = process.env.HOST |
| 197 | + ? process.env.HOST.replace(/https?:\/\//, "") |
| 198 | + : "localhost"; |
| 199 | + |
| 200 | + let hmrConfig; |
| 201 | + if (host === "localhost") { |
| 202 | + hmrConfig = { |
| 203 | + protocol: "ws", |
| 204 | + host: "localhost", |
| 205 | + port: 64999, |
| 206 | + clientPort: 64999, |
| 207 | + }; |
| 208 | + } else { |
| 209 | + hmrConfig = { |
| 210 | + protocol: "wss", |
| 211 | + host: host, |
| 212 | + port: process.env.FRONTEND_PORT, |
| 213 | + clientPort: 443, |
| 214 | + }; |
| 215 | + } |
| 216 | + ``` |
| 217 | + |
| 218 | +1. Change the `server.host` setting in the configs to `"localhost"`: |
| 219 | + |
| 220 | + ```js |
| 221 | + server: { |
| 222 | + host: "localhost", |
| 223 | + ... |
| 224 | + ``` |
| 225 | +
|
| 226 | +### I can't get past the ngrok "Visit site" page |
| 227 | +
|
| 228 | +When you’re previewing your app or extension, you might see an ngrok interstitial page with a warning: |
| 229 | +
|
| 230 | +``` |
| 231 | +You are about to visit <id>.ngrok.io: Visit Site |
| 232 | +``` |
| 233 | +
|
| 234 | +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. |
| 235 | +We've validated that [Cloudflare Tunnel](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/run-tunnel/trycloudflare/) works with this template. |
| 236 | +
|
| 237 | +To do that, you can [install the `cloudflared` CLI tool](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation/), and run: |
| 238 | +
|
| 239 | +```shell |
| 240 | +# Note that you can also use a different port |
| 241 | +cloudflared tunnel --url http://localhost:3000 |
| 242 | +``` |
| 243 | +
|
| 244 | +In a different terminal window, navigate to your app's root and call: |
| 245 | +
|
| 246 | +```shell |
| 247 | +# Using yarn |
| 248 | +yarn dev --tunnel-url https://tunnel-url:3000 |
| 249 | +# or using npm |
| 250 | +npm run dev --tunnel-url https://tunnel-url:3000 |
| 251 | +# or using pnpm |
| 252 | +pnpm dev --tunnel-url https://tunnel-url:3000 |
| 253 | +``` |
| 254 | +
|
| 255 | +## Developer resources |
| 256 | +
|
| 257 | +- [Introduction to Shopify apps](https://shopify.dev/apps/getting-started) |
| 258 | +- [App authentication](https://shopify.dev/apps/auth) |
| 259 | +- [Shopify CLI](https://shopify.dev/apps/tools/cli) |
| 260 | +- [Shopify API Library documentation](https://github.com/Shopify/shopify-api-ruby/tree/main/docs) |
0 commit comments