# Firefox Resource Tailing Documentation ## Overview Resource tailing is a network optimization mechanism in Firefox that delays the loading of certain low-priority resources (typically third-party tracking resources) until higher-priority resources have finished loading. This helps improve page load performance by prioritizing resources that are critical for rendering and user interaction. For information about how resource tailing integrates with Firefox's overall network scheduling and prioritization system, see [Firefox Network Scheduling and Prioritization](prioritization.md). ## How Resource Tailing Works ### The Mechanism 1. **Request Context**: Each page load has an associated [nsIRequestContext](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/base/nsIRequestContext.idl#41) that tracks the state of all network requests belonging to that context. 2. **Tailed vs Non-Tailed Requests**: - Non-tailed requests are regular resources that load immediately - Tailed requests are delayed resources that are queued until certain conditions are met - The request context maintains a count of active non-tailed requests via [AddNonTailRequest()](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/base/nsIRequestContext.idl#82) and [RemoveNonTailRequest()](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/base/nsIRequestContext.idl#83) 3. **Tail Blocking Logic** (see [RequestContext::IsContextTailBlocked()](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/base/RequestContextService.cpp#334): - When a channel marked as "tailable" attempts to open, it calls [IsContextTailBlocked()](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/base/RequestContextService.cpp#334) - If the context is in tail-blocked state (has active non-tailed requests), the channel is queued - The channel implements [nsIRequestTailUnblockCallback](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/base/nsIRequestContext.idl#19) and its [OnTailUnblock()](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/base/nsIRequestContext.idl#29) method is called when unblocked 4. **Unblocking Conditions**: - **Time-based**: A timer schedules when to unblock tailed requests. The delay is calculated as: - `delay = quantum * mNonTailRequests` (with maximum limits) - Default quantum decreases after DOMContentLoaded - The delay decreases progressively with time since page load began - **Load completion**: When non-tailed request count drops to zero after DOMContentLoaded - **Forced unblocking**: When the request context is canceled (e.g., navigation away) 5. **Implementation Flow** ``` AsyncOpen() or Connect() → WaitingForTailUnblock() checks if channel should be tailed → If yes: queued via IsContextTailBlocked() → mOnTailUnblock callback is set → Later: OnTailUnblock() is called → Executes the saved callback to continue operation ``` ## Eligibility for Tailing A Resource is Eligible for Tailing When: 1. Has the [nsIClassOfService::Tail](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/base/nsIClassOfService.idl#112) flag set 2. Does NOT have any of these flags: - `UrgentStart` - User-initiated or high-priority requests - `Leader` - Blocking resources in document head - `TailForbidden` - Explicitly forbidden from tailing 3. If marked `Unblocked`, must also have `TailAllowed` flag 4. Is NOT a navigation request ([IsNavigation()](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/protocol/http/HttpBaseChannel.cpp#4391) returns false) ## Resources That Are Tailed ### Primary Case: Third-Party Tracking Resources - **When**: A resource is classified as a third-party tracker (see [nsHttpChannel::Connect()](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/protocol/http/nsHttpChannel.cpp#1436)) - **Detected**: via [IsThirdPartyTrackingResource()](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/protocol/http/HttpBaseChannel.cpp#1831) using URL classifier - **Flag added**: [nsIClassOfService::Tail](https://searchfox.org/firefox-main/rev/39bc83bb8632d54d70542dc5d98c046a317ec99d/netwerk/base/nsIClassOfService.idl#112) ### Secondary Case: Async Scripts with TailAllowed - **Resource**: Async `