Shopify App Template - Remix

repository·main·Indexed 20 days ago

https://github.com/shopify/shopify-app-template-remix

A production-ready Remix template for building Shopify apps. It includes built-in support for OAuth, GraphQL Admin API integration, Webhooks, App Bridge, and the Polaris design system. The template uses Prisma for session storage (defaulting to SQLite) and provides configurations for hosting on Vercel and integrating with databases like MongoDB, MySQL, and PostgreSQL.

Tokens
3K
Snippets
13
Records
18
Agent score
19%

What's inside shopify-app-template-remix

  1. Configure application storage with Prisma

    main

    This template uses Prisma to manage session data. By default, it is configured to use an SQLite database via the schema defined in prisma/schema.prisma.

    While SQLite is suitable for single-instance production apps, you can switch to other databases (MySQL, PostgreSQL, MongoDB, or Redis) by:

    1. Updating the datasource provider in prisma/schema.prisma.
    2. Using a different SessionStorage adapter package from @shopify/shopify-api.

    Commonly used database providers include:

    • MySQL: Digital Ocean, Planet Scale, Amazon Aurora, Google Cloud SQL
    • PostgreSQL: Digital Ocean, Amazon Aurora, Google Cloud SQL
    • Redis: Digital Ocean, Amazon MemoryDB
    • MongoDB: Digital Ocean, MongoDB Atlas
  2. Recommended approach for Webhook subscriptions

    main

    While you can register shop-specific webhooks in the afterAuth hook using shopify.registerWebhooks, this approach can be unreliable because afterAuth only runs during initial installation or when an access token expires. During normal development, subscriptions may not update.

    Recommended: Declare app-specific webhooks in your shopify.app.toml file. Shopify will automatically update these subscriptions every time you run npm run deploy.

  3. Configure a non-embedded Shopify app

    main

    By default, this template is configured for embedded apps. If you need to build a non-embedded app, apply these four changes:

    1. In shopify.app.toml, set embedded = false.
    2. In ./app/shopify.server.js|ts, pass isEmbeddedApp: false to the shopifyApp() function.
    3. In /app/routes/app.jsx|tsx, set the isEmbeddedApp prop to false on the AppProvider component.
    4. Remove the @shopify/app-bridge-react dependency from package.json and vite.config.ts|js, and remove all imports from that package (e.g., NavMenu, TitleBar, useAppBridge).
  4. Host the app on Vercel

    main

    When hosting on Vercel, it is recommended to use the Vercel Preset. You must also ensure that imports intended for @remix-run/node are replaced with imports from @vercel/remix.

    Update your vite.config.ts to include the vercelPreset in the Remix plugin configuration:

    import { vitePlugin as remix } from "@remix-run/dev";
    import { defineConfig, type UserConfig } from "vite";
    import tsconfigPaths from "vite-tsconfig-paths";
    import { vercelPreset } from '@vercel/remix/vite';
    
    export default defineConfig({
      plugins: [
        remix({
          ignoredRouteFiles: ["**/.*"],
          presets: [vercelPreset()],
        }),
        tsconfigPaths(),
      ],
    });
  5. Configure MongoDB with Prisma in this template

    main

    This template uses SQLite by default. To use MongoDB, you must modify your schema and Prisma configuration.

    1. Mapping the ID field

    Prisma expects the ID field to be the ID of the session, not the MongoDB _id field. Add a field to your Session model that maps the _id attribute to an id field:

    model Session {
      session_id  String    @id @default(auto()) @map("_id") @db.ObjectId
      id          String    @unique
      ...
    }

    2. Handling migrations

    MongoDB does not support prisma migrate. Instead, use prisma db push. Update your shopify.web.toml file to use the following commands:

    [commands]
    predev = "npx prisma generate && npx prisma db push"
    dev = "npm exec remix vite:dev"

    3. Replica Set requirement

    Prisma requires your MongoDB server to be running as a replica set to perform transactions.

  6. Avoid breaking embedded apps with incorrect navigation

    main

    Embedded Shopify apps run inside an iFrame and must maintain user sessions. To prevent navigation or redirection from breaking the app session, follow these rules:

    1. Links: Use Link from @remix-run/react or @shopify/polaris. Do not use standard <a> tags.
    2. Redirects: Use the redirect helper returned from authenticate.admin. Do not use the standard redirect from @remix-run/node.
    3. Forms: Use useSubmit or the <Form/> component from @remix-run/react. Do not use lowercase <form/> tags.
    // Correct way to redirect in an embedded app
    const { redirect } = await authenticate.admin(request);
    return redirect('/path');
  7. Fix HMAC validation failures for Admin-created webhooks

    main

    Webhooks created manually via the Shopify Admin will fail HMAC validation because the payload is not signed with your app's secret key. To fix this, use one of these two methods:

    1. App-specific webhooks (Recommended): Define them in your shopify.app.toml file.
    2. Programmatic subscriptions: Create subscriptions using the shopifyApp object.