Skip to content

Static Serve

you need to install the following packages

Terminal window
npm install @gaman/static

  • Auto-detects MIME types (customizable)
  • Supports automatic Gzip and Brotli compression
  • Supports ETag and caching via Cache-Control
  • Fallback to index.html for SPAs
  • Supports path rewriting and hooks for found/not-found files

OptionTypeDefaultDescription
pathstring"public"Root directory for static files
mimesRecord<string, string>-Custom MIME types based on file extensions
priorityPriority"very-high"Middleware execution order
defaultDocumentstring"index.html"Default file to serve when a directory is requested
rewriteRequestPath(path: string) => string-Function to transform request path before file resolution
onFound(path: string, ctx: Context) => void | Promise<void>-Callback when file is found and sent
onNotFound(path: string, ctx: Context) => void | Promise<void>-Callback when file is not found
cacheControlstring"public, max-age=3600"Cache-Control header
fallbackToIndexHTMLbooleanfalseFallback to index.html when file is not found (useful for SPAs)

index.ts
defineBootstrap((app) => {
app.registerIntegration(
gamanStatic({
path: "assets",
rewriteRequestPath: (path) => path.replace(/^\/static/, ""),
fallbackToIndexHTML: true,
mimes: {
".webmanifest": "application/manifest+json",
},
})
);
});

  • GamanJS logger is disabled automatically when static files are served to avoid duplicated logs.
  • .br and .gz files will be served automatically if the browser supports Accept-Encoding.