alepha@docs:~/docs/reference/primitives$
cat $etag.md
2 min read

#$etag

#Import

typescript
1import { $etag } from "alepha/server/etag";

#Overview

Middleware that enables ETag-based response caching per-route.

Sets per-request etag options in the ALS context. The global ServerEtagProvider hooks read these to generate ETags, handle 304s, and optionally store responses.

When store is enabled, the middleware also checks the cache before calling the handler, short-circuiting on cache hits.

Route middleware — works inside $action, $page, or any pipeline.

typescript
 1class UserController { 2  // ETag only (no response caching) 3  getUser = $action({ 4    use: [$etag()], 5    handler: async ({ params }) => { ... }, 6  }); 7  8  // ETag + response caching (store) 9  getProfile = $action({10    use: [$etag(true)],11    handler: async ({ params }) => { ... },12  });13 14  // Fine-grained control15  getStats = $action({16    use: [$etag({ store: { ttl: [5, "minutes"] }, control: { public: true, maxAge: 300 } })],17    handler: async ({ params }) => { ... },18  });19}

#Options

Option Type Required Description
store true | DurationLike | CachePrimitiveOptions No If true, enables storing cached responses
etag true No If true, enables ETag support for the cached responses.
public boolean No Indicates that the response may be cached by any cache.
private boolean No Indicates that the response is intended for a single user and must not be stored by a shared cache.
noCache boolean No Forces caches to submit the request to the origin server for validation before releasing a cached copy.
noStore boolean No Instructs caches not to store the response.
maxAge number | DurationLike No Maximum amount of time a resource is considered fresh
sMaxAge number | DurationLike No Overrides max-age for shared caches (e.g., CDNs)
mustRevalidate boolean No Indicates that once a resource becomes stale, caches must not use it without successful validation.
proxyRevalidate boolean No Similar to must-revalidate, but only for shared caches.
immutable boolean No Indicates that the response can be stored but must be revalidated before each use.