<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>82931</bug_id>
          
          <creation_ts>2012-04-02 13:03:22 -0700</creation_ts>
          <short_desc>DOMContentLoaded and load events should fire asynchronously</short_desc>
          <delta_ts>2022-08-20 16:20:32 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>UI Events</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter>jochen</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>abarth</cc>
    
    <cc>ahmad.saleem792</cc>
    
    <cc>ap</cc>
    
    <cc>bfulgham</cc>
    
    <cc>cdumez</cc>
    
    <cc>rniwa</cc>
    
    <cc>tkent</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>593729</commentid>
    <comment_count>0</comment_count>
    <who name="">jochen</who>
    <bug_when>2012-04-02 13:03:22 -0700</bug_when>
    <thetext>According to http://www.whatwg.org/specs/web-apps/current-work/#the-end step 4 and step 7, the DOMContentLoaded and load events should be fired asynchronously. Currently, they&apos;re both fired synchronously.

Firefox seems to do this, all other browsers I tested (WebKit nightly, Opera 11, IE9) send the events synchronously

We could either push for an update to the spec, or send the signal asynchronously as well. The latter would allow for firing all events during DOM mutation asynchronously, without sending events that happen during DOM parsing after the load event (see e.g. issue 82778)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>593740</commentid>
    <comment_count>1</comment_count>
    <who name="Adam Barth">abarth</who>
    <bug_when>2012-04-02 13:19:09 -0700</bug_when>
    <thetext>Can you attach the test you&apos;re using?  I&apos;m not sure what you mean when you say we fire the load event synchronously.  Often the load event fires due to data coming in from the network, which happens asynchronously.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>593751</commentid>
    <comment_count>2</comment_count>
      <attachid>135167</attachid>
    <who name="">jochen</who>
    <bug_when>2012-04-02 13:26:05 -0700</bug_when>
    <thetext>Created attachment 135167
test case

