|
| 1 | +# Deploying App Host to Vercel |
| 2 | + |
| 3 | +This example demonstrates how to deploy the ObjectStack app-host to Vercel using Hono. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. A Vercel account |
| 8 | +2. Vercel CLI installed (optional): `npm i -g vercel` |
| 9 | + |
| 10 | +## Environment Variables |
| 11 | + |
| 12 | +Set the following environment variables in your Vercel project: |
| 13 | + |
| 14 | +- `AUTH_SECRET`: A secure random string (minimum 32 characters) for authentication |
| 15 | +- `TURSO_DATABASE_URL`: Your Turso database URL (e.g., `libsql://your-database.turso.io`) |
| 16 | +- `TURSO_AUTH_TOKEN`: Your Turso authentication token |
| 17 | + |
| 18 | +You can get these credentials from [Turso Dashboard](https://turso.tech/). |
| 19 | + |
| 20 | +## Deployment Steps |
| 21 | + |
| 22 | +### Option 1: Using Vercel CLI |
| 23 | + |
| 24 | +1. Install Vercel CLI: |
| 25 | + ```bash |
| 26 | + npm i -g vercel |
| 27 | + ``` |
| 28 | + |
| 29 | +2. Navigate to the app-host directory: |
| 30 | + ```bash |
| 31 | + cd examples/app-host |
| 32 | + ``` |
| 33 | + |
| 34 | +3. Deploy to Vercel: |
| 35 | + ```bash |
| 36 | + vercel |
| 37 | + ``` |
| 38 | + |
| 39 | +4. Set environment variables: |
| 40 | + ```bash |
| 41 | + vercel env add AUTH_SECRET |
| 42 | + vercel env add TURSO_DATABASE_URL |
| 43 | + vercel env add TURSO_AUTH_TOKEN |
| 44 | + ``` |
| 45 | + |
| 46 | +### Option 2: Using Vercel Dashboard |
| 47 | + |
| 48 | +1. Import the repository to Vercel |
| 49 | +2. Set the root directory to `examples/app-host` |
| 50 | +3. Add environment variables in the project settings |
| 51 | +4. Deploy |
| 52 | + |
| 53 | +## Build Configuration |
| 54 | + |
| 55 | +The build is configured in `vercel.json`: |
| 56 | + |
| 57 | +- **Install Command**: `cd ../.. && pnpm install` (installs monorepo dependencies) |
| 58 | +- **Build Command**: `bash scripts/build-vercel.sh` (builds and bundles the application) |
| 59 | +- **Framework**: `hono` (uses Vercel's Hono framework preset) |
| 60 | + |
| 61 | +## How It Works |
| 62 | + |
| 63 | +1. **Build Process** (`scripts/build-vercel.sh`): |
| 64 | + - Builds the TypeScript project using Turbo |
| 65 | + - Bundles the server code using esbuild (`scripts/bundle-api.mjs`) |
| 66 | + |
| 67 | +2. **API Handler** (`api/[[...route]].js`): |
| 68 | + - Committed catch-all route handler that Vercel detects pre-build |
| 69 | + - Delegates to the bundled handler (`api/_handler.js`) |
| 70 | + |
| 71 | +3. **Server Entrypoint** (`server/index.ts`): |
| 72 | + - Boots ObjectStack kernel with Hono adapter |
| 73 | + - Uses `@hono/node-server`'s `getRequestListener()` for Vercel compatibility |
| 74 | + - Connects to Turso database in remote mode (HTTP-only, no local SQLite) |
| 75 | + - Handles Vercel's pre-buffered request body properly |
| 76 | + |
| 77 | +4. **Hono Integration**: |
| 78 | + - Uses `@objectstack/hono` adapter to create the HTTP application |
| 79 | + - Provides REST API at `/api/v1` prefix |
| 80 | + - Includes authentication via AuthPlugin |
| 81 | + |
| 82 | +## Architecture |
| 83 | + |
| 84 | +The deployment follows Vercel's serverless function pattern: |
| 85 | + |
| 86 | +``` |
| 87 | +examples/app-host/ |
| 88 | +├── api/ |
| 89 | +│ ├── [[...route]].js # Committed entry point |
| 90 | +│ └── _handler.js # Generated bundle (not committed) |
| 91 | +├── server/ |
| 92 | +│ └── index.ts # Server implementation |
| 93 | +├── scripts/ |
| 94 | +│ ├── build-vercel.sh # Build script |
| 95 | +│ └── bundle-api.mjs # Bundler configuration |
| 96 | +├── .npmrc # pnpm configuration (node-linker=hoisted) |
| 97 | +└── vercel.json # Vercel configuration |
| 98 | +``` |
| 99 | + |
| 100 | +## Testing Locally |
| 101 | + |
| 102 | +Before deploying, you can test locally: |
| 103 | + |
| 104 | +```bash |
| 105 | +# Build the application |
| 106 | +pnpm build |
| 107 | + |
| 108 | +# Run in development mode |
| 109 | +pnpm dev |
| 110 | + |
| 111 | +# Test the API |
| 112 | +curl http://localhost:3000/api/v1/discovery |
| 113 | +``` |
| 114 | + |
| 115 | +## Accessing the API |
| 116 | + |
| 117 | +After deployment, your API will be available at: |
| 118 | + |
| 119 | +- Discovery: `https://your-app.vercel.app/api/v1/discovery` |
| 120 | +- Data API: `https://your-app.vercel.app/api/v1/data/:object` |
| 121 | +- Meta API: `https://your-app.vercel.app/api/v1/meta/:type` |
| 122 | + |
| 123 | +## Troubleshooting |
| 124 | + |
| 125 | +### Build Fails |
| 126 | + |
| 127 | +- Ensure all dependencies are installed: `pnpm install` |
| 128 | +- Check build logs in Vercel dashboard |
| 129 | +- Verify `build-vercel.sh` is executable: `chmod +x scripts/build-vercel.sh` |
| 130 | + |
| 131 | +### Runtime Errors |
| 132 | + |
| 133 | +- Check function logs in Vercel dashboard |
| 134 | +- Verify environment variables are set correctly (`TURSO_DATABASE_URL`, `TURSO_AUTH_TOKEN`, `AUTH_SECRET`) |
| 135 | +- Ensure `AUTH_SECRET` is at least 32 characters |
| 136 | +- Test Turso connection using the provided credentials |
| 137 | + |
| 138 | +### Database Connection Issues |
| 139 | + |
| 140 | +- Verify your Turso database URL and auth token are correct |
| 141 | +- Check that your Turso database is accessible (not paused) |
| 142 | +- The deployment uses TursoDriver in **remote mode** (HTTP-only), which doesn't require native modules like better-sqlite3 |
| 143 | + |
| 144 | +## References |
| 145 | + |
| 146 | +- [Vercel Hono Documentation](https://vercel.com/docs/frameworks/backend/hono) |
| 147 | +- [ObjectStack Documentation](../../README.md) |
| 148 | +- [Hono Documentation](https://hono.dev/) |
0 commit comments