Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

README.md

173 lines4.0 KB

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
60 "scripts": {
61 "start": "bun run server.ts"
62 }
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```