- Aug 2023
-
datatracker.ietf.org datatracker.ietf.org
Tags
- http:header=cache-control
- urn:ietf:rfc:9111
- caching
- http:header=cache-control:max-stale
- http:header=cache-control:s-maxage
- http:header=cache-control:only-if-cached
- http:header=cache-control:must-understand
- http:code=304
- http:header=age
- http:header=cache-control:public
- http:header=cache-control:max-age
- http:header=if-modified-since
- http:header=cache-control:no-cache
- http:header=if-unmodified-since
- http:header=cache-control:private
- http:header=if-range
- http:code=206
- http:header=cache-control:min-fresh
- http:header=cache-control:no-transform
- http:header=cache-control:proxy-revalidate
- http:header=pragma
- http:header=cache-control:must-revalidate
- http:header=cache-control:no-store
- http:header=expires
- wikipedia:en=HTTP_caching
- http:header=if-match
- http:header=warning
- http:header=if-none-match
- http
Annotators
URL
-
- May 2023
-
www.bortzmeyer.org www.bortzmeyer.org
-
datatracker.ietf.org datatracker.ietf.org
-
stackoverflow.com stackoverflow.com
- Mar 2023
-
sergiodxa.com sergiodxa.com
-
Send the 304 Not Modified response
```js import etag from "etag"; import { renderToString } from "react-dom/server"; import type { EntryContext, HandleDataRequestFunction } from "remix"; import { RemixServer } from "remix";
export default function handleRequest( request: Request, status: number, headers: Headers, remixContext: EntryContext ) { let markup = renderToString( <RemixServer context={remixContext} url={request.url} /> );
headers.set("Content-Type", "text/html"); headers.set("ETag", etag(markup));
// check if the
If-None-Match
header matches the ETag if (request.headers.get("If-None-Match") === headers.get("ETag")) { // and send an empty Response with status 304 and the headers. return new Response("", { status: 304, headers }); }return new Response("<!DOCTYPE html>" + markup, { status, headers }); }
export let handleDataRequest: HandleDataRequestFunction = async ( response: Response, { request } ) => { let body = await response.text();
if (request.method.toLowerCase() === "get") { response.headers.set("etag", etag(body)); // As with document requests, check the
If-None-Match
header // and compare it with the Etag, if they match, send the empty 304 Response if (request.headers.get("If-None-Match") === response.headers.get("ETag")) { return new Response("", { status: 304, headers: response.headers }); } }return response; }; ```
-
-
developer.mozilla.org developer.mozilla.orgETag1
- Jun 2017
-
developer.mozilla.org developer.mozilla.orgHTTP 缓存1
-
在过期时间前,资源缓存是有效的,反之则缓存失效。通过不停抛弃过期的缓存资源以保证资源的实时性。注意,旧的缓存不会被抛弃或者忽略;当发起一个针对旧缓存资源的请求时,会在请求头里带上If-None-Match用来判断缓存是否还有效。如果有效,服务端返回304(Not Modified)和空的body以节省一部分带宽。
为何要有时效性:
- 空间有限
- 服务器可能有更新文件
在缓存有效期过期前,不会去询问服务器资源是否有效..如果缓存过期,就会在请求头上带上If-None-Match来判断缓存是否依旧有效
-