Skip to content

coopermaruyama/universal-turbo-stack

 
 

Repository files navigation

universal-turbo-stack

A slightly opinionated starter for cross-platform React that runs anywhere, with a focus on developer experience, code sharing, and production readiness.

This starter is built on top of Turborepo and includes:

  • Next.js for web development.
  • Expo for mobile development.
  • tRPC for typesafe API calls.
  • Drizzle for typesafe database calls.
  • Better-Auth for authentication.
  • NativeWind for Tailwind CSS in React Native.
  • React Native Web for web support in the Expo app.
  • React Native Reusables in a ready-to-use ui package for cross-platform UI components.
  • Storybook for both Expo and Next.js apps.
  • docker-compose pre-configured with Postgres and Redis.
  • fly.io for deploying the Next.js app with Postgres and Redis.
  • Pre-configured CI with GitHub Actions which will create preview deployments for every PR.

Installation

Note

Make sure to follow the system requirements specified in package.json#engines before proceeding.

There are two ways of initializing an app using the universal-turbo-stack starter. You can either use this repository as a template:

use-as-template

or use Turbo's CLI to init your project (use PNPM as package manager):

npx create-turbo@latest -e https://github.com/coopermaruyama/universal-turbo-stack

About

This repo uses Turborepo and contains:

.github
  └─ workflows
        └─ CI with pnpm cache setup
.vscode
  └─ Recommended extensions and settings for VSCode users
apps
  ├─ expo
  │   ├─ Expo SDK 53
  │   ├─ React Native using React 19
  │   ├─ Navigation using Expo Router
  │   ├─ Tailwind using NativeWind
  │   └─ Typesafe API calls using tRPC
  └─ next.js
      ├─ Next.js 15
      ├─ React 19
      ├─ Tailwind CSS
      └─ E2E Typesafe API Server & Client
packages
  ├─ api
  │   └─ tRPC v11 router definition
  ├─ auth
  │   └─ Authentication using better-auth.
  ├─ db
  |   └─ Typesafe db calls using Drizzle & Postgres
  └─ ui
      └─ Cross-platform UI components using React Native Reusables
tooling
  ├─ tailwind
  │   └─ shared tailwind configuration
  └─ typescript
      └─ shared tsconfig you can extend from

In this template, we use @acme as a placeholder for package names. As a user, you might want to replace it with your own organization or project name. You can use find-and-replace to change all the instances of @acme to something like @my-company or @project-name.

Quick Start

Note The db package is preconfigured to use serverless postgres and is edge-bound with the Neon Serverless driver in production, and regular postgres in dev. If you're using something else, make the necessary modifications to the schema as well as the client and the drizzle config. If you want to switch to non-edge database driver, remove export const runtime = "edge"; from all pages and api routes

To get it running, follow the steps below:

1. Setup dependencies

# Install dependencies
pnpm i

# Configure environment variables
# Environment variables are checked in but encrypted for security. The encryption
# keys are store in .env.keys and should not be checked in.
echo << EOF > .env.keys
#/------------------!DOTENV_PRIVATE_KEYS!-------------------/
#/ private decryption keys. DO NOT commit to source control /
#/     [how it works](https://dotenvx.com/encryption)       /
#/----------------------------------------------------------/
# .env.development
DOTENV_PRIVATE_KEY_DEVELOPMENT=32481a02ef873fd2386669251ef0d7d5083f76465f92e013cc9948c403fcc20b

EOF

# Push the Drizzle schema to the database
pnpm db:push

2. Generate Better Auth Schema

This project uses Better Auth for authentication. The auth schema needs to be generated using the Better Auth CLI before you can use the authentication features.

# Generate the Better Auth schema
pnpm --filter @acme/auth generate

This command runs the Better Auth CLI with the following configuration:

  • Config file: packages/auth/script/auth-cli.ts - A CLI-only configuration file (isolated from src to prevent imports)
  • Output: packages/db/src/auth-schema.ts - Generated Drizzle schema for authentication tables

