|
| 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 | + |
| 16 | +## Deployment Steps |
| 17 | + |
| 18 | +### Option 1: Using Vercel CLI |
| 19 | + |
| 20 | +1. Install Vercel CLI: |
| 21 | + ```bash |
| 22 | + npm i -g vercel |
| 23 | + ``` |
| 24 | + |
| 25 | +2. Navigate to the app-host directory: |
| 26 | + ```bash |
| 27 | + cd examples/app-host |
| 28 | + ``` |
| 29 | + |
| 30 | +3. Deploy to Vercel: |
| 31 | + ```bash |
| 32 | + vercel |
| 33 | + ``` |
| 34 | + |
| 35 | +4. Set environment variables: |
| 36 | + ```bash |
| 37 | + vercel env add AUTH_SECRET |
| 38 | + ``` |
| 39 | + |
| 40 | +### Option 2: Using Vercel Dashboard |
| 41 | + |
| 42 | +1. Import the repository to Vercel |
| 43 | +2. Set the root directory to `examples/app-host` |
| 44 | +3. Add environment variables in the project settings |
| 45 | +4. Deploy |
| 46 | + |
| 47 | +## Build Configuration |
| 48 | + |
| 49 | +The build is configured in `vercel.json`: |
| 50 | + |
| 51 | +- **Install Command**: `cd ../.. && pnpm install` (installs monorepo dependencies) |
| 52 | +- **Build Command**: `bash scripts/build-vercel.sh` (builds and bundles the application) |
| 53 | +- **Framework**: `null` (uses custom build setup) |
| 54 | + |
| 55 | +## How It Works |
| 56 | + |
| 57 | +1. **Build Process** (`scripts/build-vercel.sh`): |
| 58 | + - Builds the TypeScript project using Turbo |
| 59 | + - Bundles the server code using esbuild (`scripts/bundle-api.mjs`) |
| 60 | + - Copies native dependencies (like better-sqlite3) to local node_modules |
| 61 | + |
| 62 | +2. **API Handler** (`api/[[...route]].js`): |
| 63 | + - Committed catch-all route handler that Vercel detects pre-build |
| 64 | + - Delegates to the bundled handler (`api/_handler.js`) |
| 65 | + |
| 66 | +3. **Server Entrypoint** (`server/index.ts`): |
| 67 | + - Boots ObjectStack kernel with Hono adapter |
| 68 | + - Uses `@hono/node-server`'s `getRequestListener()` for Vercel compatibility |
| 69 | + - Handles Vercel's pre-buffered request body properly |
| 70 | + |
| 71 | +4. **Hono Integration**: |
| 72 | + - Uses `@objectstack/hono` adapter to create the HTTP application |
| 73 | + - Provides REST API at `/api/v1` prefix |
| 74 | + - Includes authentication via AuthPlugin |
| 75 | + |
| 76 | +## Architecture |
| 77 | + |
| 78 | +The deployment follows Vercel's serverless function pattern: |
| 79 | + |
| 80 | +``` |
| 81 | +examples/app-host/ |
| 82 | +├── api/ |
| 83 | +│ ├── [[...route]].js # Committed entry point |
| 84 | +│ └── _handler.js # Generated bundle (not committed) |
| 85 | +├── server/ |
| 86 | +│ └── index.ts # Server implementation |
| 87 | +├── scripts/ |
| 88 | +│ ├── build-vercel.sh # Build script |
| 89 | +│ └── bundle-api.mjs # Bundler configuration |
| 90 | +├── .npmrc # pnpm configuration (node-linker=hoisted) |
| 91 | +└── vercel.json # Vercel configuration |
| 92 | +``` |
| 93 | + |
| 94 | +## Testing Locally |
| 95 | + |
| 96 | +Before deploying, you can test locally: |
| 97 | + |
| 98 | +```bash |
| 99 | +# Build the application |
| 100 | +pnpm build |
| 101 | + |
| 102 | +# Run in development mode |
| 103 | +pnpm dev |
| 104 | + |
| 105 | +# Test the API |
| 106 | +curl http://localhost:3000/api/v1/discovery |
| 107 | +``` |
| 108 | + |
| 109 | +## Accessing the API |
| 110 | + |
| 111 | +After deployment, your API will be available at: |
| 112 | + |
| 113 | +- Discovery: `https://your-app.vercel.app/api/v1/discovery` |
| 114 | +- Data API: `https://your-app.vercel.app/api/v1/data/:object` |
| 115 | +- Meta API: `https://your-app.vercel.app/api/v1/meta/:type` |
| 116 | + |
| 117 | +## Troubleshooting |
| 118 | + |
| 119 | +### Build Fails |
| 120 | + |
| 121 | +- Ensure all dependencies are installed: `pnpm install` |
| 122 | +- Check build logs in Vercel dashboard |
| 123 | +- Verify `build-vercel.sh` is executable: `chmod +x scripts/build-vercel.sh` |
| 124 | + |
| 125 | +### Runtime Errors |
| 126 | + |
| 127 | +- Check function logs in Vercel dashboard |
| 128 | +- Verify environment variables are set correctly |
| 129 | +- Ensure `AUTH_SECRET` is at least 32 characters |
| 130 | + |
| 131 | +### Module Not Found |
| 132 | + |
| 133 | +- The build script copies native modules to local node_modules |
| 134 | +- Check that `better-sqlite3` is in dependencies |
| 135 | +- Verify `vercel.json` includeFiles pattern matches the module |
| 136 | + |
| 137 | +## References |
| 138 | + |
| 139 | +- [Vercel Hono Documentation](https://vercel.com/docs/frameworks/backend/hono) |
| 140 | +- [ObjectStack Documentation](../../README.md) |
| 141 | +- [Hono Documentation](https://hono.dev/) |
0 commit comments