You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit d4eea12
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: .agents/http/response.md
+12-4Lines changed: 12 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# The response side — `entry.ts`, `validate.ts`, `vary.ts`, `cache-control.ts`, `conditional.ts`
2
2
3
-
`defineCachedHandler` uses `cachedFunction<Response>` and divides work at the `serialize` boundary. The **resolver** narrows the request and returns the handler's live `Response`. Internal **`serialize`** consumes the body, creates `etag`, `last-modified`, `cache-control`, and `Vary`, removes every `Set-Cookie`, and creates the stored `ResponseCacheEntry`. **`transform`** rebuilds a servable response and adds the cache-status header on read. `CachedEventHandlerOptions` uses `Omit` for all three hooks so caller options cannot conflict with this internal use.
3
+
`defineCachedHandler` uses `cachedFunction<Response>` and divides work at the `serialize` boundary. The **resolver** narrows the request and returns the handler's live `Response`. Internal **`serialize`** consumes the body, creates `etag`, `cache-control`, and `Vary`, removes every `Set-Cookie`, and creates the stored `ResponseCacheEntry`. **`transform`** rebuilds a servable response and adds the cache-status header on read. `CachedEventHandlerOptions` uses `Omit` for all three hooks so caller options cannot conflict with this internal use.
4
4
5
5
## What may be stored
6
6
@@ -41,7 +41,7 @@ The wrapper must compute its internal override **first and independently**. Run
41
41
42
42
## `cache-control` synthesis
43
43
44
-
Create `cache-control`, `etag`, and `last-modified`. Never replace an explicit `cache-control` from the handler.
44
+
Create `cache-control`and `etag`. Never replace an explicit `cache-control` from the handler.
45
45
46
46
Gate synthesis on **`isCacheableStatus`, `hasVaryWildcard`, and `hasUnkeyedVary`**. `validate` rejects responses with the same three shared predicates. Advertisement and storage must not use different logic. They differed before because `serialize` runs before `validate` and always added the header. A 500 was not stored, so the origin received every request and had no protection. However, CDNs received `s-maxage=60, stale-while-revalidate=600` and kept the error for 11 minutes while revalidation succeeded. The two layers had opposite behavior.
47
47
@@ -57,10 +57,18 @@ Finding 10.2. `serialize` receives the full `CacheEntry`. Read `entry.maxAge ??
57
57
58
58
Emit `max-age=<lifetime>` whenever a lifetime exists, including zero. When `swr` is enabled, also emit `s-maxage=<the same number>`. Emit `stale-while-revalidate=<staleMaxAge>` only when a stale window has a defined value.
59
59
60
-
-**Emit `max-age` with `s-maxage`; do not replace it** (finding 10.3). `s-maxage` applies only to shared caches and overrides `max-age` there under RFC 9111 §5.2.2.10. CDNs therefore behave the same. A private cache previously received no freshness lifetime and used heuristic freshness based on `Date − Last-Modified` under §4.2.2. This result was about zero because `last-modified` is set when the entry is created. Browsers revalidated on **every** navigation while ocache retained the value for full `maxAge`. Use the same value in both directives because ocache enforces that value. A smaller value would describe an unenforced limit. A larger value would claim too much. Keep `s-maxage` because it also allows shared caches to store responses to requests with `Authorization` under §3.5. This can occur with `allowAuthorization`.
60
+
-**Emit `max-age` with `s-maxage`; do not replace it** (finding 10.3). `s-maxage` applies only to shared caches and overrides `max-age` there under RFC 9111 §5.2.2.10. CDNs therefore behave the same. A private cache previously received no freshness lifetime and used heuristic freshness based on `Date − Last-Modified` under §4.2.2. No `last-modified` is synthesized, so that heuristic has no basis at all. Browsers revalidated on **every** navigation while ocache retained the value for full `maxAge`. Use the same value in both directives because ocache enforces that value. A smaller value would describe an unenforced limit. A larger value would claim too much. Keep `s-maxage` because it also allows shared caches to store responses to requests with `Authorization` under §3.5. This can occur with `allowAuthorization`.
61
61
-**Never emit `stale-while-revalidate` without a value** (finding 10.4). RFC 5861 §3 requires delta-seconds. The old ISR form emitted a token without an argument. Parsers had to ignore it under RFC 9111 §5.2.3, so downstream stale reuse disappeared even though the header appeared to request it. Add **nothing** in its place. This stale window is unbounded, so no number describes it. Do not invent `maxAge` or one year, because ocache does not enforce such a value. This omission has no practical cost. When downstream `max-age` expires, that cache revalidates. ocache can serve the retained stale value to that request while refreshing in the background. ISR still runs at the layer that enforces it. `{ swr: true, maxAge: 60 }` emits `max-age=60, s-maxage=60`. `docs/1.guide/9.isr.md` documents this exact string.
62
62
- Treat `maxAge`**the same with and without `swr`**. Emit it when present, including zero. Do not emit it when absent. Earlier branches differed: SWR used `!= null`, while non-SWR used a truthy check. `{ swr: true, maxAge: 0 }` emitted `s-maxage=0`, while `{ maxAge: 0 }` emitted nothing. Using `!= null` also makes `validate` read the same zero-lifetime opt-out from synthesized and manual headers. Therefore, **`maxAge: 0` prevents storage in both cases**. This is a restriction. `cache.ts` already makes a `<= 0` TTL resolve on every access, so the handler already ran each time. The change avoids storing an unused entry and sends an accurate `max-age=0` instead of allowing heuristic caching. The opposite choice would begin storing `{ swr: true, maxAge: 0 }` and serving it stale. The `!= null` check for `maxAge` is defensive. The same check for `staleMaxAge` is required for ISR.
63
-
-`sendCacheControl: false` disables all synthesis. This provides **server-only caching** for issue #49 and nitro#3997. The cache still stores and serves the entry. SWR, `etag`, and `last-modified` still work. Nothing advertises cacheability downstream. Do not use `no-store` or `private` to achieve this result because `validate` would also reject the entry. This option controls only ocache synthesis. Keep an explicit handler `cache-control` unchanged and send it.
63
+
-`sendCacheControl: false` disables all synthesis. This provides **server-only caching** for issue #49 and nitro#3997. The cache still stores and serves the entry. SWR and `etag` still work. Nothing advertises cacheability downstream. Do not use `no-store` or `private` to achieve this result because `validate` would also reject the entry. This option controls only ocache synthesis. Keep an explicit handler `cache-control` unchanged and send it.
64
+
65
+
### Never synthesize `last-modified`
66
+
67
+
`serialize` once stamped `new Date().toUTCString()` whenever the handler sent no `last-modified`. An HTTP-date has one-second resolution, so two **different** bodies filled inside the same second carried the same value. A client holding the first one then sent `If-Modified-Since: <that date>`, `defaultHandleCacheHeaders` found `date >= modifiedTime`, and it answered `304` for content that had changed. Fill time is not a modification time in the other direction either: refilling an unchanged entry moved the date forward for a representation that never changed.
68
+
69
+
The synthesized weak etag is the validator for a synthesized response, and it is derived from the stored bytes, so it changes exactly when the representation does. `If-None-Match` takes precedence anyway, so the date branch only ever ran for the clients that had no etag to send. Only a handler that knows when its content changed may set `last-modified`; both `defineCachedHandler` paths pass `modifiedTime: undefined` when the header is absent, and `headersOnly` already worked this way.
70
+
71
+
This removes a header from responses that do not set their own. Downstream heuristic freshness (RFC 9111 §4.2.2) loses its `Date − Last-Modified` input, which matters only when nothing advertises `cache-control` — that is, under `sendCacheControl: false`. Guessing a lifetime from a fabricated date was never a promise ocache could keep.
Copy file name to clipboardExpand all lines: docs/1.index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ ocache provides composable caching tools. It has no framework dependencies. It w
10
10
You can use two main tools:
11
11
12
12
-[`defineCachedFunction`](/guide/functions) — adds TTL, stale-while-revalidate, integrity checks, and in-flight request deduplication to any async function.
13
-
-[`defineCachedHandler`](/guide/handler) — adds full response caching to an HTTP handler. It supports `etag`, `last-modified`, `304 Not Modified`, `Cache-Control`, and more.
13
+
-[`defineCachedHandler`](/guide/handler) — adds full response caching to an HTTP handler. It supports `etag`, `304 Not Modified`, `Cache-Control`, and more.
Copy file name to clipboardExpand all lines: docs/5.handler.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ title: Caching HTTP Handlers
5
5
6
6
# Caching HTTP Handlers
7
7
8
-
Wrap an HTTP handler with `defineCachedHandler` to cache its `Response` automatically. It supports `etag`, `last-modified`, and `304 Not Modified`. It works with any runtime that provides the standard `Request` and `Response` APIs. The event has the form `{ req: Request; url?: URL }`.
8
+
Wrap an HTTP handler with `defineCachedHandler` to cache its `Response` automatically. It supports `etag` and `304 Not Modified`. It works with any runtime that provides the standard `Request` and `Response` APIs. The event has the form `{ req: Request; url?: URL }`.
9
9
10
10
```ts
11
11
import { defineCachedHandler } from"ocache";
@@ -36,7 +36,7 @@ The first request runs your handler and stores the response. Later requests use
36
36
37
37
-**Cache keys** — ocache creates keys from the handler [name](#cache-key-name), request [origin](#multi-host-deployments), URL path, and request method. The keys also include query parameters and headers that you choose to vary.
38
38
-**Conditional requests** — ocache responds with `304 Not Modified` when the client sends a matching `if-none-match` or `if-modified-since`.
39
-
-**Response headers** — ocache creates `etag`, `last-modified`, and `cache-control` when the handler does not set them.
39
+
-**Response headers** — ocache creates `etag`and `cache-control` when the handler does not set them. It never creates `last-modified`, because the time it fills an entry is not the time the content changed. Set that header yourself when the handler knows the real modification time.
40
40
-**`Vary`** — ocache advertises the request headers that vary the response. These come from `varies` and include `Cookie` when you set `allowCookies`. ocache refuses to store a response if its own `Vary` names another header.
41
41
-**Binary responses** — ocache caches images, tiles, and other non-text bodies. It replays them byte for byte.
42
42
-**Resolution deadline** — a handler or upstream that never responds cannot block its cache key forever. ocache abandons the shared resolution after [`maxResolveTime`](/guide/functions#resolution-deadline), which is 30 seconds by default. Waiting requests reject instead of continuing to wait.
@@ -223,7 +223,7 @@ This affects only cacheable requests. Other methods, such as `POST`, bypass the
223
223
224
224
## Conditionalrequests&304
225
225
226
-
Thehandlerreturns`304 Not Modified`withanemptybodywhen`if-none-match`matchesthestored`etag`. Whentherequesthasno`if-none-match`, itreturns304if`if-modified-since`isatorafter`last-modified`:
226
+
Thehandlerreturns`304 Not Modified`withanemptybodywhen`if-none-match`matchesthestored`etag`. Whentherequesthasno`if-none-match`, itreturns304if`if-modified-since`isatorafterthe`last-modified`**thatthehandlerset**. ocachenevercreatesthatheader, soahandlerthatsetsnodateisvalidatedbyits`etag`alone:
227
227
228
228
```ts
229
229
const handler = defineCachedHandler(() => new Response("<h1>Hi</h1>"), {
Copy file name to clipboardExpand all lines: docs/8.cache-control.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ Before ocache stores a response, it applies these built-in checks. If a response
16
16
-**`Vary: *`** — this is the strongest "do not share this" signal other than `no-store`. No stored response can match such a request (RFC 9111 §4.1). ocache keeps one entry for each cache key, so it must reject the entry.
17
17
-**A `Vary` that names a request header outside the cache key** — `Vary` states that responses are not interchangeable (RFC 9111 §4.1). ocache keys only on [`varies`](/guide/handler#varying-by-request-headers) and `Cookie` under [`allowCookies`](/guide/cookies). If a handler returns `Vary: Accept-Language` without `varies: ["accept-language"]`, ocache could replay the first language to all other languages. Instead, it rejects the entry. ocache caches a response normally when `Vary` names only headers already in the key. Make the two lists agree. See [Varying by request headers](/guide/handler#varying-by-request-headers).
18
18
-**A missing body** — there is nothing to replay. A zero-byte body is different. An empty `200` is valid and caches normally.
19
-
-**An `etag` or `last-modified` with the literal string `"undefined"`** — a handler can create this value when it converts a missing value to a string. ocache creates both headers when the handler omits them, so a genuinely missing header never reaches this check. ocache rejects the placeholder because it would break conditional requests.
19
+
-**An `etag` or `last-modified` with the literal string `"undefined"`** — a handler can create this value when it converts a missing value to a string. A header the handler truly omits is absent rather than this string, so it never reaches this check. ocache rejects the placeholder because it would break conditional requests.
20
20
21
21
These checks always apply. You cannot turn them off.
22
22
@@ -37,7 +37,7 @@ When the handler does not set `Cache-Control`, ocache creates one from the lifet
37
37
38
38
Important details about this header:
39
39
40
-
-**`max-age` uses the same value as `s-maxage`.**`s-maxage` applies only to shared caches and overrides `max-age` in those caches (RFC 9111 §5.2.2.10). Because both values match, adding `max-age` does not change CDN behavior. A browser does not use `s-maxage`. Because ocache sets `last-modified` when it fills the entry, heuristic freshness (§4.2.2) is close to zero. Previously, private caches revalidated on every navigation while the server kept the entry for the full `maxAge`. They now receive the same freshness lifetime that ocache enforces. This is the only lifetime that ocache can promise.
40
+
-**`max-age` uses the same value as `s-maxage`.**`s-maxage` applies only to shared caches and overrides `max-age` in those caches (RFC 9111 §5.2.2.10). Because both values match, adding `max-age` does not change CDN behavior. A browser does not use `s-maxage`. Previously, a private cache received no freshness lifetime at all and revalidated on every navigation while the server kept the entry for the full `maxAge`. They now receive the same freshness lifetime that ocache enforces. This is the only lifetime that ocache can promise.
41
41
-**ocache sends `stale-while-revalidate` only with a value.** The directive requires a number of seconds (RFC 5861 §3). The [ISR pattern](/guide/isr), which uses `swr: true` without `staleMaxAge`, has an unlimited stale window. No number can describe it correctly, so ocache does not advertise the directive. A downstream cache revalidates when `max-age` ends. ocache responds from its stale copy while it refreshes in the background. ISR behavior stays the same, but the downstream cache does not manage it. Before v1, ocache sent `stale-while-revalidate` without a value. Compliant caches must ignore that form, so it did not advertise the window.
42
42
-**[`getMaxAge`](/guide/api) controls each field.** A per-entry `maxAge` or `staleMaxAge` overrides the static option in the header. It also overrides the option in the freshness check and storage TTL. If the hook omits a field, ocache uses the static option for that field. For example, a 2-second window from the hook is advertised as `max-age=2`, even with `{ maxAge: 3600 }`. The downstream lifetime always matches the lifetime that ocache enforces.
43
43
@@ -133,7 +133,7 @@ Internally, ocache uses `staleMaxAge: 0` for that entry. [`getMaxAge`](/guide/ap
133
133
134
134
Use this option when ocache should store a response without adding a downstream freshness lifetime. This does not define a complete downstream storage policy.
135
135
136
-
Set `sendCacheControl: false` to suppress the synthesized header. ocache still stores and serves the response. SWR, `etag`, and `last-modified` still apply:
136
+
Set `sendCacheControl: false` to suppress the synthesized header. ocache still stores and serves the response. SWRand `etag` still apply:
0 commit comments