The generation process:

  1. Reads the Better Auth configuration from packages/auth/script/auth-cli.ts
  2. Generates the appropriate database schema based on your auth setup
  3. Outputs a Drizzle-compatible schema file to the @acme/db package

Note: The auth-cli.ts file is placed in the script/ directory (instead of src/) to prevent accidental imports from other parts of the codebase. This file is exclusively for CLI schema generation and should not be used directly in your application. For runtime authentication, use the configuration from packages/auth/src/index.ts.

For more information about the Better Auth CLI, see the official documentation.

3. Configure Expo dev-script

Use iOS Simulator

  1. Make sure you have XCode and XCommand Line Tools installed as shown on expo docs.

    NOTE: If you just installed XCode, or if you have updated it, you need to open the simulator manually once. Run npx expo start from apps/expo, and then enter I to launch Expo Go. After the manual launch, you can run pnpm dev in the root directory.

    +  "dev": "expo start --ios",

Use Android Emulator

  1. Install Android Studio tools as shown on expo docs.

  2. Change the dev script at apps/expo/package.json to open the Android emulator.

    +  "dev": "expo start --android",
  3. Run pnpm dev at the project root folder.

3. Run the apps

Run pnpm dev at the project root folder. This will start Next.js and Expo in development mode.

Storybook

Storybook is configured to run on both Expo and Next.js.

For Expo, storybook is already configured to run on the iOS simulator. Use the dev menu to switch to the Storybook UI.

For Next.js, you can run Storybook by running the following command:

cd apps/nextjs

pnpm storybook

3. Configuring Better-Auth to work with Expo

In order to get Better-Auth to work with Expo, you must either:

Deploy the Auth Proxy (RECOMMENDED)

Better-auth comes with an auth proxy plugin. By deploying the Next.js app, you can get OAuth working in preview deployments and development for Expo apps.

By using the proxy plugin, the Next.js apps will forward any auth requests to the proxy server, which will handle the OAuth flow and then redirect back to the Next.js app. This makes it easy to get OAuth working since you'll have a stable URL that is publicly accessible and doesn't change for every deployment and doesn't rely on what port the app is running on. So if port 3000 is taken and your Next.js app starts at port 3001 instead, your auth should still work without having to reconfigure the OAuth provider.

Add your local IP to your OAuth provider

You can alternatively add your local IP (e.g. 192.168.x.y:$PORT) to your OAuth provider. This may not be as reliable as your local IP may change when you change networks. Some OAuth providers may also only support a single callback URL for each app making this approach unviable for some providers (e.g. GitHub).

4. When it's time to add a new package

To add a new package, simply run pnpm turbo gen init in the monorepo root. This will prompt you for a package name as well as if you want to install any dependencies to the new package (of course you can also do this yourself later).

The generator sets up the package.json, tsconfig.json and a index.ts, as well as configures all the necessary configurations for tooling around your package such as formatting, linting and typechecking. When the package is created, you're ready to go build out the package.

FAQ

Does the starter include Solito?

No. Solito will not be included in this repo. It is a great tool if you want to share code between your Next.js and Expo app. However, the main purpose of this repo is not the integration between Next.js and Expo — it's the code splitting of your T3 App into a monorepo. The Expo app is just a bonus example of how you can utilize the monorepo with multiple apps but can just as well be any app such as Vite, Electron, etc.

Integrating Solito into this repo isn't hard, and there are a few official templates by the creators of Solito that you can use as a reference.

Does this pattern leak backend code to my client applications?

No, it does not. The api package should only be a production dependency in the Next.js application where it's served. The Expo app, and all other apps you may add in the future, should only add the api package as a dev dependency. This lets you have full typesafety in your client applications, while keeping your backend code safe.

If you need to share runtime code between the client and server, such as input validation schemas, you can create a separate shared package for this and import it on both sides.

Deployment

Next.js

Prerequisites

Note Please note that the Next.js application with tRPC must be deployed in order for the Expo app to communicate with the server in a production environment.

