Open History to see the last commit
1# TanStack Start + Bun Production Server
2β
3An 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β
15This project was created with TanStack Start:
16β
17```bash
18bunx create-start-app@latest
19```
20β
21Install dependencies:
22β
23```bash
24bun install
25```
26β
27## πββοΈ Development
28β
29For development:
30β
31```bash
32bun run dev
33```
34β
35## π¨ Production Build
36β
37Build the application for production:
38β
39```bash
40bun run build
41```
42β
43## π Production Server with server.ts
44β
45### Quick Start - Use in Your Project
46β
47You can easily use this production server in your own TanStack Start project:
48β
491. **Copy the `server.ts` file** into your project root
502. **Build your project** with `bun run build`
513. **Start the server** directly with:
52 ```bash
53 bun run server.ts
54 ```
55β
56Or add it to your `package.json` scripts:
57β
58```json
59{
60 "scripts": {
61 "start": "bun run server.ts"
62 }
63}
64```
65β
66Then run with:
67β
68```bash
69bun run start
70```
71β
72### Server Features
73β
74The `server.ts` implements a high-performance production server with the following features:
75β
76#### 1. Intelligent Asset Loading
77β
78The 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)
88PORT=3000
89β
90# Maximum file size for in-memory loading (in bytes, default: 5MB)
91STATIC_PRELOAD_MAX_BYTES=5242880
92β
93# Include patterns (comma-separated, only these files will be preloaded)
94STATIC_PRELOAD_INCLUDE="*.js,*.css,*.woff2"
95β
96# Exclude patterns (comma-separated, these files will be excluded)
97STATIC_PRELOAD_EXCLUDE="*.map,*.txt"
98β
99# Enable detailed logging
100STATIC_PRELOAD_VERBOSE=true
101```
102β
103### Example Configurations
104β
105#### Minimal Memory Footprint
106β
107```bash
108# Preload only critical assets
109STATIC_PRELOAD_MAX_BYTES=1048576 \
110STATIC_PRELOAD_INCLUDE="*.js,*.css" \
111STATIC_PRELOAD_EXCLUDE="*.map,vendor-*" \
112bun run start
113```
114β
115#### Maximum Performance
116β
117```bash
118# Preload all small assets
119STATIC_PRELOAD_MAX_BYTES=10485760 \
120bun run start
121```
122β
123#### Debug Mode
124β
125```bash
126# With detailed logging
127STATIC_PRELOAD_VERBOSE=true \
128bun run start
129```
130β
131### Server Output
132β
133The 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β
155This project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:
156β
157```bash
158bun run test
159```
160β
161## Styling
162β
163This project uses [Tailwind CSS](https://tailwindcss.com/) for styling.
164β
165## Linting & Formatting
166β
167This 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
170bun run lint
171bun run format
172bun run check
173```