Chrome outputs emtpy document fragment to the console before inserting it to the dom

When inserting a document fragment to the dom Chrome browser outputs empty document fragment to the console whereas Firefox outputs non-empty fragment before inserting and empty fragment after inserting.
If a fragment is not inserted to the dom then Chrome also outputs non-empty fragment to the console.
I’m running latest stable versions (Chrome 126 and Firefox 128)

const init = function() {
  let fn0 = function() {
    let fragment = document.querySelector('template').content.cloneNode(true);
    console.log('before insertion: ', fragment); // empty in Chrome but not in Firefox
    document.querySelector('ul').append(fragment);
    console.log('after insertion: ', fragment);
  };
  let fn1 = function() {
    let fragment = document.querySelector('template').content.cloneNode(true);
    console.log('no insertion: ', fragment); // non-empty in both
  };
  let buttons = document.getElementsByTagName('button');
  buttons[0].addEventListener('click', fn0);
  buttons[1].addEventListener('click', fn1);
};
window.addEventListener('load', init);
.container {
  width: 200px;
  height: 200px;
  display: flex;
  flex-direction: column;
  font-family: sans-serif;
}

.bar {
  flex-basis: max-content;
  display: flex;
  padding: 6px;
  gap: 6px;
}

.list {
  overflow: auto;
  border: 1px solid;
}

button {
  text-align: left;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>template test</title>
</head>

<body>
  <div class="container">
    <div class="bar">
      <button type="button">log document fragment before insertion, then insert the fragment and log the fragment after insertion</button>
      <button type="button">log document fragment without inserting</button>
    </div>
    <div class="list">
      <ul>
        <template>
          <li>list item</li>
        </template>
      </ul>
    </div>
  </div>
</body>

</html>

1

There are two points here. First, console is asynchronous, sou you can’t really know when exactly your value will be actually rendered in the console. Second is that chrome actually shows you current state of the object and not it’s state at the time you logged it. Consider this code:

const obj = {a: 1};
console.log(obj);
obj.b = 2;
delete obj.a;

If you try to log it in chrome, you will see that it have a shape you expect ({a: 1}). However, if you try to expand it to see properties, you will see that it has no “a” property at all, despite the fact that it was logged when property existed. Same thing happens with your DocumentFragment – you see it’s state at “current” time, but expect to see “past”. One more thing here is the difference in logging regular objects and DOM structures (like DocumentFrarment or other elements). In first case you see stringified version of it and then can expand to see real thing. In second case (your case) you see object from get go.

This is pretty confusing, but can be very useful when you want to track how object changes during it’s lifetime, especially when paired with breakpoints (set in browser or with debugger statement) without having to log same thing many many many times.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật