Http1.0 uses forced caching

  • Expires forces a cache in milliseconds. If the client and server time are inconsistent, a bug can occur that the foreground cannot detect
  // HTTP 1.0 forced caching, in milliseconds, if the client and server time is inconsistent, there will be a BUG
  res.setHeader("Expires".new Date(Date.now() + 10 * 1000).toUTCString())
Copy the code

Http1.1 uses forced caching and negotiated caching

  • Max-age enforces a cache, in seconds, that overwrites expires and uses a larger one between negotiated caches that the foreground can sense
      // HTTP 1.1 forced-cache protocol, which overwrites Expires in seconds with negotiated caches
      res.setHeader("Cache-Control"."max-age=5")
    Copy the code
  • The expiration time of the last-Modified negotiated cache is subject to the expiration time set by the backend, which cannot be sensed by the foreground
    // HTTP 1.1 negotiation cache - final file modification time
    res.setHeader("Cache-Control"."no-cache")
    res.setHeader("last-modified".new Date().toUTCString())
    if (new Date(req.headers['if-modified-since']).getTime() + 3000 > Date.now()) {
      res.statusCode = 304
      res.end()
      return
    }
  ` `The '* Etag negotiation cache is based on the final modification time of the file, which is not sensed by the foreground` `javascript
    HTTP 1.1 Negotiation Cache - File Content Overview Hash comparison
    res.setHeader("Cache-Control"."no-cache")
    const crypto = require("crypto")
    const hash = crypto.createHash("sha1").update(content).digest("hex");
    res.setHeader("Etag", hash)
    if (req.headers["if-none-match"] === hash) {
      res.statusCode = 304
      res.end()
      return
    }
  ` ``
Copy the code