Key Facts
- ✓ The technique uses <link rel="preload"> combined with the media attribute.
- ✓ It allows for conditional resource loading without JavaScript.
- ✓ The method was discussed on Hacker News, receiving 75 points.
- ✓ The original article was published on January 11, 2026.
Quick Summary
A technical exploration introduces a method for conditional lazy loading using standard HTML attributes. The technique combines the preload link type with media queries to control resource fetching based on viewport characteristics.
By leveraging the browser's native handling of `preload` and `media`, developers can achieve resource loading logic without JavaScript dependencies. This approach specifically targets scenarios where resources should only be loaded when certain conditions, such as screen width, are met.
The discussion surrounding this method focuses on its potential to optimize performance for responsive designs. It provides a mechanism to defer the loading of heavy assets until they are actually needed by the user's device.
The Core Mechanism
The proposed solution utilizes the HTML `` element with specific attributes to achieve conditional fetching. The key components are the `rel="preload"` attribute, which instructs the browser to fetch the resource early, and the `media` attribute, which applies a media query to the preload operation.
When a browser encounters a link tag like ``, it evaluates the media query. If the query matches the current viewport, the browser proceeds to download the resource. If the query does not match, the resource is not fetched.
This behavior differs from standard `` tags with `srcset` or `
Implementation Details
To implement this technique, specific attribute combinations are required. The `as` attribute must match the type of resource being loaded (e.g., `image`, `style`, `script`) to ensure proper CSP (Content Security Policy) checks and resource prioritization.
Example implementation for a background image:
- ``
- ``
The browser handles the logic internally. If the user resizes the window, the browser does not automatically unload or load resources based on the `media` attribute changes on a `preload` link; the attribute is typically evaluated only during the initial parsing of the document. Therefore, this method is best suited for initial viewport determination rather than dynamic resizing scenarios.
Performance Implications
The primary benefit of this approach is the reduction of bandwidth usage on devices that do not require high-resolution assets. By preventing the download of large images on mobile devices via a pure HTML mechanism, page load times can be significantly improved.
However, there are nuances to consider. The `preload` scanner hint might cause the browser to fetch the resource with higher priority than a standard lazy-loaded image. Developers must ensure that the `media` query accurately reflects the usage of the resource.
Compared to JavaScript-based lazy loading libraries, this method reduces the JavaScript execution overhead. It relies entirely on the browser's resource fetching engine, which is generally more efficient and predictable.
Browser Compatibility & Discussion
The technique relies on support for `rel="preload"` and the `media` attribute on `` elements. Most modern browsers support these features, but testing across different environments is recommended. The discussion on Hacker News (Y Combinator) brought attention to this specific combination of attributes for conditional loading.
Users on the platform explored the edge cases, such as how browsers handle conflicting media queries or the interaction with the preload cache. The consensus was that while the technique is valid HTML, its behavior might vary slightly between browser engines regarding resource prioritization and garbage collection of unused preloads.




