workshop/packages/nano-remix/CACHING.md

2.1 KiB

Build Caching in Nano-Remix

The nano-remix package now includes intelligent build caching to improve performance by avoiding unnecessary rebuilds.

How it Works

The caching system tracks:

  • File modification timestamps
  • Last build times
  • Output file existence

Routes are only rebuilt when:

  1. Source files have been modified since last build
  2. Output files don't exist
  3. Cache is explicitly disabled or forced

Usage Examples

Development Mode (with caching)

import { nanoRemix } from "@workshop/nano-remix"

// Default behavior - uses intelligent caching
export default {
  fetch: (req: Request) => nanoRemix(req),
}

Development Mode (without caching)

import { nanoRemix } from "@workshop/nano-remix"

// Disable caching for development if you want immediate rebuilds
export default {
  fetch: (req: Request) =>
    nanoRemix(req, {
      disableCache: true,
    }),
}

Production Setup

import { nanoRemix, preloadAllRoutes } from "@workshop/nano-remix"

// Pre-build all routes on startup for production
const server = {
  async fetch(req: Request) {
    return nanoRemix(req)
  },
}

// Build all routes once on server startup
await preloadAllRoutes()

export default server

Cache Management

import { clearBuildCache, getBuildCacheStats } from "@workshop/nano-remix"

// Clear cache (useful for development)
clearBuildCache()

// Get cache statistics for monitoring
const stats = getBuildCacheStats()
console.log("Cache entries:", stats.length)

Configuration Options

type Options = {
  routesDir?: string // Custom routes directory
  distDir?: string // Custom output directory
  disableCache?: boolean // Disable caching entirely
}

Performance Impact

With caching enabled:

  • First request to a route: Normal build time
  • Subsequent requests: Near-instant response (no rebuild)
  • File changes: Automatic rebuild on next request
  • Production: Pre-build all routes once

This should significantly improve your server performance, especially for routes that haven't changed!