|
| 1 | +# TanStack Start + Bun Production Server |
| 2 | + |
| 3 | +An optimized production server for TanStack Start applications using Bun, implementing intelligent static asset loading with configurable memory management. |
| 4 | + |
| 5 | +## 🚀 Features |
| 6 | + |
| 7 | +- **Hybrid Loading Strategy**: Small files are preloaded into memory, large files are served on-demand |
| 8 | +- **Configurable File Filtering**: Include/Exclude patterns for precise control |
| 9 | +- **Memory-efficient Response Generation**: Optimized for high performance |
| 10 | +- **Production-ready Caching Headers**: Automatic Cache-Control headers for optimal performance |
| 11 | +- **Detailed Logging**: Vite-like output for better overview |
| 12 | + |
| 13 | +## 📦 Installation |
| 14 | + |
| 15 | +This project was created with TanStack Start: |
| 16 | + |
| 17 | +```bash |
| 18 | +bunx create-start-app@latest |
| 19 | +``` |
| 20 | + |
| 21 | +Install dependencies: |
| 22 | + |
| 23 | +```bash |
| 24 | +bun install |
| 25 | +``` |
| 26 | + |
| 27 | +## 🏃♂️ Development |
| 28 | + |
| 29 | +For development: |
| 30 | + |
| 31 | +```bash |
| 32 | +bun run dev |
| 33 | +``` |
| 34 | + |
| 35 | +## 🔨 Production Build |
| 36 | + |
| 37 | +Build the application for production: |
| 38 | + |
| 39 | +```bash |
| 40 | +bun run build |
| 41 | +``` |
| 42 | + |
| 43 | +## 🚀 Production Server with server.ts |
| 44 | + |
| 45 | +### Quick Start - Use in Your Project |
| 46 | + |
| 47 | +You can easily use this production server in your own TanStack Start project: |
| 48 | + |
| 49 | +1. **Copy the `server.ts` file** into your project root |
| 50 | +2. **Build your project** with `bun run build` |
| 51 | +3. **Start the server** directly with: |
| 52 | + ```bash |
| 53 | + bun run server.ts |
| 54 | + ``` |
| 55 | + |
| 56 | +Or add it to your `package.json` scripts: |
| 57 | + |
| 58 | +```json |
| 59 | +{ |
| 60 | + "scripts": { |
| 61 | + "start": "bun run server.ts" |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +Then run with: |
| 67 | + |
| 68 | +```bash |
| 69 | +bun run start |
| 70 | +``` |
| 71 | + |
| 72 | +### Server Features |
| 73 | + |
| 74 | +The `server.ts` implements a high-performance production server with the following features: |
| 75 | + |
| 76 | +#### 1. Intelligent Asset Loading |
| 77 | + |
| 78 | +The server automatically decides which files to preload into memory and which to serve on-demand: |
| 79 | + |
| 80 | +- **In-Memory Loading**: Small files (default < 5MB) are loaded into memory at startup |
| 81 | +- **On-Demand Loading**: Large files are loaded from disk only when requested |
| 82 | +- **Optimized Performance**: Frequently used assets are served from memory |
| 83 | + |
| 84 | +#### 2. Configuration via Environment Variables |
| 85 | + |
| 86 | +```bash |
| 87 | +# Server Port (default: 3000) |
| 88 | +PORT=3000 |
| 89 | + |
| 90 | +# Maximum file size for in-memory loading (in bytes, default: 5MB) |
| 91 | +STATIC_PRELOAD_MAX_BYTES=5242880 |
| 92 | + |
| 93 | +# Include patterns (comma-separated, only these files will be preloaded) |
| 94 | +STATIC_PRELOAD_INCLUDE="*.js,*.css,*.woff2" |
| 95 | + |
| 96 | +# Exclude patterns (comma-separated, these files will be excluded) |
| 97 | +STATIC_PRELOAD_EXCLUDE="*.map,*.txt" |
| 98 | + |
| 99 | +# Enable detailed logging |
| 100 | +STATIC_PRELOAD_VERBOSE=true |
| 101 | +``` |
| 102 | + |
| 103 | +### Example Configurations |
| 104 | + |
| 105 | +#### Minimal Memory Footprint |
| 106 | + |
| 107 | +```bash |
| 108 | +# Preload only critical assets |
| 109 | +STATIC_PRELOAD_MAX_BYTES=1048576 \ |
| 110 | +STATIC_PRELOAD_INCLUDE="*.js,*.css" \ |
| 111 | +STATIC_PRELOAD_EXCLUDE="*.map,vendor-*" \ |
| 112 | +bun run start |
| 113 | +``` |
| 114 | + |
| 115 | +#### Maximum Performance |
| 116 | + |
| 117 | +```bash |
| 118 | +# Preload all small assets |
| 119 | +STATIC_PRELOAD_MAX_BYTES=10485760 \ |
| 120 | +bun run start |
| 121 | +``` |
| 122 | + |
| 123 | +#### Debug Mode |
| 124 | + |
| 125 | +```bash |
| 126 | +# With detailed logging |
| 127 | +STATIC_PRELOAD_VERBOSE=true \ |
| 128 | +bun run start |
| 129 | +``` |
| 130 | + |
| 131 | +### Server Output |
| 132 | + |
| 133 | +The server displays a clear overview of all loaded assets at startup: |
| 134 | + |
| 135 | +```txt |
| 136 | +📦 Loading static assets from ./dist/client... |
| 137 | + Max preload size: 5.00 MB |
| 138 | + Include patterns: *.js,*.css,*.woff2 |
| 139 | +
|
| 140 | +📁 Preloaded into memory: |
| 141 | + /assets/index-a1b2c3d4.js 45.23 kB │ gzip: 15.83 kB |
| 142 | + /assets/index-e5f6g7h8.css 12.45 kB │ gzip: 4.36 kB |
| 143 | +
|
| 144 | +💾 Served on-demand: |
| 145 | + /assets/vendor-i9j0k1l2.js 245.67 kB │ gzip: 86.98 kB |
| 146 | +
|
| 147 | +✅ Preloaded 2 files (57.68 KB) into memory |
| 148 | +ℹ️ 1 files will be served on-demand (1 too large, 0 filtered) |
| 149 | +
|
| 150 | +🚀 Server running at http://localhost:3000 |
| 151 | +``` |
| 152 | + |
| 153 | +## Testing |
| 154 | + |
| 155 | +This project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with: |
| 156 | + |
| 157 | +```bash |
| 158 | +bun run test |
| 159 | +``` |
| 160 | + |
| 161 | +## Styling |
| 162 | + |
| 163 | +This project uses [Tailwind CSS](https://tailwindcss.com/) for styling. |
| 164 | + |
| 165 | +## Linting & Formatting |
| 166 | + |
| 167 | +This project uses [eslint](https://eslint.org/) and [prettier](https://prettier.io/) for linting and formatting. Eslint is configured using [tanstack/eslint-config](https://tanstack.com/config/latest/docs/eslint). The following scripts are available: |
| 168 | + |
| 169 | +```bash |
| 170 | +bun run lint |
| 171 | +bun run format |
| 172 | +bun run check |
| 173 | +``` |
0 commit comments