ตรวจหาสถานะการมองเห็นเริ่มต้น

การพิจารณาว่าหน้าเว็บโหลดในเบื้องหลังตั้งแต่แรกหรือไม่ (เช่น เปิดในแท็บเบื้องหลังใหม่) เป็นสิ่งสำคัญสำหรับการตรวจสอบประสิทธิภาพที่แม่นยำ หน้าเว็บที่โหลดในเบื้องหลังมักจะแสดงผลล่าช้าและใช้เวลาในการวัดเมตริกนานขึ้น (เช่น First Contentful Paint) การระบุหน้าเว็บเหล่านี้ช่วยให้คุณกรองหน้าเว็บดังกล่าวออกจากการวิเคราะห์ประสิทธิภาพเพื่อหลีกเลี่ยงข้อมูลที่คลาดเคลื่อนได้

วิธีที่แม่นยำที่สุดในการวัดค่านี้คือการใช้ VisibilityStateEntry API ซึ่งจะบันทึกการเปลี่ยนแปลงระดับการเข้าถึงในไทม์ไลน์ประสิทธิภาพของเบราว์เซอร์ได้อย่างน่าเชื่อถือ ไม่ว่าสคริปต์จะทำงานจริงเมื่อใดก็ตาม

กำลังตรวจหาการแสดงผลเริ่มต้นและเวลาที่ทำงานอยู่เบื้องหลัง

บังคับ: ใช้ performance.getEntriesByType('visibility-state') เพื่อเข้าถึงประวัติระดับการเข้าถึงที่แน่นอน อย่าพึ่งพาการตรวจสอบ document.visibilityState เพียงอย่างเดียวในเวลาดำเนินการ เนื่องจากอาจเกิดภาวะแข่งขันได้

/**
 * Accurately determines visibility state history using the Performance API.
 */
function getVisibilityInfo() {
  // Retrieve VisibilityStateEntry members:
  const entries = performance.getEntriesByType('visibility-state');
  
  if (entries.length > 0) {
    const firstEntry = entries[0];
    
    // If the first performance entry for visibility is 'hidden',
    // the page was loaded in the background.
    const initiallyBackgrounded = firstEntry.name === 'hidden';
    
    // Find the precise, high-resolution timestamp of when the page 
    // was first backgrounded.
    let timeBackgrounded = null;
    for (const entry of entries) {
      if (entry.name === 'hidden') {
        // entry.startTime is used because it provides the exact browser 
        // timestamp of the visibility change, which is required for precision
        timeBackgrounded = entry.startTime;
        break;
      }
    }

    return {
      initiallyBackgrounded,
      timeBackgrounded
    };
  }
}

กลยุทธ์สำรอง

สำหรับสภาพแวดล้อมที่ไม่รองรับ คุณอาจกลับไปตรวจสอบพร็อพเพอร์ตี้ document.visibilityState หรือฟังเหตุการณ์ visibilitychange

ข้อบังคับ: คุณต้องเข้าใจว่าแนวทางสำรองนี้มักจะไม่แม่นยำอย่างมากในการระบุสถานะเบื้องหลังเริ่มต้น เนื่องจากสคริปต์สามารถโหลดและดำเนินการแบบอะซิงโครนัสได้ หน้าเว็บจึงอาจเปิดในแท็บเบื้องหลังแล้วผู้ใช้จึงนำมาไว้เบื้องหน้าก่อนที่สคริปต์จะดาวน์โหลดและดำเนินการเสร็จสิ้น เมื่อสคริปต์ทำงานในที่สุด document.visibilityState จะอ่านพร้อมกันเป็น 'visible' และคุณจะถือว่าหน้าเว็บโหลดในเบื้องหน้าอย่างไม่ถูกต้อง โดยพลาดสถานะที่ซ่อนอยู่ตอนแรกไปโดยสิ้นเชิง นอกจากนี้ การประทับเวลาสำรองยังขาดความแม่นยำภายในของ Performance API หากความแม่นยำเป็นสิ่งสำคัญอันดับแรก อย่าใช้โหมดสำรอง

/**
 * Fallback implementation using document.visibilityState.
 * This approach is prone to race conditions if the script loads asynchronously.
 */
function getFallbackVisibilityInfo() {
  // Check the state exactly when this script executes.
  // This will fail to detect an initial background state if the user 
  // foregrounded the page before this script executed.
  let initiallyBackgrounded = document.visibilityState === 'hidden';
  
  // If it's hidden now, we approximate that it was hidden from load (time 0).
  let timeBackgrounded = initiallyBackgrounded ? 0 : null;

  // Listen for future visibility changes to capture if it is backgrounded later.
  document.addEventListener('visibilitychange', () => {
    if (document.visibilityState === 'hidden' && timeBackgrounded === null) {
      // performance.now() is used here as a fallback, but it only gives 
      // us the time the event listener fired, not the precise internal 
      // browser time the visibility actually changed.
      timeBackgrounded = performance.now();
    }
  });

  return {
    get initiallyBackgrounded() { return initiallyBackgrounded; },
    get timeBackgrounded() { return timeBackgrounded; }
  };
}

// Modern implementation using VisibilityStateEntry API.
function getVisibilityInfo() {
  // Code omitted here would be the same modern
  // implementation shown earlier in this guide
  // ...
}

// DO: Detect if the VisibilityStateEntry API is available
if ('VisibilityStateEntry' in window) {
  // DO: If VisibilityStateEntry is available, use it first:
  getVisibilityInfo();
} else {
  // DO: If VisibilityStateEntry is unavailable, fall back to `document.visibilityState`:
  getFallbackVisibilityInfo();
}