Bug 1880528 - Introduce preference for fetch priority adjustment of scripts. r=valentin,necko-reviewers

This introduces preferences for non-link scripts. See D201997 for the
rationale, test coverage and why there is no behavior change.

Differential Revision: https://phabricator.services.mozilla.com/D202046
This commit is contained in:
Frédéric Wang 2024-02-19 13:35:39 +00:00
parent a4d4b33917
commit 4b0b8b1ca2
3 changed files with 103 additions and 37 deletions

View File

@ -711,50 +711,40 @@ void AdjustPriorityForNonLinkPreloadScripts(nsIChannel* aChannel,
if (nsCOMPtr<nsISupportsPriority> supportsPriority =
do_QueryInterface(aChannel)) {
LOG(("Is not <link rel=[module]preload"));
const RequestPriority fetchPriority = aRequest->FetchPriority();
const auto fetchPriority = ToFetchPriority(aRequest->FetchPriority());
// The spec defines the priority to be set in an implementation defined
// manner (<https://fetch.spec.whatwg.org/#concept-fetch>, step 15 and
// <https://html.spec.whatwg.org/#concept-script-fetch-options-fetch-priority>).
// <testing/web-platform/mozilla/tests/fetch/fetchpriority/support/script-tests-data.js>
// provides more context for the priority mapping.
const int32_t supportsPriorityValue = [&]() {
switch (fetchPriority) {
case RequestPriority::Auto: {
if (aRequest->IsModuleRequest()) {
return nsISupportsPriority::PRIORITY_HIGH;
}
const ScriptLoadContext* scriptLoadContext =
aRequest->GetScriptLoadContext();
if (scriptLoadContext->IsAsyncScript() ||
scriptLoadContext->IsDeferredScript()) {
return nsISupportsPriority::PRIORITY_LOW;
}
if (scriptLoadContext->mScriptFromHead) {
return nsISupportsPriority::PRIORITY_HIGH;
}
return nsISupportsPriority::PRIORITY_NORMAL;
}
case RequestPriority::Low: {
return nsISupportsPriority::PRIORITY_LOW;
}
case RequestPriority::High: {
return nsISupportsPriority::PRIORITY_HIGH;
}
default: {
MOZ_ASSERT_UNREACHABLE();
return nsISupportsPriority::PRIORITY_NORMAL;
}
// See corresponding preferences in StaticPrefList.yaml for more context.
const int32_t supportsPriorityDelta = [&]() {
const ScriptLoadContext* scriptLoadContext =
aRequest->GetScriptLoadContext();
if (aRequest->IsModuleRequest()) {
return FETCH_PRIORITY_ADJUSTMENT_FOR(module_script, fetchPriority);
}
if (scriptLoadContext->IsAsyncScript() ||
scriptLoadContext->IsDeferredScript()) {
return FETCH_PRIORITY_ADJUSTMENT_FOR(async_or_defer_script,
fetchPriority);
}
if (scriptLoadContext->mScriptFromHead) {
return FETCH_PRIORITY_ADJUSTMENT_FOR(script_in_head, fetchPriority);
}
return FETCH_PRIORITY_ADJUSTMENT_FOR(other_script, fetchPriority);
}();
if (supportsPriorityValue) {
LogPriorityMapping(ScriptLoader::gScriptLoaderLog,
ToFetchPriority(fetchPriority), supportsPriorityValue);
supportsPriority->SetPriority(supportsPriorityValue);
if (supportsPriorityDelta) {
supportsPriority->AdjustPriority(supportsPriorityDelta);
#ifdef DEBUG
int32_t adjustedPriority;
supportsPriority->GetPriority(&adjustedPriority);
LogPriorityMapping(ScriptLoader::gScriptLoaderLog, fetchPriority,
adjustedPriority);
#endif
}
}
}

View File

@ -11783,6 +11783,78 @@
value: 10
mirror: always
# Adjustments to apply to the internal priority of <script type="module"
# fetchpriority=low/high/auto> with respect to the case when
# network.fetchpriority is disabled.
# - When the flag is disabled, Gecko currently sets priority to NORMAL.
# - When the flag is enabled, it respectively maps to LOW/HIGH/HIGH.
- name: network.fetchpriority.adjustments.module-script.low
type: int32_t
value: 10
mirror: always
- name: network.fetchpriority.adjustments.module-script.high
type: int32_t
value: -10
mirror: always
- name: network.fetchpriority.adjustments.module-script.auto
type: int32_t
value: -10
mirror: always
# Adjustments to apply to the internal priority of async or defer <script
# fetchpriority=low/high/auto> with respect to the case when
# network.fetchpriority is disabled.
# - When the flag is disabled, Gecko currently sets priority to NORMAL.
# - When the flag is enabled, it respectively maps to LOW/HIGH/LOW.
- name: network.fetchpriority.adjustments.async-or-defer-script.low
type: int32_t
value: 10
mirror: always
- name: network.fetchpriority.adjustments.async-or-defer-script.high
type: int32_t
value: -10
mirror: always
- name: network.fetchpriority.adjustments.async-or-defer-script.auto
type: int32_t
value: 10
mirror: always
# Adjustments to apply to the internal priority of <script
# fetchpriority=low/high/auto> inside the <head>, with respect to the case when
# network.fetchpriority is disabled.
# - When the flag is disabled, Gecko currently sets priority to NORMAL.
# - When the flag is enabled, it respectively maps to LOW/HIGH/HIGH.
- name: network.fetchpriority.adjustments.script-in-head.low
type: int32_t
value: 10
mirror: always
- name: network.fetchpriority.adjustments.script-in-head.high
type: int32_t
value: -10
mirror: always
- name: network.fetchpriority.adjustments.script-in-head.auto
type: int32_t
value: -10
mirror: always
# Adjustments to apply to the internal priority of <script
# fetchpriority=low/high/auto> (other than the scripts handled above) with
# respect to the case when network.fetchpriority is disabled.
# - When the flag is disabled, Gecko currently sets priority to NORMAL.
# - When the flag is enabled, it respectively maps to LOW/HIGH/NORMAL.
- name: network.fetchpriority.adjustments.other-script.low
type: int32_t
value: 10
mirror: always
- name: network.fetchpriority.adjustments.other-script.high
type: int32_t
value: -10
mirror: always
- name: network.fetchpriority.adjustments.other-script.auto
type: int32_t
value: 0
mirror: always
# Enables `<link rel="preconnect">` tag and `Link: rel=preconnect` response header
# handling.
- name: network.preconnect

View File

@ -8,6 +8,10 @@
const fetchpriorities = ["auto", "low", "high"];
const prioritiesWhenFetchpriorityDisabled = {
"link-preload-script": SpecialPowers.Ci.nsISupportsPriority.PRIORITY_HIGHEST,
"module-script": SpecialPowers.Ci.nsISupportsPriority.PRIORITY_NORMAL,
"async-or-defer-script": SpecialPowers.Ci.nsISupportsPriority.PRIORITY_NORMAL,
"script-in-head": SpecialPowers.Ci.nsISupportsPriority.PRIORITY_NORMAL,
"other-script": SpecialPowers.Ci.nsISupportsPriority.PRIORITY_NORMAL,
};
for (const name in prioritiesWhenFetchpriorityDisabled) {
let adjustments = {};