Home  >  Article  >  Web Front-end  >  html settings cache

html settings cache

PHPz
PHPzOriginal
2023-05-15 18:18:382507browse

With the development of web applications, more and more data and files are included in the page. For some reused data and files, the browser's caching mechanism can effectively reduce network requests and increase the speed at which users access pages. Setting cache in HTML is a simple and effective way. Let's introduce how to set cache in HTML.

1. Why set up cache?

When browsing the web, each page load will trigger a network request, including requests for HTML, CSS, JavaScript and other files as well as pictures, videos and other resources. The time required for network requests is often the bottleneck of page loading speed, so reducing the number of requests is crucial to improving page loading speed.

At this time, the browser’s caching mechanism needs to come into play. When a user requests a page for the first time, the browser will cache the resources in the page (such as CSS, JavaScript and other files). When the user visits the page again, the browser can obtain these resources directly from the cache without having to initiate a new request. This not only reduces the load on the server, but also greatly improves user access speed.

2. How to set cache in HTML?

In HTML, you can use HTTP headers to control how caching is used. Commonly used HTTP headers include Expires and Cache-Control.

  1. Expires

Expires specifies a date or time before which the browser will consider the resource valid. If the resource is accessed again after this date or time, the browser will re-request the resource.

On the server side, browser caching can be achieved by setting Expires in Response Headers. For example, set Expires to 30 days from now:

Expires: Fri, 16 Jul 2021 20:00:00 GMT
  1. Cache-Control

Cache-Control is one of the more modern HTTP headers that control caching. Through Cache-Control, we can finely control the cache strategy.

Commonly used Cache-Control attributes are:

  • public: the cache can be cached by all users (including proxy servers);
  • private: the cache can only be cached by the terminal User cache, intermediate proxy server cannot cache;
  • max-age: Set the cache expiration time in seconds.

For example, set Cache-Control to public and max-age to the time after one week:

Cache-Control: public, max-age=604800
  1. ETag

ETag is Another mechanism to control caching, it is a unique identifier generated based on the response content. When the browser requests a resource, the server will return the ETag value of the resource to the browser. The next time the resource is requested, the browser sends the previous ETag value to the server through If-None-Match to ask whether the resource has changed.

If the resource has not changed, the server will return a 304 Not Modified response and include the ETag in the Response Headers; if the resource has changed, the server will return the new resource and update the ETag.

On the server side, browser caching can be achieved by setting ETag in Response Headers. For example:

ETag: "1234"

3. Precautions for setting up cache

While using cache, you also need to pay attention to the following points.

  1. Avoid caching private information

When the cache sets the public attribute, the cached content can be accessed by all users, including the browser's cache and the proxy server's cache. Therefore, it is necessary to avoid caching private information (such as user passwords, etc.).

  1. When updating the cache, force refresh should be considered

When the page content changes, the user's browser cache needs to be refreshed. At this time, you can use the max-age attribute of Cache-Control to set the cache validity time in seconds.

For example, setting Cache-Control to max-age=0 can force the browser to re-request resources and update the cache:

Cache-Control: max-age=0
  1. Different resources should have different caching strategies set

For most static resources (such as images, CSS, JS, etc.), max-age can be set to a longer time to improve operating efficiency. For some dynamic resources that change frequently, mechanisms such as ETag should be used to ensure that the resource is updated every time it is requested.

Summary:

Setting cache in HTML is one of the common methods to improve page performance. By setting HTTP headers such as Expires, Cache-Control, and ETag, you can implement the browser's caching mechanism and improve user access speed. When setting up a cache, you need to pay attention to the detailed settings of the cache and the different caching strategies for various resources to achieve the best results.

The above is the detailed content of html settings cache. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:html button jumpNext article:html button jump