⚡ Fix an IntersectionObserver Firing Only Once
An IntersectionObserver callback runs once immediately when you call observe(), reporting the target’s current intersection state before anything has actually changed, and again every time that state crosses one of your configured thresholds afterward. A callback that calls unobserve() unconditionally on its first invocation — written assuming the first call always means “it just became visible” — ends up unobserving the target during that very first synchronous report if the element happens to start out of view, so the observer never gets a chance to fire again when the element actually does scroll into frame.
🔎 The Problem
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
loadContent(entry.target);
observer.unobserve(entry.target); // unobserves on EVERY callback,
// including the initial report
// where isIntersecting is false
});
});
observer.observe(el); // el starts off-screen -> callback fires once
// with isIntersecting:false -> unobserved
// before it ever scrolls into view
✅ Fix: An IntersectionObserver Firing Only Once
- Check entry.isIntersecting before acting on a callback, and only call unobserve() when it’s true, so the observer stays attached through the initial “not yet visible” report.
- Log the entries array during development for any lazy-load or reveal-on-scroll feature, since the very first callback firing immediately (rather than only on scroll) surprises a lot of people the first time they see it.
- If you only care about one-time reveal behavior, pair the isIntersecting check with unobserve() rather than removing one and keeping the other — both lines are doing a different, necessary job.
🚨 Why This Is Dangerous
- Content that’s supposed to lazy-load on scroll but was unobserved before it ever became visible simply never loads, and because there’s no error, it can go unnoticed until a user reports a blank section on the page.
An IntersectionObserver’s first callback isn’t an event — it’s a status report, and status reports happen even when there’s nothing to report yet.




