Skip to content

Commit 7d4df72

Browse files
authored
docs(solid-start): hosting guide for bun (#5817)
1 parent 6d2a5da commit 7d4df72

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

docs/start/framework/solid/guide/hosting.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,108 @@ export default defineConfig({
289289
})
290290
```
291291

292+
#### Production Server with Bun
293+
294+
Alternatively, you can use a custom server implementation that leverages Bun's native APIs.
295+
296+
We provide a reference implementation that demonstrates one approach to building a production-ready Bun server. This example uses Bun-native functions for optimal performance and includes features like intelligent asset preloading and memory management.
297+
298+
**This is a starting point - feel free to adapt it to your needs or simplify it for your use case.**
299+
300+
**What this example demonstrates:**
301+
302+
- Serving static assets using Bun's native file handling
303+
- Hybrid loading strategy (preload small files, serve large files on-demand)
304+
- Optional features like ETag support and Gzip compression
305+
- Production-ready caching headers
306+
307+
**Quick Setup:**
308+
309+
1. Copy the [`server.ts`](https://github.com/tanstack/router/blob/main/examples/react/start-bun/server.ts) file from the example repository to your project root (or use it as inspiration for your own implementation)
310+
311+
2. Build your application:
312+
313+
```sh
314+
bun run build
315+
```
316+
317+
3. Start the server:
318+
319+
```sh
320+
bun run server.ts
321+
```
322+
323+
**Configuration (Optional):**
324+
325+
The reference server implementation includes several optional configuration options via environment variables. You can use these as-is, modify them, or remove features you don't need:
326+
327+
```sh
328+
# Basic usage - just works out of the box
329+
bun run server.ts
330+
331+
# Common configurations
332+
PORT=8080 bun run server.ts # Custom port
333+
ASSET_PRELOAD_VERBOSE_LOGGING=true bun run server.ts # See what's happening
334+
```
335+
336+
**Available Environment Variables:**
337+
338+
| Variable | Description | Default |
339+
| -------------------------------- | -------------------------------------------------- | ----------------------------------------------------------------------------- |
340+
| `PORT` | Server port | `3000` |
341+
| `ASSET_PRELOAD_MAX_SIZE` | Maximum file size to preload into memory (bytes) | `5242880` (5MB) |
342+
| `ASSET_PRELOAD_INCLUDE_PATTERNS` | Comma-separated glob patterns for files to include | All files |
343+
| `ASSET_PRELOAD_EXCLUDE_PATTERNS` | Comma-separated glob patterns for files to exclude | None |
344+
| `ASSET_PRELOAD_VERBOSE_LOGGING` | Enable detailed logging | `false` |
345+
| `ASSET_PRELOAD_ENABLE_ETAG` | Enable ETag generation | `true` |
346+
| `ASSET_PRELOAD_ENABLE_GZIP` | Enable Gzip compression | `true` |
347+
| `ASSET_PRELOAD_GZIP_MIN_SIZE` | Minimum file size for Gzip (bytes) | `1024` (1KB) |
348+
| `ASSET_PRELOAD_GZIP_MIME_TYPES` | MIME types eligible for Gzip | `text/,application/javascript,application/json,application/xml,image/svg+xml` |
349+
350+
<details>
351+
<summary>Advanced configuration examples</summary>
352+
353+
```sh
354+
# Optimize for minimal memory usage
355+
ASSET_PRELOAD_MAX_SIZE=1048576 bun run server.ts
356+
357+
# Preload only critical assets
358+
ASSET_PRELOAD_INCLUDE_PATTERNS="*.js,*.css" \
359+
ASSET_PRELOAD_EXCLUDE_PATTERNS="*.map,vendor-*" \
360+
bun run server.ts
361+
362+
# Disable optional features
363+
ASSET_PRELOAD_ENABLE_ETAG=false \
364+
ASSET_PRELOAD_ENABLE_GZIP=false \
365+
bun run server.ts
366+
367+
# Custom Gzip configuration
368+
ASSET_PRELOAD_GZIP_MIN_SIZE=2048 \
369+
ASSET_PRELOAD_GZIP_MIME_TYPES="text/,application/javascript,application/json" \
370+
bun run server.ts
371+
```
372+
373+
</details>
374+
375+
**Example Output:**
376+
377+
```txt
378+
📦 Loading static assets from ./dist/client...
379+
Max preload size: 5.00 MB
380+
381+
📁 Preloaded into memory:
382+
/assets/index-a1b2c3d4.js 45.23 kB │ gzip: 15.83 kB
383+
/assets/index-e5f6g7h8.css 12.45 kB │ gzip: 4.36 kB
384+
385+
💾 Served on-demand:
386+
/assets/vendor-i9j0k1l2.js 245.67 kB │ gzip: 86.98 kB
387+
388+
✅ Preloaded 2 files (57.68 KB) into memory
389+
🚀 Server running at http://localhost:3000
390+
```
391+
392+
For a complete working example, check out the [TanStack Start + Bun example](https://github.com/TanStack/router/tree/main/examples/react/start-bun) in this repository.
393+
292394
### Appwrite Sites
293395

294396
When deploying to [Appwrite Sites](https://appwrite.io/products/sites), you'll need to complete a few steps:

0 commit comments

Comments
 (0)