bun-types 1.3.3-canary.20251114T140703 → 1.3.3-canary.20251116T140533

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,107 +48,745 @@ mode: center
48
48
 
49
49
  ## Hosting
50
50
 
51
- <Steps>
52
- <Step title="Add Nitro to your project">
53
- Add [Nitro](https://nitro.build/) to your project. This tool allows you to deploy your TanStack Start app to different platforms.
51
+ To host your TanStack Start app, you can use [Nitro](https://nitro.build/) or a custom Bun server for production deployments.
54
52
 
55
- ```sh terminal icon="terminal"
56
- bun add nitro
57
- ```
53
+ <Tabs>
54
+ <Tab title="Nitro">
55
+ <Steps>
56
+ <Step title="Add Nitro to your project">
57
+ Add [Nitro](https://nitro.build/) to your project. This tool allows you to deploy your TanStack Start app to different platforms.
58
58
 
59
- </Step>
60
- <Step title={<span>Update your <code>vite.config.ts</code> file</span>}>
61
- Update your `vite.config.ts` file to include the necessary plugins for TanStack Start with Bun.
62
-
63
- ```ts vite.config.ts icon="/icons/typescript.svg"
64
- // other imports...
65
- import { nitro } from "nitro/vite"; // [!code ++]
66
-
67
- const config = defineConfig({
68
- plugins: [
69
- tanstackStart(),
70
- nitro({ preset: "bun" }), // [!code ++]
71
- // other plugins...
72
- ],
73
- });
74
-
75
- export default config;
76
- ```
59
+ ```sh terminal icon="terminal"
60
+ bun add nitro
61
+ ```
62
+
63
+ </Step>
64
+ <Step title={<span>Update your <code>vite.config.ts</code> file</span>}>
65
+ Update your `vite.config.ts` file to include the necessary plugins for TanStack Start with Bun.
66
+
67
+ ```ts vite.config.ts icon="/icons/typescript.svg"
68
+ // other imports...
69
+ import { nitro } from "nitro/vite"; // [!code ++]
70
+
71
+ const config = defineConfig({
72
+ plugins: [
73
+ tanstackStart(),
74
+ nitro({ preset: "bun" }), // [!code ++]
75
+ // other plugins...
76
+ ],
77
+ });
78
+
79
+ export default config;
80
+ ```
81
+
82
+ <Note>
83
+ The `bun` preset is optional, but it configures the build output specifically for Bun's runtime.
84
+ </Note>
85
+
86
+ </Step>
87
+ <Step title="Update the start command">
88
+ Make sure `build` and `start` scripts are present in your `package.json` file:
89
+
90
+ ```json package.json icon="file-json"
91
+ {
92
+ "scripts": {
93
+ "build": "bun --bun vite build", // [!code ++]
94
+ // The .output files are created by Nitro when you run `bun run build`.
95
+ // Not necessary when deploying to Vercel.
96
+ "start": "bun run .output/server/index.mjs" // [!code ++]
97
+ }
98
+ }
99
+ ```
100
+
101
+
102
+ <Note>
103
+ You do **not** need the custom `start` script when deploying to Vercel.
104
+ </Note>
77
105
 
106
+ </Step>
107
+ <Step title="Deploy your app">
108
+ Check out one of our guides to deploy your app to a hosting provider.
109
+
110
+ <Note>
111
+ When deploying to Vercel, you can either add the `"bunVersion": "1.x"` to your `vercel.json` file, or add it to the `nitro` config in your `vite.config.ts` file:
112
+
113
+ <Warning>
114
+ Do **not** use the `bun` Nitro preset when deploying to Vercel.
115
+ </Warning>
116
+
117
+ ```ts vite.config.ts icon="/icons/typescript.svg"
118
+ export default defineConfig({
119
+ plugins: [
120
+ tanstackStart(),
121
+ nitro({
122
+ preset: "bun", // [!code --]
123
+ vercel: { // [!code ++]
124
+ functions: { // [!code ++]
125
+ runtime: "bun1.x", // [!code ++]
126
+ }, // [!code ++]
127
+ }, // [!code ++]
128
+ }),
129
+ ],
130
+ });
131
+ ```
132
+ </Note>
133
+ </Step>
134
+ </Steps>
135
+
136
+ </Tab>
137
+ <Tab title="Custom Server">
78
138
  <Note>
79
- The `bun` preset is optional, but it configures the build output specifically for Bun's runtime.
139
+ This custom server implementation is based on [TanStack's Bun template](https://github.com/TanStack/router/blob/main/examples/react/start-bun/server.ts). It provides fine-grained control over static asset serving, including configurable memory management that preloads small files into memory for fast serving while serving larger files on-demand. This approach is useful when you need precise control over resource usage and asset loading behavior in production deployments.
80
140
  </Note>
81
141
 
82
- </Step>
83
- <Step title="Update the start command">
84
- Make sure `build` and `start` scripts are present in your `package.json` file:
142
+ <Steps>
143
+ <Step title="Create the production server">
144
+ Create a `server.ts` file in your project root with the following custom server implementation:
85
145
 
86
- ```json package.json icon="file-json"
87
- {
88
- "scripts": {
89
- "build": "bun --bun vite build", // [!code ++]
90
- // The .output files are created by Nitro when you run `bun run build`.
91
- // Not necessary when deploying to Vercel.
92
- "start": "bun run .output/server/index.mjs" // [!code ++]
146
+ ```ts server.ts icon="/icons/typescript.svg" expandable
147
+ /**
148
+ * TanStack Start Production Server with Bun
149
+ *
150
+ * A high-performance production server for TanStack Start applications that
151
+ * implements intelligent static asset loading with configurable memory management.
152
+ *
153
+ * Features:
154
+ * - Hybrid loading strategy (preload small files, serve large files on-demand)
155
+ * - Configurable file filtering with include/exclude patterns
156
+ * - Memory-efficient response generation
157
+ * - Production-ready caching headers
158
+ *
159
+ * Environment Variables:
160
+ *
161
+ * PORT (number)
162
+ * - Server port number
163
+ * - Default: 3000
164
+ *
165
+ * ASSET_PRELOAD_MAX_SIZE (number)
166
+ * - Maximum file size in bytes to preload into memory
167
+ * - Files larger than this will be served on-demand from disk
168
+ * - Default: 5242880 (5MB)
169
+ * - Example: ASSET_PRELOAD_MAX_SIZE=5242880 (5MB)
170
+ *
171
+ * ASSET_PRELOAD_INCLUDE_PATTERNS (string)
172
+ * - Comma-separated list of glob patterns for files to include
173
+ * - If specified, only matching files are eligible for preloading
174
+ * - Patterns are matched against filenames only, not full paths
175
+ * - Example: ASSET_PRELOAD_INCLUDE_PATTERNS="*.js,*.css,*.woff2"
176
+ *
177
+ * ASSET_PRELOAD_EXCLUDE_PATTERNS (string)
178
+ * - Comma-separated list of glob patterns for files to exclude
179
+ * - Applied after include patterns
180
+ * - Patterns are matched against filenames only, not full paths
181
+ * - Example: ASSET_PRELOAD_EXCLUDE_PATTERNS="*.map,*.txt"
182
+ *
183
+ * ASSET_PRELOAD_VERBOSE_LOGGING (boolean)
184
+ * - Enable detailed logging of loaded and skipped files
185
+ * - Default: false
186
+ * - Set to "true" to enable verbose output
187
+ *
188
+ * ASSET_PRELOAD_ENABLE_ETAG (boolean)
189
+ * - Enable ETag generation for preloaded assets
190
+ * - Default: true
191
+ * - Set to "false" to disable ETag support
192
+ *
193
+ * ASSET_PRELOAD_ENABLE_GZIP (boolean)
194
+ * - Enable Gzip compression for eligible assets
195
+ * - Default: true
196
+ * - Set to "false" to disable Gzip compression
197
+ *
198
+ * ASSET_PRELOAD_GZIP_MIN_SIZE (number)
199
+ * - Minimum file size in bytes required for Gzip compression
200
+ * - Files smaller than this will not be compressed
201
+ * - Default: 1024 (1KB)
202
+ *
203
+ * ASSET_PRELOAD_GZIP_MIME_TYPES (string)
204
+ * - Comma-separated list of MIME types eligible for Gzip compression
205
+ * - Supports partial matching for types ending with "/"
206
+ * - Default: text/,application/javascript,application/json,application/xml,image/svg+xml
207
+ *
208
+ * Usage:
209
+ * bun run server.ts
210
+ */
211
+
212
+ import path from 'node:path'
213
+
214
+ // Configuration
215
+ const SERVER_PORT = Number(process.env.PORT ?? 3000)
216
+ const CLIENT_DIRECTORY = './dist/client'
217
+ const SERVER_ENTRY_POINT = './dist/server/server.js'
218
+
219
+ // Logging utilities for professional output
220
+ const log = {
221
+ info: (message: string) => {
222
+ console.log(`[INFO] ${message}`)
223
+ },
224
+ success: (message: string) => {
225
+ console.log(`[SUCCESS] ${message}`)
226
+ },
227
+ warning: (message: string) => {
228
+ console.log(`[WARNING] ${message}`)
229
+ },
230
+ error: (message: string) => {
231
+ console.log(`[ERROR] ${message}`)
232
+ },
233
+ header: (message: string) => {
234
+ console.log(`\n${message}\n`)
235
+ },
93
236
  }
94
- }
95
- ```
96
237
 
238
+ // Preloading configuration from environment variables
239
+ const MAX_PRELOAD_BYTES = Number(
240
+ process.env.ASSET_PRELOAD_MAX_SIZE ?? 5 * 1024 * 1024, // 5MB default
241
+ )
97
242
 
98
- <Note>
99
- You do **not** need the custom `start` script when deploying to Vercel.
100
- </Note>
243
+ // Parse comma-separated include patterns (no defaults)
244
+ const INCLUDE_PATTERNS = (process.env.ASSET_PRELOAD_INCLUDE_PATTERNS ?? '')
245
+ .split(',')
246
+ .map((s) => s.trim())
247
+ .filter(Boolean)
248
+ .map((pattern: string) => convertGlobToRegExp(pattern))
101
249
 
102
- </Step>
103
- <Step title="Deploy your app">
104
- Check out one of our guides to deploy your app to a hosting provider.
250
+ // Parse comma-separated exclude patterns (no defaults)
251
+ const EXCLUDE_PATTERNS = (process.env.ASSET_PRELOAD_EXCLUDE_PATTERNS ?? '')
252
+ .split(',')
253
+ .map((s) => s.trim())
254
+ .filter(Boolean)
255
+ .map((pattern: string) => convertGlobToRegExp(pattern))
105
256
 
106
- <Note>
107
- When deploying to Vercel, you can either add the `"bunVersion": "1.x"` to your `vercel.json` file, or add it to the `nitro` config in your `vite.config.ts` file:
108
-
109
- <Warning>
110
- Do **not** use the `bun` Nitro preset when deploying to Vercel.
111
- </Warning>
112
-
113
- ```ts vite.config.ts icon="/icons/typescript.svg"
114
- export default defineConfig({
115
- plugins: [
116
- tanstackStart(),
117
- nitro({
118
- preset: "bun", // [!code --]
119
- vercel: { // [!code ++]
120
- functions: { // [!code ++]
121
- runtime: "bun1.x", // [!code ++]
122
- }, // [!code ++]
123
- }, // [!code ++]
124
- }),
125
- ],
126
- });
127
- ```
128
- </Note>
257
+ // Verbose logging flag
258
+ const VERBOSE = process.env.ASSET_PRELOAD_VERBOSE_LOGGING === 'true'
129
259
 
130
- <Columns cols={3}>
131
- <Card title="Vercel" href="/guides/deployment/vercel" icon="/icons/ecosystem/vercel.svg">
132
- Deploy on Vercel
133
- </Card>
134
- <Card title="Render" href="/guides/deployment/render" icon="/icons/ecosystem/render.svg">
135
- Deploy on Render
136
- </Card>
137
- <Card title="Railway" href="/guides/deployment/railway" icon="/icons/ecosystem/railway.svg">
138
- Deploy on Railway
139
- </Card>
140
- <Card title="DigitalOcean" href="/guides/deployment/digital-ocean" icon="/icons/ecosystem/digitalocean.svg">
141
- Deploy on DigitalOcean
142
- </Card>
143
- <Card title="AWS Lambda" href="/guides/deployment/aws-lambda" icon="/icons/ecosystem/aws.svg">
144
- Deploy on AWS Lambda
145
- </Card>
146
- <Card title="Google Cloud Run" href="/guides/deployment/google-cloud-run" icon="/icons/ecosystem/gcp.svg">
147
- Deploy on Google Cloud Run
148
- </Card>
149
- </Columns>
260
+ // Optional ETag feature
261
+ const ENABLE_ETAG = (process.env.ASSET_PRELOAD_ENABLE_ETAG ?? 'true') === 'true'
150
262
 
151
- </Step>
152
- </Steps>
263
+ // Optional Gzip feature
264
+ const ENABLE_GZIP = (process.env.ASSET_PRELOAD_ENABLE_GZIP ?? 'true') === 'true'
265
+ const GZIP_MIN_BYTES = Number(process.env.ASSET_PRELOAD_GZIP_MIN_SIZE ?? 1024) // 1KB
266
+ const GZIP_TYPES = (
267
+ process.env.ASSET_PRELOAD_GZIP_MIME_TYPES ??
268
+ 'text/,application/javascript,application/json,application/xml,image/svg+xml'
269
+ )
270
+ .split(',')
271
+ .map((v) => v.trim())
272
+ .filter(Boolean)
273
+
274
+ /**
275
+ * Convert a simple glob pattern to a regular expression
276
+ * Supports * wildcard for matching any characters
277
+ */
278
+ function convertGlobToRegExp(globPattern: string): RegExp {
279
+ // Escape regex special chars except *, then replace * with .*
280
+ const escapedPattern = globPattern
281
+ .replace(/[-/\\^$+?.()|[\]{}]/g, '\\$&')
282
+ .replace(/\*/g, '.*')
283
+ return new RegExp(`^${escapedPattern}$`, 'i')
284
+ }
285
+
286
+ /**
287
+ * Compute ETag for a given data buffer
288
+ */
289
+ function computeEtag(data: Uint8Array): string {
290
+ const hash = Bun.hash(data)
291
+ return `W/"${hash.toString(16)}-${data.byteLength.toString()}"`
292
+ }
293
+
294
+ /**
295
+ * Metadata for preloaded static assets
296
+ */
297
+ interface AssetMetadata {
298
+ route: string
299
+ size: number
300
+ type: string
301
+ }
302
+
303
+ /**
304
+ * In-memory asset with ETag and Gzip support
305
+ */
306
+ interface InMemoryAsset {
307
+ raw: Uint8Array
308
+ gz?: Uint8Array
309
+ etag?: string
310
+ type: string
311
+ immutable: boolean
312
+ size: number
313
+ }
314
+
315
+ /**
316
+ * Result of static asset preloading process
317
+ */
318
+ interface PreloadResult {
319
+ routes: Record<string, (req: Request) => Response | Promise<Response>>
320
+ loaded: AssetMetadata[]
321
+ skipped: AssetMetadata[]
322
+ }
323
+
324
+ /**
325
+ * Check if a file is eligible for preloading based on configured patterns
326
+ */
327
+ function isFileEligibleForPreloading(relativePath: string): boolean {
328
+ const fileName = relativePath.split(/[/\\]/).pop() ?? relativePath
329
+
330
+ // If include patterns are specified, file must match at least one
331
+ if (INCLUDE_PATTERNS.length > 0) {
332
+ if (!INCLUDE_PATTERNS.some((pattern) => pattern.test(fileName))) {
333
+ return false
334
+ }
335
+ }
336
+
337
+ // If exclude patterns are specified, file must not match any
338
+ if (EXCLUDE_PATTERNS.some((pattern) => pattern.test(fileName))) {
339
+ return false
340
+ }
341
+
342
+ return true
343
+ }
344
+
345
+ /**
346
+ * Check if a MIME type is compressible
347
+ */
348
+ function isMimeTypeCompressible(mimeType: string): boolean {
349
+ return GZIP_TYPES.some((type) =>
350
+ type.endsWith('/') ? mimeType.startsWith(type) : mimeType === type,
351
+ )
352
+ }
353
+
354
+ /**
355
+ * Conditionally compress data based on size and MIME type
356
+ */
357
+ function compressDataIfAppropriate(
358
+ data: Uint8Array,
359
+ mimeType: string,
360
+ ): Uint8Array | undefined {
361
+ if (!ENABLE_GZIP) return undefined
362
+ if (data.byteLength < GZIP_MIN_BYTES) return undefined
363
+ if (!isMimeTypeCompressible(mimeType)) return undefined
364
+ try {
365
+ return Bun.gzipSync(data.buffer as ArrayBuffer)
366
+ } catch {
367
+ return undefined
368
+ }
369
+ }
370
+
371
+ /**
372
+ * Create response handler function with ETag and Gzip support
373
+ */
374
+ function createResponseHandler(
375
+ asset: InMemoryAsset,
376
+ ): (req: Request) => Response {
377
+ return (req: Request) => {
378
+ const headers: Record<string, string> = {
379
+ 'Content-Type': asset.type,
380
+ 'Cache-Control': asset.immutable
381
+ ? 'public, max-age=31536000, immutable'
382
+ : 'public, max-age=3600',
383
+ }
384
+
385
+ if (ENABLE_ETAG && asset.etag) {
386
+ const ifNone = req.headers.get('if-none-match')
387
+ if (ifNone && ifNone === asset.etag) {
388
+ return new Response(null, {
389
+ status: 304,
390
+ headers: { ETag: asset.etag },
391
+ })
392
+ }
393
+ headers.ETag = asset.etag
394
+ }
395
+
396
+ if (
397
+ ENABLE_GZIP &&
398
+ asset.gz &&
399
+ req.headers.get('accept-encoding')?.includes('gzip')
400
+ ) {
401
+ headers['Content-Encoding'] = 'gzip'
402
+ headers['Content-Length'] = String(asset.gz.byteLength)
403
+ const gzCopy = new Uint8Array(asset.gz)
404
+ return new Response(gzCopy, { status: 200, headers })
405
+ }
406
+
407
+ headers['Content-Length'] = String(asset.raw.byteLength)
408
+ const rawCopy = new Uint8Array(asset.raw)
409
+ return new Response(rawCopy, { status: 200, headers })
410
+ }
411
+ }
412
+
413
+ /**
414
+ * Create composite glob pattern from include patterns
415
+ */
416
+ function createCompositeGlobPattern(): Bun.Glob {
417
+ const raw = (process.env.ASSET_PRELOAD_INCLUDE_PATTERNS ?? '')
418
+ .split(',')
419
+ .map((s) => s.trim())
420
+ .filter(Boolean)
421
+ if (raw.length === 0) return new Bun.Glob('**/*')
422
+ if (raw.length === 1) return new Bun.Glob(raw[0])
423
+ return new Bun.Glob(`{${raw.join(',')}}`)
424
+ }
425
+
426
+ /**
427
+ * Initialize static routes with intelligent preloading strategy
428
+ * Small files are loaded into memory, large files are served on-demand
429
+ */
430
+ async function initializeStaticRoutes(
431
+ clientDirectory: string,
432
+ ): Promise<PreloadResult> {
433
+ const routes: Record<string, (req: Request) => Response | Promise<Response>> =
434
+ {}
435
+ const loaded: AssetMetadata[] = []
436
+ const skipped: AssetMetadata[] = []
437
+
438
+ log.info(`Loading static assets from ${clientDirectory}...`)
439
+ if (VERBOSE) {
440
+ console.log(
441
+ `Max preload size: ${(MAX_PRELOAD_BYTES / 1024 / 1024).toFixed(2)} MB`,
442
+ )
443
+ if (INCLUDE_PATTERNS.length > 0) {
444
+ console.log(
445
+ `Include patterns: ${process.env.ASSET_PRELOAD_INCLUDE_PATTERNS ?? ''}`,
446
+ )
447
+ }
448
+ if (EXCLUDE_PATTERNS.length > 0) {
449
+ console.log(
450
+ `Exclude patterns: ${process.env.ASSET_PRELOAD_EXCLUDE_PATTERNS ?? ''}`,
451
+ )
452
+ }
453
+ }
454
+
455
+ let totalPreloadedBytes = 0
456
+
457
+ try {
458
+ const glob = createCompositeGlobPattern()
459
+ for await (const relativePath of glob.scan({ cwd: clientDirectory })) {
460
+ const filepath = path.join(clientDirectory, relativePath)
461
+ const route = `/${relativePath.split(path.sep).join(path.posix.sep)}`
462
+
463
+ try {
464
+ // Get file metadata
465
+ const file = Bun.file(filepath)
466
+
467
+ // Skip if file doesn't exist or is empty
468
+ if (!(await file.exists()) || file.size === 0) {
469
+ continue
470
+ }
471
+
472
+ const metadata: AssetMetadata = {
473
+ route,
474
+ size: file.size,
475
+ type: file.type || 'application/octet-stream',
476
+ }
477
+
478
+ // Determine if file should be preloaded
479
+ const matchesPattern = isFileEligibleForPreloading(relativePath)
480
+ const withinSizeLimit = file.size <= MAX_PRELOAD_BYTES
481
+
482
+ if (matchesPattern && withinSizeLimit) {
483
+ // Preload small files into memory with ETag and Gzip support
484
+ const bytes = new Uint8Array(await file.arrayBuffer())
485
+ const gz = compressDataIfAppropriate(bytes, metadata.type)
486
+ const etag = ENABLE_ETAG ? computeEtag(bytes) : undefined
487
+ const asset: InMemoryAsset = {
488
+ raw: bytes,
489
+ gz,
490
+ etag,
491
+ type: metadata.type,
492
+ immutable: true,
493
+ size: bytes.byteLength,
494
+ }
495
+ routes[route] = createResponseHandler(asset)
496
+
497
+ loaded.push({ ...metadata, size: bytes.byteLength })
498
+ totalPreloadedBytes += bytes.byteLength
499
+ } else {
500
+ // Serve large or filtered files on-demand
501
+ routes[route] = () => {
502
+ const fileOnDemand = Bun.file(filepath)
503
+ return new Response(fileOnDemand, {
504
+ headers: {
505
+ 'Content-Type': metadata.type,
506
+ 'Cache-Control': 'public, max-age=3600',
507
+ },
508
+ })
509
+ }
510
+
511
+ skipped.push(metadata)
512
+ }
513
+ } catch (error: unknown) {
514
+ if (error instanceof Error && error.name !== 'EISDIR') {
515
+ log.error(`Failed to load ${filepath}: ${error.message}`)
516
+ }
517
+ }
518
+ }
519
+
520
+ // Show detailed file overview only when verbose mode is enabled
521
+ if (VERBOSE && (loaded.length > 0 || skipped.length > 0)) {
522
+ const allFiles = [...loaded, ...skipped].sort((a, b) =>
523
+ a.route.localeCompare(b.route),
524
+ )
525
+
526
+ // Calculate max path length for alignment
527
+ const maxPathLength = Math.min(
528
+ Math.max(...allFiles.map((f) => f.route.length)),
529
+ 60,
530
+ )
531
+
532
+ // Format file size with KB and actual gzip size
533
+ const formatFileSize = (bytes: number, gzBytes?: number) => {
534
+ const kb = bytes / 1024
535
+ const sizeStr = kb < 100 ? kb.toFixed(2) : kb.toFixed(1)
536
+
537
+ if (gzBytes !== undefined) {
538
+ const gzKb = gzBytes / 1024
539
+ const gzStr = gzKb < 100 ? gzKb.toFixed(2) : gzKb.toFixed(1)
540
+ return {
541
+ size: sizeStr,
542
+ gzip: gzStr,
543
+ }
544
+ }
545
+
546
+ // Rough gzip estimation (typically 30-70% compression) if no actual gzip data
547
+ const gzipKb = kb * 0.35
548
+ return {
549
+ size: sizeStr,
550
+ gzip: gzipKb < 100 ? gzipKb.toFixed(2) : gzipKb.toFixed(1),
551
+ }
552
+ }
553
+
554
+ if (loaded.length > 0) {
555
+ console.log('\n📁 Preloaded into memory:')
556
+ console.log(
557
+ 'Path │ Size │ Gzip Size',
558
+ )
559
+ loaded
560
+ .sort((a, b) => a.route.localeCompare(b.route))
561
+ .forEach((file) => {
562
+ const { size, gzip } = formatFileSize(file.size)
563
+ const paddedPath = file.route.padEnd(maxPathLength)
564
+ const sizeStr = `${size.padStart(7)} kB`
565
+ const gzipStr = `${gzip.padStart(7)} kB`
566
+ console.log(`${paddedPath} │ ${sizeStr} │ ${gzipStr}`)
567
+ })
568
+ }
569
+
570
+ if (skipped.length > 0) {
571
+ console.log('\n💾 Served on-demand:')
572
+ console.log(
573
+ 'Path │ Size │ Gzip Size',
574
+ )
575
+ skipped
576
+ .sort((a, b) => a.route.localeCompare(b.route))
577
+ .forEach((file) => {
578
+ const { size, gzip } = formatFileSize(file.size)
579
+ const paddedPath = file.route.padEnd(maxPathLength)
580
+ const sizeStr = `${size.padStart(7)} kB`
581
+ const gzipStr = `${gzip.padStart(7)} kB`
582
+ console.log(`${paddedPath} │ ${sizeStr} │ ${gzipStr}`)
583
+ })
584
+ }
585
+ }
586
+
587
+ // Show detailed verbose info if enabled
588
+ if (VERBOSE) {
589
+ if (loaded.length > 0 || skipped.length > 0) {
590
+ const allFiles = [...loaded, ...skipped].sort((a, b) =>
591
+ a.route.localeCompare(b.route),
592
+ )
593
+ console.log('\n📊 Detailed file information:')
594
+ console.log(
595
+ 'Status │ Path │ MIME Type │ Reason',
596
+ )
597
+ allFiles.forEach((file) => {
598
+ const isPreloaded = loaded.includes(file)
599
+ const status = isPreloaded ? 'MEMORY' : 'ON-DEMAND'
600
+ const reason =
601
+ !isPreloaded && file.size > MAX_PRELOAD_BYTES
602
+ ? 'too large'
603
+ : !isPreloaded
604
+ ? 'filtered'
605
+ : 'preloaded'
606
+ const route =
607
+ file.route.length > 30
608
+ ? file.route.substring(0, 27) + '...'
609
+ : file.route
610
+ console.log(
611
+ `${status.padEnd(12)} │ ${route.padEnd(30)} │ ${file.type.padEnd(28)} │ ${reason.padEnd(10)}`,
612
+ )
613
+ })
614
+ } else {
615
+ console.log('\n📊 No files found to display')
616
+ }
617
+ }
618
+
619
+ // Log summary after the file list
620
+ console.log() // Empty line for separation
621
+ if (loaded.length > 0) {
622
+ log.success(
623
+ `Preloaded ${String(loaded.length)} files (${(totalPreloadedBytes / 1024 / 1024).toFixed(2)} MB) into memory`,
624
+ )
625
+ } else {
626
+ log.info('No files preloaded into memory')
627
+ }
628
+
629
+ if (skipped.length > 0) {
630
+ const tooLarge = skipped.filter((f) => f.size > MAX_PRELOAD_BYTES).length
631
+ const filtered = skipped.length - tooLarge
632
+ log.info(
633
+ `${String(skipped.length)} files will be served on-demand (${String(tooLarge)} too large, ${String(filtered)} filtered)`,
634
+ )
635
+ }
636
+ } catch (error) {
637
+ log.error(
638
+ `Failed to load static files from ${clientDirectory}: ${String(error)}`,
639
+ )
640
+ }
641
+
642
+ return { routes, loaded, skipped }
643
+ }
644
+
645
+ /**
646
+ * Initialize the server
647
+ */
648
+ async function initializeServer() {
649
+ log.header('Starting Production Server')
650
+
651
+ // Load TanStack Start server handler
652
+ let handler: { fetch: (request: Request) => Response | Promise<Response> }
653
+ try {
654
+ const serverModule = (await import(SERVER_ENTRY_POINT)) as {
655
+ default: { fetch: (request: Request) => Response | Promise<Response> }
656
+ }
657
+ handler = serverModule.default
658
+ log.success('TanStack Start application handler initialized')
659
+ } catch (error) {
660
+ log.error(`Failed to load server handler: ${String(error)}`)
661
+ process.exit(1)
662
+ }
663
+
664
+ // Build static routes with intelligent preloading
665
+ const { routes } = await initializeStaticRoutes(CLIENT_DIRECTORY)
666
+
667
+ // Create Bun server
668
+ const server = Bun.serve({
669
+ port: SERVER_PORT,
670
+
671
+ routes: {
672
+ // Serve static assets (preloaded or on-demand)
673
+ ...routes,
674
+
675
+ // Fallback to TanStack Start handler for all other routes
676
+ '/*': (req: Request) => {
677
+ try {
678
+ return handler.fetch(req)
679
+ } catch (error) {
680
+ log.error(`Server handler error: ${String(error)}`)
681
+ return new Response('Internal Server Error', { status: 500 })
682
+ }
683
+ },
684
+ },
685
+
686
+ // Global error handler
687
+ error(error) {
688
+ log.error(
689
+ `Uncaught server error: ${error instanceof Error ? error.message : String(error)}`,
690
+ )
691
+ return new Response('Internal Server Error', { status: 500 })
692
+ },
693
+ })
694
+
695
+ log.success(`Server listening on http://localhost:${String(server.port)}`)
696
+ }
697
+
698
+ // Initialize the server
699
+ initializeServer().catch((error: unknown) => {
700
+ log.error(`Failed to start server: ${String(error)}`)
701
+ process.exit(1)
702
+ })
703
+ ```
704
+
705
+ </Step>
706
+ <Step title="Update package.json scripts">
707
+ Add a `start` script to run the custom server:
708
+
709
+ ```json package.json icon="file-json"
710
+ {
711
+ "scripts": {
712
+ "build": "bun --bun vite build",
713
+ "start": "bun run server.ts" // [!code ++]
714
+ }
715
+ }
716
+ ```
717
+
718
+ </Step>
719
+ <Step title="Build and run">
720
+ Build your application and start the server:
721
+
722
+ ```sh terminal icon="terminal"
723
+ bun run build
724
+ bun run start
725
+ ```
726
+
727
+ The server will start on port 3000 by default (configurable via `PORT` environment variable).
728
+
729
+ </Step>
730
+ </Steps>
731
+
732
+ </Tab>
733
+ </Tabs>
734
+
735
+ <Columns cols={3}>
736
+ <Card title="Vercel" href="/guides/deployment/vercel" icon="/icons/ecosystem/vercel.svg">
737
+ Deploy on Vercel
738
+ </Card>
739
+ <Card title="Render" href="/guides/deployment/render" icon="/icons/ecosystem/render.svg">
740
+ Deploy on Render
741
+ </Card>
742
+ <Card title="Railway" href="/guides/deployment/railway" icon="/icons/ecosystem/railway.svg">
743
+ Deploy on Railway
744
+ </Card>
745
+ <Card title="DigitalOcean" href="/guides/deployment/digital-ocean" icon="/icons/ecosystem/digitalocean.svg">
746
+ Deploy on DigitalOcean
747
+ </Card>
748
+ <Card title="AWS Lambda" href="/guides/deployment/aws-lambda" icon="/icons/ecosystem/aws.svg">
749
+ Deploy on AWS Lambda
750
+ </Card>
751
+ <Card title="Google Cloud Run" href="/guides/deployment/google-cloud-run" icon="/icons/ecosystem/gcp.svg">
752
+ Deploy on Google Cloud Run
753
+ </Card>
754
+ </Columns>
755
+
756
+ ---
757
+
758
+ ## Templates
759
+
760
+ <Columns cols={2}>
761
+ <Card
762
+ title="Todo App with Tanstack + Bun"
763
+ img="/images/templates/bun-tanstack-todo.png"
764
+ href="https://github.com/bun-templates/bun-tanstack-todo"
765
+ arrow="true"
766
+ cta="Go to template"
767
+ >
768
+ A Todo application built with Bun, TanStack Start, and PostgreSQL.
769
+ </Card>
770
+ <Card
771
+ title="Bun + TanStack Start Application"
772
+ img="/images/templates/bun-tanstack-basic.png"
773
+ href="https://github.com/bun-templates/bun-tanstack-basic"
774
+ arrow="true"
775
+ cta="Go to template"
776
+ >
777
+ A TanStack Start template using Bun with SSR and file-based routing.
778
+ </Card>
779
+ <Card
780
+ title="Basic Bun + Tanstack Starter"
781
+ img="/images/templates/bun-tanstack-start.png"
782
+ href="https://github.com/bun-templates/bun-tanstack-start"
783
+ arrow="true"
784
+ cta="Go to template"
785
+ >
786
+ The basic TanStack starter using the Bun runtime and Bun's file APIs.
787
+ </Card>
788
+ </Columns>
789
+
790
+ ---
153
791
 
154
792
  [→ See TanStack Start's official documentation](https://tanstack.com/start/latest/docs/framework/react/guide/hosting) for more information on hosting.