That&apos;s my (lame) test-case. On Firefox, the timeout and load event come in an arbitrary order (reload a view times). On all other browsers I tested, load is always first.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>593752</commentid>
    <comment_count>3</comment_count>
    <who name="Adam Barth">abarth</who>
    <bug_when>2012-04-02 13:27:55 -0700</bug_when>
    <thetext>That&apos;s not a good test case.  There&apos;s no necessary relation between when those events fire.  For example, transfer the HTML document with chunked encoding and make the server sleep for a minute between the &lt;/div&gt; and the &lt;/body&gt;.  All browsers will fire the timeout before the load event in that case.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>593777</commentid>
    <comment_count>4</comment_count>
    <who name="">jochen</who>
    <bug_when>2012-04-02 13:37:20 -0700</bug_when>
    <thetext>(In reply to comment #1)
&gt; Can you attach the test you&apos;re using?  I&apos;m not sure what you mean when you say we fire the load event synchronously.  Often the load event fires due to data coming in from the network, which happens asynchronously.

I read the spec as follows:

between step 1 (set the document readiness to interactive) and step 4 (fire DOMContentLoaded event), we should drop back to the message loop in any case.

If there are no scripts to be run when the documented finished parsing, we won&apos;t spin the message loop

same between steps 4 and 7</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>593836</commentid>
    <comment_count>5</comment_count>
    <who name="">jochen</who>
    <bug_when>2012-04-02 14:17:49 -0700</bug_when>
    <thetext>I chatted with Adam a bit, and we both agree that the test case is lame.

We considered making events that are fired asynchronously but should come before the load event could just delay the load event, but this &quot;delay&quot; is done between the DOMContentLoaded and the load event, so it would be still observable that the events are fired in the wrong (as in not conforming to the spec) order.

Fixing this probably requires some non-trivial changes to FrameLoader, as it triggers e.g. DOMContentLoaded (fired in Document::finishedParsing) by invoking FrameLoader::finishedParsing in FrameLoader::stopLoading and then FrameLoader continues to mess with the document and its parser.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>593850</commentid>
    <comment_count>6</comment_count>
      <attachid>135188</attachid>
    <who name="Ojan Vafai">ojan</who>
    <bug_when>2012-04-02 14:31:28 -0700</bug_when>
    <thetext>Created attachment 135188
slightly better test case</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>593898</commentid>
    <comment_count>7</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2012-04-02 15:08:09 -0700</bug_when>
    <thetext>(In reply to comment #0)
&gt; According to http://www.whatwg.org/specs/web-apps/current-work/#the-end step 4 and step 7, the DOMContentLoaded and load events should be fired asynchronously. Currently, they&apos;re both fired synchronously.
&gt; 
&gt; Firefox seems to do this, all other browsers I tested (WebKit nightly, Opera 11, IE9) send the events synchronously

I&apos;m not certain if we want to change our behavior then.

It seems like there&apos;s some confusion here due to the bad test case. Could someone summarize the current state of this bug? (e.g. what&apos;s wrong with our current behavior, other browsers&apos; behaviors, and possible solutions).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>593967</commentid>
    <comment_count>8</comment_count>
    <who name="">jochen</who>
    <bug_when>2012-04-02 16:05:38 -0700</bug_when>
    <thetext>(In reply to comment #7)
&gt; (In reply to comment #0)
&gt; &gt; According to http://www.whatwg.org/specs/web-apps/current-work/#the-end step 4 and step 7, the DOMContentLoaded and load events should be fired asynchronously. Currently, they&apos;re both fired synchronously.
&gt; &gt; 
&gt; &gt; Firefox seems to do this, all other browsers I tested (WebKit nightly, Opera 11, IE9) send the events synchronously
&gt; 
&gt; I&apos;m not certain if we want to change our behavior then.
&gt; 

note that this assessment was based on a poor test, so it might as well be wrong.

&gt; It seems like there&apos;s some confusion here due to the bad test case. Could someone summarize the current state of this bug? (e.g. what&apos;s wrong with our current behavior, other browsers&apos; behaviors, and possible solutions).

The main issue is that if you create an asynchronous event either before the DOMContentLoaded event, or between the DOMContentLoaded event and the load event (depending on which code path is executed), the said event will not be fired before DOMContentLoaded or load respectively.

Lacking a good test case, it&apos;s hard to describe what other browsers are doing.

A possible solution would be to clean up all code paths such that 

- there&apos;s no sync code path from firing DOMContentLoaded to firing load (Document::finishedParsing to Document::implicitClose)
- there&apos;s no sync code path from setting the document&apos;s readiness to interactive to firing DOMContentLoaded (HTMLDocumentParser::prepareToStopParsing to Document::finishedParsing)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>593986</commentid>
    <comment_count>9</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2012-04-02 16:28:30 -0700</bug_when>
    <thetext>(In reply to comment #8)
&gt; The main issue is that if you create an asynchronous event either before the DOMContentLoaded event, or between the DOMContentLoaded event and the load event (depending on which code path is executed), the said event will not be fired before DOMContentLoaded or load respectively.
&gt; 
&gt; Lacking a good test case, it&apos;s hard to describe what other browsers are doing.

How can we be talking about fixing a &quot;bug&quot; when we don&apos;t even know what we&apos;re testing. We need to figure out what other browsers are doing first, or else this whole discussion doesn&apos;t lead us anywhere.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>594036</commentid>
    <comment_count>10</comment_count>
    <who name="Ojan Vafai">ojan</who>
    <bug_when>2012-04-02 17:00:46 -0700</bug_when>
    <thetext>(In reply to comment #9)
&gt; (In reply to comment #8)
&gt; &gt; The main issue is that if you create an asynchronous event either before the DOMContentLoaded event, or between the DOMContentLoaded event and the load event (depending on which code path is executed), the said event will not be fired before DOMContentLoaded or load respectively.
&gt; &gt; 
&gt; &gt; Lacking a good test case, it&apos;s hard to describe what other browsers are doing.
&gt; 
&gt; How can we be talking about fixing a &quot;bug&quot; when we don&apos;t even know what we&apos;re testing. We need to figure out what other browsers are doing first, or else this whole discussion doesn&apos;t lead us anywhere.

That&apos;s nice in theory. In practice, browsers don&apos;t give a consistent set of APIs for determining how they behave here. All the tests that I&apos;ve been able to come up with are racy and make it hard to tell definitively what a browser does.

I&apos;m open to suggestions for how to test this better, but we should either try to match the spec or get the spec changed.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1450123</commentid>
    <comment_count>11</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2018-08-14 15:45:00 -0700</bug_when>
    <thetext>Latest spec: https://html.spec.whatwg.org/multipage/parsing.html#stop-parsing</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1890998</commentid>
    <comment_count>12</comment_count>
    <who name="Ahmad Saleem">ahmad.saleem792</who>
    <bug_when>2022-08-12 14:37:44 -0700</bug_when>
    <thetext>Latest requests across browsers:

*** Safari 15.6 on macOS 12.5 ***

[object HTMLBodyElement]
DOMContentLoaded
load
focus
postMessage

*** Safari Technical Preview 151 ****

[object HTMLBodyElement]
DOMContentLoaded
load
focus
postMessage

*** Chrome Canary 106 ***

[object HTMLBodyElement]
focus
postMessage
DOMContentLoaded
load

*** Firefox Nightly 105 ***

[object HTMLBodyElement]
focus
DOMContentLoaded
load
postMessage

________

I am not sure on web-spec but just wanted to posted updated results. I used &quot;slightly better..&quot; test case. Thanks!</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>135167</attachid>
            <date>2012-04-02 13:26:05 -0700</date>
            <delta_ts>2012-04-02 13:26:05 -0700</delta_ts>
            <desc>test case</desc>
            <filename>test.html</filename>
            <type>text/html</type>
            <size>260</size>
            <attacher>jochen</attacher>
            
              <data encoding="base64">PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8Ym9keT4KPHNjcmlwdD4KZnVuY3Rpb24gbG9nKG1zZykg
ewogICAgZG9jdW1lbnQuZ2V0RWxlbWVudEJ5SWQoJ2xvZycpLmlubmVySFRNTCArPSBtc2cgKyAn
PGJyPic7Cn0KCm9ubG9hZCA9IGZ1bmN0aW9uKCkgewogICAgbG9nKCdsb2FkJyk7Cn0KCnNldFRp
bWVvdXQoZnVuY3Rpb24oKSB7IGxvZygndGltZW91dCcpOyB9LCAwKTsKPC9zY3JpcHQ+CjxkaXYg
aWQ9J2xvZyc+PC9kaXY+CjwvYm9keT4KPC9odG1sPgo=
</data>

          </attachment>
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>135188</attachid>
            <date>2012-04-02 14:31:28 -0700</date>
            <delta_ts>2012-04-02 14:31:28 -0700</delta_ts>
            <desc>slightly better test case</desc>
            <filename>scratch.html</filename>
            <type>text/html</type>
            <size>1026</size>
            <attacher name="Ojan Vafai">ojan</attacher>
            
              <data encoding="base64">PCFEb2NUeXBlIEhUTUw+Cjxib2R5IG9ubG9hZD0ibG9nZ2VyLmlubmVySFRNTCArPSAnbG9hZDxi
cj4nIj4KU2hvdWxkIHNlZSAidGltZW91dCIgcHJpbnRlZCBiZWZvcmUgImZvY3VzIi4KPGRpdiBp
ZD0ibG9nZ2VyIj48L2Rpdj4KPHNjcmlwdD4KdmFyIGlucHV0ID0gZG9jdW1lbnQuY3JlYXRlRWxl
bWVudCgnaW5wdXQnKTsKaW5wdXQuc2V0QXR0cmlidXRlKCdhdXRvZm9jdXMnLCAndHJ1ZScpOwps
b2dnZXIgPSBkb2N1bWVudC5nZXRFbGVtZW50QnlJZCgnbG9nZ2VyJyk7Cgp3aW5kb3cub25tZXNz
YWdlID0gZnVuY3Rpb24oKSB7CiAgICBsb2dnZXIuaW5uZXJIVE1MICs9ICdwb3N0TWVzc2FnZTxi
cj4nOwp9Cgpkb2N1bWVudC5hZGRFdmVudExpc3RlbmVyKCdET01Db250ZW50TG9hZGVkJywgZnVu
Y3Rpb24oKSB7CiAgICBsb2dnZXIuaW5uZXJIVE1MICs9ICdET01Db250ZW50TG9hZGVkPGJyPic7
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