Deploy to Vercel

Let's deploy the Next.js application to Vercel. If you've never deployed a Turborepo app there, don't worry, the steps are quite straightforward. You can also read the official Turborepo guide on deploying to Vercel.

  1. Create a new project on Vercel, select the apps/nextjs folder as the root directory. Vercel's zero-config system should handle all configurations for you.

  2. Add your POSTGRES_URL environment variable.

  3. Done! Your app should successfully deploy. Assign your domain and use that instead of localhost for the url in the Expo app so that your Expo app can communicate with your backend when you are not in development.

Auth Proxy

The auth proxy comes as a better-auth plugin. This is required for the Next.js app to be able to authenticate users in preview deployments. The auth proxy is not used for OAuth request in production deployments. The easiest way to get it running is to deploy the Next.js app to vercel.

Expo

Deploying your Expo application works slightly differently compared to Next.js on the web. Instead of "deploying" your app online, you need to submit production builds of your app to app stores, like Apple App Store and Google Play. You can read the full guide to distributing your app, including best practices, in the Expo docs.

  1. Make sure to modify the getBaseUrl function to point to your backend's production URL:

    https://github.com/t3-oss/create-t3-turbo/blob/656965aff7db271e5e080242c4a3ce4dad5d25f8/apps/expo/src/utils/api.tsx#L20-L37

  2. Let's start by setting up EAS Build, which is short for Expo Application Services. The build service helps you create builds of your app, without requiring a full native development setup. The commands below are a summary of Creating your first build.

    # Install the EAS CLI
    pnpm add -g eas-cli
    
    # Log in with your Expo account
    eas login
    
    # Configure your Expo app
    cd apps/expo
    eas build:configure
  3. After the initial setup, you can create your first build. You can build for Android and iOS platforms and use different eas.json build profiles to create production builds or development, or test builds. Let's make a production build for iOS.

    eas build --platform ios --profile production

    If you don't specify the --profile flag, EAS uses the production profile by default.

  4. Now that you have your first production build, you can submit this to the stores. EAS Submit can help you send the build to the stores.

    eas submit --platform ios --latest

    You can also combine build and submit in a single command, using eas build ... --auto-submit.

  5. Before you can get your app in the hands of your users, you'll have to provide additional information to the app stores. This includes screenshots, app information, privacy policies, etc. While still in preview, EAS Metadata can help you with most of this information.

  6. Once everything is approved, your users can finally enjoy your app. Let's say you spotted a small typo; you'll have to create a new build, submit it to the stores, and wait for approval before you can resolve this issue. In these cases, you can use EAS Update to quickly send a small bugfix to your users without going through this long process. Let's start by setting up EAS Update.

    The steps below summarize the Getting started with EAS Update guide.

    # Add the `expo-updates` library to your Expo app
    cd apps/expo
    pnpm expo install expo-updates
    
    # Configure EAS Update
    eas update:configure
  7. Before we can send out updates to your app, you have to create a new build and submit it to the app stores. For every change that includes native APIs, you have to rebuild the app and submit the update to the app stores. See steps 2 and 3.

  8. Now that everything is ready for updates, let's create a new update for production builds. With the --auto flag, EAS Update uses your current git branch name and commit message for this update. See How EAS Update works for more information.

    cd apps/expo
    eas update --auto

    Your OTA (Over The Air) updates must always follow the app store's rules. You can't change your app's primary functionality without getting app store approval. But this is a fast way to update your app for minor changes and bug fixes.

  9. Done! Now that you have created your production build, submitted it to the stores, and installed EAS Update, you are ready for anything!

References

The stack originates from create-t3-app.

A blog post where I wrote how to migrate a T3 app into this.

About

T3 Stack with Universal React Support using Nativewind, NextJS, and Expo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 96.2%
  • JavaScript 1.3%
  • Dockerfile 0.6%
  • CSS 0.6%
  • Shell 0.6%
  • Makefile 0.5%
  • Other 0.2%