fix(performance): minimize resource timing content types

This commit is contained in:
ldm0
2026-11-02 06:02:26 +08:00
parent f8c2607c5e
commit 350831da29
7 changed files with 248 additions and 28 deletions
@@ -261,6 +261,44 @@ fn record_performance_load_event_for_window(
let scope = &mut v8::ContextScope::new(scope, relevant_context);
record(scope);
}
fn resource_timing_content_type(headers: &[(String, Vec<u8>)]) -> String {
let Some(essence) = moli_web_mime::extract_response_mime_essence(headers) else {
return String::new();
};
// Fetch stores a minimized MIME type in response body info. Classify the
// extracted MIME record, not the first header or a sniffed body type.
// https://mimesniff.spec.whatwg.org/#minimize-a-supported-mime-type
if essence == "image/svg+xml" {
// SVG retains its own type instead of joining the generic XML group.
return essence;
}
if moli_web_mime::is_javascript_mime_essence(&essence) {
"text/javascript".to_owned()
} else if matches!(essence.as_str(), "application/json" | "text/json")
|| essence.ends_with("+json")
{
"application/json".to_owned()
} else if matches!(essence.as_str(), "application/xml" | "text/xml")
|| essence.ends_with("+xml")
{
"application/xml".to_owned()
} else if moli_web_mime::is_text_mime_essence(&essence)
|| moli_image::supports_image_mime_essence(&essence)
|| moli_web_mime::media_mime_support(&essence)
!= moli_web_mime::MediaMimeSupport::Unsupported
|| moli_web_mime::is_supported_font_mime_essence(&essence)
|| essence == "application/wasm"
{
// Text documents, decodable images, media, fonts and WebAssembly use
// the same support decisions as their consuming APIs. An arbitrary
// image/*, audio/*, video/* or font/* prefix does not imply support.
essence
} else {
String::new()
}
}
pub(crate) struct ResourcePerformanceEntry {
preload_state: moli_fetch::ResponsePreloadState,
name: String,
@@ -380,9 +418,7 @@ impl ResourcePerformanceEntry {
moli_fetch::ResponseCacheState::Local => 0.0,
moli_fetch::ResponseCacheState::Validated => 300.0,
};
let content_type = moli_web_mime::response_content_type(response_headers)
.and_then(|value| moli_web_mime::mime_essence(&value))
.unwrap_or_default();
let content_type = resource_timing_content_type(response_headers);
Self {
preload_state,
name: name.into(),
@@ -586,6 +622,89 @@ pub(in crate::context_bootstrap) fn set_performance_slot_value<'s>(
mod tests {
use super::*;
#[test]
fn resource_content_type_extracts_the_last_valid_mime_before_minimizing() {
let cases: &[(&[&str], &str)] = &[
(&[], ""),
(&["", "invalid", "*/*"], ""),
(
&["APPLICATION/X-JAVASCRIPT; charset=UTF-8"],
"text/javascript",
),
(
&["text/plain", "application/problem+json"],
"application/json",
),
(
&["application/rss+xml, image/svg+xml; charset=UTF-8"],
"image/svg+xml",
),
(&["image/svg+xml", "text/xml"], "application/xml"),
(&["application/json", "*/*", "invalid"], "application/json"),
(&[r#"text/plain; x="a,application/json""#], "text/plain"),
(&[r#"text/plain; x="a"#, "application/json"], "text/plain"),
(&["application/json", "application/unknown"], ""),
];
for &(values, expected) in cases {
let headers = values
.iter()
.map(|value| ("cOnTeNt-TyPe".to_owned(), value.as_bytes().to_vec()))
.collect::<Vec<_>>();
let entry = ResourcePerformanceEntry::from_response_parts(
"https://example.test/resource",
"fetch",
None,
200,
&headers,
68.0,
moli_fetch::ResponseCacheState::None,
Default::default(),
);
assert_eq!(entry.content_type, expected, "{values:?}");
}
}
#[test]
fn resource_content_type_retains_only_supported_essences() {
for essence in [
"text/html",
"text/plain",
"text/css",
"text/vtt",
"image/png",
"image/jpeg",
"image/gif",
"image/webp",
"audio/mpeg",
"video/mp4",
"font/woff2",
"font/collection",
"application/font-woff",
"application/wasm",
] {
let headers = vec![(
"Content-Type".to_owned(),
format!("{essence}; x=private").into_bytes(),
)];
assert_eq!(resource_timing_content_type(&headers), essence);
}
for essence in [
"image/jpe",
"image/unknown",
"application/png",
"audio/unknown",
"video/unknown",
"font/unknown",
"application/vnd.ms-fontobject",
"application/octet-stream",
"application/zip",
"application/pdf",
] {
let headers = vec![("Content-Type".to_owned(), essence.as_bytes().to_vec())];
assert_eq!(resource_timing_content_type(&headers), "", "{essence}");
}
}
#[test]
fn failed_network_result_keeps_a_zero_sized_resource_timing_record() {
let result = Err("failed to fetch stylesheet".to_owned());
+1 -17
View File
@@ -53,23 +53,7 @@ impl LinkAsDestination {
match self {
Self::Script => moli_web_mime::is_javascript_mime_essence(&essence),
Self::Image => moli_image::supports_image_mime_essence(&essence),
// The font pipeline consumes SFNT (including collections), WOFF
// and WOFF2. Include their legacy font MIME types, but not EOT or
// arbitrary font/* subtypes merely because they are font MIME types.
Self::Font => matches!(
essence.as_str(),
"font/ttf"
| "font/otf"
| "font/sfnt"
| "font/collection"
| "font/woff"
| "font/woff2"
| "application/font-ttf"
| "application/font-otf"
| "application/font-sfnt"
| "application/font-woff"
| "application/vnd.ms-opentype"
),
Self::Font => moli_web_mime::is_supported_font_mime_essence(&essence),
Self::Style => essence == "text/css",
Self::Track => essence == "text/vtt",
_ => false,
@@ -682,7 +682,7 @@ fn parser_script_network_results_populate_buffered_resource_timing_snapshots() {
assert_eq!(
result,
r#"{"length":1,"name":"https://resource-timing-script.test/app.js","entryType":"resource","initiatorType":"script","instance":true,"responseStatus":200,"contentType":"application/javascript","renderBlockingStatusValid":true,"transferSizePositive":true,"encodedBodySize":7,"decodedBodySize":7,"identity":true,"jsonOwnKeys":true,"jsonMatchesEntry":true,"toJSONDescriptor":"toJSON:0:true:true:true","fakeToJSONError":"TypeError"}"#
r#"{"length":1,"name":"https://resource-timing-script.test/app.js","entryType":"resource","initiatorType":"script","instance":true,"responseStatus":200,"contentType":"text/javascript","renderBlockingStatusValid":true,"transferSizePositive":true,"encodedBodySize":7,"decodedBodySize":7,"identity":true,"jsonOwnKeys":true,"jsonMatchesEntry":true,"toJSONDescriptor":"toJSON:0:true:true:true","fakeToJSONError":"TypeError"}"#
);
}
@@ -99,9 +99,16 @@ async fn handle_timing_request(
body.clear();
}
let mut headers = format!(
"HTTP/1.1 {status} Fixture\r\nContent-Type: {mime}\r\nContent-Length: {}\r\nCache-Control: no-store\r\nConnection: close\r\n",
"HTTP/1.1 {status} Fixture\r\nContent-Length: {}\r\nCache-Control: no-store\r\nConnection: close\r\n",
body.len()
);
if url.path() == "/mime" {
for (_, value) in url.query_pairs().filter(|(name, _)| name == "value") {
headers.push_str(&format!("Content-Type: {value}\r\n"));
}
} else {
headers.push_str(&format!("Content-Type: {mime}\r\n"));
}
if redirect {
headers.push_str(&format!("Location: {}\r\n", get("to")));
}
@@ -133,6 +140,23 @@ async fn handle_timing_request(
#[tokio::test(flavor = "current_thread")]
async fn window_fetch_reports_native_resource_timing_at_body_terminal() {
run_resource_timing_fixture(
include_str!("../../../tests/fixtures/fetch-resource-timing.js"),
36,
)
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn resource_timing_minimizes_response_content_type() {
run_resource_timing_fixture(
include_str!("../../../tests/fixtures/resource-timing-content-type.js"),
44,
)
.await;
}
async fn run_resource_timing_fixture(fixture: &str, expected_total: usize) {
let first = timing_server().await;
let second = timing_server().await;
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
@@ -143,7 +167,6 @@ async fn window_fetch_reports_native_resource_timing_at_body_terminal() {
serde_json::to_string(&[&first.origin, &second.origin]).unwrap(),
))
.unwrap();
let fixture = include_str!("../../../tests/fixtures/fetch-resource-timing.js");
page.eval(&format!(
"({}).then(value => __fetchTimingResult = value, error => __fetchTimingResult = String(error));",
fixture.trim().trim_end_matches(';'),
@@ -159,6 +182,6 @@ async fn window_fetch_reports_native_resource_timing_at_body_terminal() {
let result = page.eval("__fetchTimingResult").unwrap();
let result: serde_json::Value = serde_json::from_str(&result)
.unwrap_or_else(|error| panic!("fetch timing probe returned {result:?}: {error}"));
assert_eq!(result["total"], 36);
assert_eq!(result["total"], expected_total);
assert_eq!(result["failures"], serde_json::json!([]));
}
@@ -0,0 +1,74 @@
(async () => {
const rows = [];
let sequence = 0;
const assert = (condition, message) => { if (!condition) throw new Error(message); };
const test = async (name, run) => {
try { await run(); rows.push({name, passed: true}); }
catch (error) { rows.push({name, passed: false, error: String(error)}); }
};
const urlFor = (values, origin = 0, extra = '') =>
`${__fetchOrigins[origin]}/mime?key=mime-${++sequence}&${values.map(value => 'value=' + encodeURIComponent(value)).join('&')}&${extra}`;
const entryFor = url => new Promise((resolve, reject) => {
const previous = performance.getEntriesByName(url, 'resource');
if (previous.length) { resolve(previous[0]); return; }
const observer = new PerformanceObserver(list => {
const entry = list.getEntriesByName(url, 'resource')[0];
if (entry) { clearTimeout(timer); observer.disconnect(); resolve(entry); }
});
observer.observe({type: 'resource'});
const timer = setTimeout(() => { observer.disconnect(); reject(new Error('missing resource entry')); }, 1000);
});
const fetchBody = (url, mode = 'cors') => fetch(url, {mode}).then(response => response.arrayBuffer());
const xhrBody = url => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = resolve;
xhr.onerror = () => reject(new Error('XHR failed'));
xhr.send();
});
const check = (entry, expected, initiator) => {
assert(entry.contentType === expected, `contentType: expected ${JSON.stringify(expected)}, got ${JSON.stringify(entry.contentType)}`);
assert(entry.toJSON().contentType === expected, 'toJSON preserves the minimized type');
assert(entry.initiatorType === initiator, 'initiator type');
assert(performance.getEntriesByName(entry.name, 'resource').length === 1, 'one entry per request');
};
const cases = [
[[], ''],
[[''], ''],
[['invalid'], ''],
[['*/*'], ''],
[['APPLICATION/X-JAVASCRIPT; charset=UTF-8'], 'text/javascript'],
[['text/x-ecmascript'], 'text/javascript'],
[['TEXT/JSON'], 'application/json'],
[['application/problem+json'], 'application/json'],
[['application/rss+xml'], 'application/xml'],
[['image/svg+xml; charset=UTF-8'], 'image/svg+xml'],
[['text/plain; charset=UTF-8'], 'text/plain'],
[['image/png'], 'image/png'],
[['image/jpe'], ''],
[['application/octet-stream'], ''],
[['text/html', 'application/json'], 'application/json'],
[['application/json,text/plain'], 'text/plain'],
[['text/html', '*/*', 'invalid'], 'text/html'],
[['text/plain; x="a,application/json"'], 'text/plain'],
[['application/json', 'application/unknown'], ''],
];
for (const [initiator, load] of [['fetch', fetchBody], ['xmlhttprequest', xhrBody]]) {
for (const [values, expected] of cases) await test(`${initiator} ${JSON.stringify(values)}`, async () => {
const url = urlFor(values);
await load(url);
check(await entryFor(url), expected, initiator);
});
for (const tao of ['', '*']) await test(`${initiator} CORS TAO=${tao}`, async () => {
const url = urlFor(['application/ld+json'], 1, `cors=*&tao=${tao}`);
await load(url);
check(await entryFor(url), 'application/json', initiator);
});
}
for (const tao of ['', '*']) await test(`opaque response TAO=${tao}`, async () => {
const url = urlFor(['image/png; x=private'], 1, `tao=${tao}`);
await fetchBody(url, 'no-cors');
check(await entryFor(url), '', 'fetch');
});
return JSON.stringify({total: rows.length, failures: rows.filter(row => !row.passed)});
})();
+20
View File
@@ -143,6 +143,26 @@ pub fn is_font_mime_essence(input: &str) -> bool {
)
}
/// MIME types consumed by Moli's SFNT, WOFF and WOFF2 font pipeline.
/// This includes collections and legacy aliases, but not EOT or arbitrary
/// font/* subtypes merely because they are font MIME types.
pub fn is_supported_font_mime_essence(input: &str) -> bool {
matches!(
input,
"font/ttf"
| "font/otf"
| "font/sfnt"
| "font/collection"
| "font/woff"
| "font/woff2"
| "application/font-ttf"
| "application/font-otf"
| "application/font-sfnt"
| "application/font-woff"
| "application/vnd.ms-opentype"
)
}
pub fn is_form_urlencoded_mime(input: &str) -> bool {
mime_essence(input).is_some_and(|mime| mime == "application/x-www-form-urlencoded")
}
+4 -4
View File
@@ -17,10 +17,10 @@ pub use classification::{
is_font_mime, is_font_mime_essence, is_form_urlencoded_mime, is_html_document_mime,
is_image_mime, is_image_mime_essence, is_javascript_mime, is_javascript_mime_essence,
is_json_mime, is_json_mime_essence, is_multipart_form_data_mime, is_png_image_mime,
is_png_image_mime_essence, is_supported_document_mime_type, is_svg_image_mime,
is_svg_image_mime_essence, is_text_document_mime, is_text_mime, is_text_mime_essence,
is_video_mime, is_video_mime_essence, is_webassembly_mime, is_xml_document_mime,
multipart_form_data_boundary,
is_png_image_mime_essence, is_supported_document_mime_type, is_supported_font_mime_essence,
is_svg_image_mime, is_svg_image_mime_essence, is_text_document_mime, is_text_mime,
is_text_mime_essence, is_video_mime, is_video_mime_essence, is_webassembly_mime,
is_xml_document_mime, multipart_form_data_boundary,
};
pub use data_url::{
data_url_body_and_computed_mime_type, data_url_body_and_mime_type, data_url_mime_type,