Optimizing Page Navigation and Resource Loading with Resource Hints
Description
Resource Hints are a set of browser directives declared via <link> tags or HTTP headers, used to guide the browser in advance on handling page navigation, resource loading, and other behaviors, thereby reducing user-perceived latency. Common resource hints include dns-prefetch, preconnect, prefetch, and prerender, and appropriate strategies should be selected based on the scenario.
Optimization Goals and Principles
- Predict User Behavior: By analyzing actions a user might take (such as clicking a link or loading the next page), perform DNS resolution, establish connections, or fetch resources in advance.
- Reduce Critical Path Latency: Complete non-critical tasks ahead of time to avoid blocking critical rendering resources.
- Balance Network and Computational Costs: Avoid excessive preloading that leads to bandwidth waste; intelligent triggering should be combined with data logging or heuristic rules.
Step-by-Step Implementation Strategy
-
Identify Applicable Scenarios
- Use
dns-prefetchorpreconnectfor cross-origin domains, such as third-party CDNs or API domains. - Use
prefetchfor next-page resources with a high probability of access (e.g., the next product on an e-commerce details page). - Use
prerenderfor pages the user is explicitly likely to visit (e.g., the top result on a search results page).
- Use
-
Configure DNS Prefetching (dns-prefetch)
<!-- Pre-resolve DNS for third-party domains to reduce DNS lookup time for subsequent requests --> <link rel="dns-prefetch" href="https://cdn.example.com">Note: Browsers perform DNS prefetching for links on the current page by default; explicit declaration is required for cross-origin resources.
-
Establish Preconnection (preconnect)
<!-- Complete DNS resolution, TCP handshake, and TLS negotiation in advance; suitable for critical cross-origin resources --> <link rel="preconnect" href="https://api.example.com" crossorigin>Applicable Scenarios: Known upcoming third-party resource requests (e.g., fonts, critical APIs). More efficient than
dns-prefetchbut consumes more resources. -
Prefetch Resources (prefetch)
<!-- Load resources needed for the next page in advance and cache them to disk --> <link rel="prefetch" href="/next-page.html" as="document"> <link rel="prefetch" href="/static/next-page.js" as="script">Rules:
- Use the
asattribute to specify the resource type (e.g.,script,style), helping the browser set priority and caching policies. - Only apply to high-probability access scenarios to avoid bandwidth waste.
- Use the
-
Prerender Pages (prerender)
<!-- Fully render the entire page in the background; switch directly when the user visits --> <link rel="prerender" href="https://example.com/next-page">Risk Control:
- May trigger extensive computation and network requests; strictly limit to extremely high-probability scenarios (e.g., the next step for a paid user).
- Deprecated in some browsers; consider using
prefetch+Speculative Rules API(experimental feature) instead.
-
Dynamic Injection via Priority Rules
// Dynamically add resource hints based on user behavior document.getElementById('button').addEventListener('mouseover', () => { const link = document.createElement('link'); link.rel = 'prefetch'; link.href = '/anticipated-resource.jpg'; document.head.appendChild(link); });Optimization Points:
- Trigger based on events like mouse hover, scroll position, etc., to improve prediction accuracy.
- Use
Priority Hints(e.g.,fetchpriority="high") to further control loading priority.
Verification and Monitoring
- Use the Chrome DevTools Network panel to check if resources are loaded in advance and if their priority is
Lowest(prefetch) orHighest(preconnect). - Use the Performance panel to compare navigation times before and after optimization, focusing on the reduction in
DOMContentLoadedandLoadevent times. - Use the
Navigation Timing APIfor logging to measure real-user performance improvements.
Precautions
- Preloaded resources may compete for bandwidth; dynamically enable them after checking network conditions via
navigator.connection.effectiveType. - Use
prerendercautiously on mobile to avoid draining battery and consuming data. - When using
HTTP/2+, multiple small resources can be consolidated under a single domain preconnection to reduce handshake overhead.