Skip to content

Commit afc5e2a

Browse files
committed
docs: add Bun production server guide for TanStack Start
1 parent ed7004b commit afc5e2a

File tree

2 files changed

+370
-10
lines changed

2 files changed

+370
-10
lines changed

docs/start/framework/react/hosting.md

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ npm run start
202202
Make sure that your `react` and `react-dom` packages are set to version 19.0.0 or higher in your `package.json` file. If not, run the following command to upgrade the packages:
203203

204204
```sh
205-
npm install react@rc react-dom@rc
205+
bun install react@rc react-dom@rc
206206
```
207207

208-
Ensure your `vite.config.ts` file is correct.
208+
Ensure your `vite.config.ts` file is correct:
209209

210210
```ts
211211
// vite.config.ts
@@ -218,14 +218,81 @@ export default defineConfig({
218218
})
219219
```
220220

221-
Then you can run the following command to build and start your application:
221+
#### Production Server with Bun
222+
223+
To run TanStack Start applications in production with Bun, you need a custom server implementation. The default build output doesn't work directly with `bun run dist/server/server.js`.
224+
225+
We've created an optimized production server that provides intelligent static asset loading with configurable memory management.
226+
227+
**Features:**
228+
229+
- **Hybrid loading strategy**: Small files (<5MB by default) are preloaded into memory, large files are served on-demand
230+
- **Configurable file filtering**: Use include/exclude patterns to control which files are preloaded
231+
- **Production-ready caching headers**: Automatic optimization for static assets
232+
- **Memory-efficient**: Smart memory management prevents excessive RAM usage
233+
234+
**Quick Setup:**
235+
236+
1. Copy the [`server.ts`](https://github.com/magnusmay/tanstack-start-bun-hosting/blob/main/server.ts) file from the example repository to your project root
237+
238+
2. Build your application:
239+
240+
```sh
241+
bun run build
242+
```
243+
244+
3. Start the optimized server:
245+
246+
```sh
247+
bun run server.ts
248+
```
249+
250+
**Configuration:**
251+
252+
The server can be configured using environment variables:
222253

223254
```sh
224-
bun run build
255+
# Basic usage
256+
bun run server.ts
257+
258+
# Custom port
259+
PORT=8080 bun run server.ts
260+
261+
# Optimize for minimal memory usage (1MB preload limit)
262+
STATIC_PRELOAD_MAX_BYTES=1048576 bun run server.ts
263+
264+
# Preload only critical assets
265+
STATIC_PRELOAD_INCLUDE="*.js,*.css" \
266+
STATIC_PRELOAD_EXCLUDE="*.map,vendor-*" \
267+
bun run server.ts
268+
269+
# Debug mode with verbose logging
270+
STATIC_PRELOAD_VERBOSE=true bun run server.ts
225271
```
226272

227-
You're now ready to deploy your application to a Bun server. You can start your application by running:
273+
**Environment Variables:**
228274

229-
```sh
230-
bun run .output/server/index.mjs
275+
- `PORT`: Server port (default: 3000)
276+
- `STATIC_PRELOAD_MAX_BYTES`: Maximum file size to preload in bytes (default: 5242880 = 5MB)
277+
- `STATIC_PRELOAD_INCLUDE`: Comma-separated glob patterns for files to include
278+
- `STATIC_PRELOAD_EXCLUDE`: Comma-separated glob patterns for files to exclude
279+
- `STATIC_PRELOAD_VERBOSE`: Enable detailed logging (set to "true")
280+
281+
**Example Output:**
282+
283+
```txt
284+
📦 Loading static assets from ./dist/client...
285+
Max preload size: 5.00 MB
286+
287+
📁 Preloaded into memory:
288+
/assets/index-a1b2c3d4.js 45.23 kB │ gzip: 15.83 kB
289+
/assets/index-e5f6g7h8.css 12.45 kB │ gzip: 4.36 kB
290+
291+
💾 Served on-demand:
292+
/assets/vendor-i9j0k1l2.js 245.67 kB │ gzip: 86.98 kB
293+
294+
✅ Preloaded 2 files (57.68 KB) into memory
295+
🚀 Server running at http://localhost:3000
231296
```
297+
298+
For a complete example and more details, check out the [TanStack Start + Bun Hosting repository](https://github.com/magnusmay/tanstack-start-bun-hosting).

0 commit comments

Comments
 (0)