The preload in the response header does not always work, which types of relationship are reliable in the HTTP header?
Resource prefetching should be written into the page tag or the HTTP response header, the answer to which relationships are reliable and effective is not the same. The semantic difference between the two layers is often confused. The response header is parsed before the main content of the document is delivered, the timing is earlier, but the types of relationships it supports are fewer. According to MDN's description of the Link header, in practice, most rel link types put into the HTTP header do not take effect, and the truly reliable ones are the preconnection and preload types. To determine which hints to give to the response header, it is necessary to first clarify the differences between the two layers.
The division of labor between the Link header and the page tag is different.
The link tag within the page is read by the HTML parser, in addition to pre-fetching hints, it can also handle style sheet associations, icon declarations, and other functions;The Link response header only specifies link relationships at the document level and does not take over the behavior of these tags themselves. syntactically, they are also different: each link value in the response header must enclose the URI in angle brackets, followed by rel and other parameters, and non-ASCII characters in the URI must be percent-encoded first; otherwise, the entire header field may be ignored directly.The benefit is that it is injected by the server or edge node, without changing the HTML template, and is effective consistently for all requests to the same document.
Most rel added to the response header does not take effect
Pre-fetch class, module preloading class, style sheet class, these common relationships in the tag, often cannot get the expected processing after being added to the response header. The table is layered by reliability.
| Rel type | Is it reliable to put it in the response header | Description |
|---|---|---|
| Preconnect | Reliable | Complete domain resolution and connection establishment in advance, directly benefiting cross-origin resources |
| Preload | Reliable | Preferably initiate the request for specified resources, and specify the as and cross-origin attributes |
| Prefetch | unreliable | speculative download for the next navigation, the header form is often ignored |
| modulepreload | unreliable | semantic dependency module graph, usually only valid when declared within the document |
| stylesheet | Not applicable | only the tag form takes on the responsibility of style application, the header declaration does not equal to take effect |
| dns-prefetch | unreliable | covered by preconnect, the header form lacks stable support |
only combined with 103 Early Hints can have an earlier window
The value of the Link header lies in its timeliness, but the extent of its timeliness depends on when it is sent out. If the server can issue the 103 Early Hints status code before the response is fully assembled and include the preconnect and preload Link headers in this batch of information, the browser can start establishing connections and fetching resources during the time it is waiting for the body, and some requests will already be completed by the time the formal response arrives. This level of cooperation requires the support of both the web server and the upstream program, and if any hop drops it, the effect degrades to a normal response header.
What should be taken in advance on the homepage of the content station and how to check it
Resources that are worth putting in the response header have two characteristics: cross-origin and not on the initial HTML reference chain. Font files used on the first screen, critical styles injected dynamically by scripts, and avatars and album entry points from independent static domains are typical objects;The images and main stylesheet already written in the page, the browser will find it out itself, redundant prefetching will only increase unnecessary requests. Which layer should provide the resources depends on the production method of static files: the path is determined during the build phase, it is easier to maintain and audit if written into the template tags.The path is determined at runtime based on the visitor, language, or compression strategy, which is more convenient to inject into the response header and also avoids conditional branches in the template.
The method of verification should fall on observable evidence. First, capture a real response and confirm whether the URI in the header field contains angle brackets, whether it is completed with percentage encoding, and whether 'as' and 'crossorigin' are present; then, look at the reason for initiating the request in the browser's network panel, and only when there are marks related to preloading or early hints does it mean that this prompt has been truly consumed. Finally, do a round of comparison, and test the three arrangements of writing tags, writing response headers, and combining both several times on the same page, taking the distribution instead of single-point values to avoid taking sporadic cache hits as optimization effects.
Common questions
Ask: Why is the prefetch in the response header not working, and what usually goes wrong?
First, check three points: whether the relationship type is within a reliable range, whether the URI is enclosed in angle brackets and encoded with percent signs, and whether the cross-origin resource is supplemented with 'as' and 'crossorigin'. If any of these are missing, the browser may ignore the entire item.
Ask: Since the response header is stronger, should all the prefetches in the tag be moved over?
It is not recommended to take a one-size-fits-all approach. Relationships that depend on the semantics of the tag, such as stylesheet associations and icon declarations, should stay on the page. The response header should only handle preconnection and preloading, with each layer performing its duties more stably.
Ask: Does the Link header still have meaning if 103 Early Hints is not used?
Yes, but the window of opportunity has become smaller. The hints can only be read after the text arrives or the first block of the response, and the time saved mainly falls on the connection establishment stage, and the improvement for cross-origin resources is still visible.
Ask: How can you determine whether a hint is worth keeping?
Answer: Conduct a separate closed-loop test for it, observe whether the start time of the resource and the first screen rendering metrics degrade. Hints that do not show any distinguishable changes after being turned off are redundant and should be cleaned up.