JuneDeng


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

HTTP-Caching

发表于 2017-12-13 | 分类于 技术

Validating cached responses with ETags

  • The server uses the ETag HTTP header to communicate a validation token.
  • The validation token enables efficient resource update checks:no data transferred if the resource has not changed.
    Ensure that the server is providing the necessary ETag tokens. Check your server documentation for the necessary configuration flags.

Cache-Control

  • Each resource can define its caching policy via the Cache-Control HTTP header.
  • Cache-Control directives control who can cache the response, under which conditions, and for how long.

“no-cache” and “no-store”

“no-cache” indicates that the returned response can’t be used to satisfy a subsequent request to the same URL without first checking with the server if the response has changed. As a result, if a proper validation token (ETag) is present, no-cache incurs a roundtrip to validate the cached response, but can eliminate the download if the resource has not changed.

By contrast, “no-store” is much simpler. It simply disallows the browser and all intermediate caches from storing any version of the returned response—for example, one containing private personal or banking data. Every time the user requests this asset, a request is sent to the server and a full response is downloaded.

“public” vs. “private”

If the response is marked as “public”, then it can be cached, even if it has HTTP authentication associated with it, and even when the response status code isn’t normally cacheable. Most of the time, “public” isn’t necessary, because explicit caching information (like “max-age”) indicates that the response is cacheable anyway.

By contrast, the browser can cache “private” responses. However, these responses are typically intended for a single user, so an intermediate cache is not allowed to cache them. For example, a user’s browser can cache an HTML page with private user information, but a CDN can’t cache the page.

“max-age”

This directive specifies the maximum time in seconds that the fetched response is allowed to be reused from the time of the request. For example, “max-age=60” indicates that the response can be cached and reused for the next 60 seconds.

Define optimal Cache-Control policy

https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/images/http-cache-decision-tree.png

Invalidating and updating cached responses

  • Locally cached responses are used until the resource “expires.”
  • Embedding a file content fingerprint in the URL enables you force the client to update a new version of the response.
  • Each application needs to define its own cache hierarchy for optimal performance.

All HTTP requests that the browser makes are first routed to the browser cache to check whether there is a valid cached response that can be used to fulfill the request. If there’s a match, the response is read from the cache, which eliminates both the network latency and the data costs that the transfer incurs.

How do you get the best of both worlds: client-side caching and quick updates? You change the URL of the resource and force the user to download the new response whenever its content changes. Typically, you do this by embedding a fingerprint of the file, or a version number, in its filename—for example, style.x234dff.css.

Caching checklist

  • Use consistent URLs:if you serve the same content on different URLs, then that content will be fetched and stored multiple times.
  • Ensure that the server provides a validation token (ETag): validation tokens eliminate the need to transfer the same bytes when a resource has not changed on the server.
  • Identify which resources can be cached by intermediaries: those with responses that are identical for all users are great candidates to be cached by a CDN and other intermediaries.
  • Determine the optimal cache lifetime for each resource:different resources may have different freshness requirements. Audit and determine the appropriate max-age for each one.
  • Determine the best cache hierarchy for your site:the combination of resource URLs with content fingerprints and short or no-cache lifetimes for HTML documents allows you to control how quickly the client picks up updates.
  • Minimize churn: some resources are updated more frequently than others. If there is a particular part of a resource (for example, a JavaScript function or a set of CSS styles) that is often updated, consider delivering that code as a separate file. Doing so allows the remainder of the content (for example, library code that doesn’t change very often), to be fetched from cache and minimizes the amount of downloaded content whenever an update is fetched.

web-font-optimization

发表于 2017-12-12 | 分类于 技术

Anatomy of a webfont

  • Unicode fonts can contain thousands of glyphs.
  • There are four font formats:WOFF2,WOFF,EOT,and TTF.
  • Some font formates require the use of compression.

Webfont formats:

  • EOT:IE only
  • TTF:partial IE support
  • WOFF:widest support but not available in some older browsers
  • WOFF2:in progress for many browsers

There is no single format that works in all browsers,which means that we need to deliver multiple fomats to provide consistent experience:

  • Serve WOFF 2.0 variant to browsers that support it.
  • Serve WOFF variant to the majority of browsers.
  • Serve TTF variant to old Android (below 4.4) browsers.
  • Serve EOT variant to old IE (below IE9) browsers.

Reducing font size with compression

A font is a collection of glyphs, each of which is a set of paths describing the letter form. The individual glyphs are different, but they contain a lot of similar information that can be compressed with GZIP or a compatible compressor:

  • EOT and TTF formats are not compressed by default. Ensure that your servers are configured to apply GZIP compression when delivering these formats.
  • WOFF has built-in compression. Ensure that your WOFF compressor is using optimal compression settings.
  • WOFF2 uses custom preprocessing and compression algorithms to deliver ~30% file-size reduction over other formats.

Note: Consider using Zopfli compression for the EOT, TTF, and WOFF formats. Zopfli is a zlib compatible compressor that delivers ~5% file-size reduction over gzip.

Define a font family with @font-face

  • Use the format() hint to specify multiple font formats.
  • Subset large Unicode fonts to improve performance. Use Unicode-range subsetting and provide a manual subsetting fallback for older browsers.
  • Reduce the number of stylistic font variants to improve the page- and text-rendering performance.
  1. The browser requests the HTML document.
  2. The browser begins parsing the HTML response and constructing the DOM.
  3. The browser discovers CSS, JS, and other resources and dispatches requests.
  4. The browser constructs the CSSOM after all of the CSS content is received and combines it with the DOM tree to construct the render tree.
    • Font requests are dispatched after the render tree indicates which font variants are needed to render the specified text on the page.
  5. The browser performs layout and paints content to the screen.
    • If the font is not yet available, the browser may not render any text pixels.
    • After the font is available, the browser paints the text pixels.

The “race” between the first paint of page content, which can be done shortly after the render tree is built, and the request for the font resource is what creates the “blank text problem” where the browser might render page layout but omits any text.

1…141516…32
June Deng

June Deng

make the world a better place

63 日志
2 分类
24 标签
GitHub
© 2018 June Deng
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.4