কোনও পৃষ্ঠা প্রাথমিকভাবে ব্যাকগ্রাউন্ডে লোড করা হয়েছে কিনা তা নির্ধারণ করা (যেমন, একটি নতুন ব্যাকগ্রাউন্ড ট্যাবে খোলা হয়েছে) সঠিক পারফর্ম্যান্স মনিটর করার জন্য গুরুত্বপূর্ণ। ব্যাকগ্রাউন্ডে লোড হওয়া পৃষ্ঠা প্রায়ই রেন্ডার হতে দেরি করে এবং মেট্রিক টাইম (যেমন, 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' হিসেবে পড়বে এবং আপনি ভুলভাবে ধরে নেবেন যে পৃষ্ঠাটি ফোরগ্রাউন্ডে লোড করা হয়েছে, এর প্রাথমিক লুকানো অবস্থা সম্পূর্ণভাবে মিস হয়ে যাবে। এছাড়াও, ফলব্যাক টাইমস্ট্যাম্পে পারফর্ম্যান্স 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();
}