PerformanceResourceTiming: domainLookupStart property
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.
Note: This feature is available in Web Workers.
Thuộc tính chỉ đọc domainLookupStart trả về timestamp ngay trước khi trình duyệt bắt đầu tra cứu tên miền cho tài nguyên.
Giá trị
Thuộc tính domainLookupStart có thể có các giá trị sau:
- Một
DOMHighResTimeStampngay trước khi trình duyệt bắt đầu tra cứu tên miền cho tài nguyên. 0nếu tài nguyên được lấy tức thời từ cache.0nếu tài nguyên là một yêu cầu cross-origin và không dùng tiêu đề phản hồi HTTPTiming-Allow-Origin.
Ví dụ
>Đo thời gian tra cứu DNS
Các thuộc tính domainLookupStart và domainLookupEnd có thể dùng để đo xem quá trình tra cứu DNS mất bao lâu.
const dns = entry.domainLookupEnd - entry.domainLookupStart;
Ví dụ dùng PerformanceObserver, thông báo các performance entry resource mới khi chúng được ghi trong performance timeline của trình duyệt. Dùng tùy chọn buffered để truy cập các entry từ trước khi observer được tạo.
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
const dns = entry.domainLookupEnd - entry.domainLookupStart;
if (dns > 0) {
console.log(`${entry.name}: DNS lookup duration: ${dns}ms`);
}
});
});
observer.observe({ type: "resource", buffered: true });
Ví dụ dùng Performance.getEntriesByType(), chỉ hiển thị các performance entry resource đang có trong performance timeline tại thời điểm bạn gọi phương thức này:
const resources = performance.getEntriesByType("resource");
resources.forEach((entry) => {
const dns = entry.domainLookupEnd - entry.domainLookupStart;
if (dns > 0) {
console.log(`${entry.name}: DNS lookup duration: ${dns}ms`);
}
});
Thông tin timing liên miền
Nếu giá trị của thuộc tính domainLookupStart là 0, tài nguyên có thể là một yêu cầu cross-origin. Để cho phép xem thông tin timing cross-origin, cần đặt tiêu đề phản hồi HTTP Timing-Allow-Origin.
Ví dụ, để cho phép https://mdn.go-mizu.dev xem timing tài nguyên, tài nguyên cross-origin nên gửi:
Timing-Allow-Origin: https://mdn.go-mizu.dev
Thông số kỹ thuật
| Specification |
|---|
| Resource Timing> # dom-performanceresourcetiming-domainlookupstart> |