forked from bluesmoon/boomerang
-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathauto-xhr.js
3210 lines (2782 loc) · 106 KB
/
auto-xhr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Instrument and measure `XMLHttpRequest` (AJAX) requests.
*
* With this plugin, sites can measure the performance of `XMLHttpRequests`
* (XHRs) and other in-page interactions after the page has been loaded.
*
* This plugin also monitors DOM manipulations following a XHR to filter out
* "background" XHRs.
*
* This plugin provides the backbone for the {@link BOOMR.plugins.SPA} plugin. Single Page App navigations
* use XHR and DOM monitoring to determine when the SPA navigations are complete.
*
* This plugin has a corresponding {@tutorial header-snippets} that helps monitor XHRs prior to Boomerang loading.
*
* For information on how to include this plugin, see the {@tutorial building} tutorial.
*
* ## What is Measured
*
* When `AutoXHR` is enabled, this plugin will monitor several events:
*
* - `XMLHttpRequest` requests
* - `Fetch` API requests
* - Clicks
* - `window.History` changes indirectly through SPA plugins (History, Angular, etc.)
*
* When any of these events occur, `AutoXHR` will start monitoring the page for
* other events, DOM manipulations and other networking activity.
*
* As long as the event isn't determined to be background activity (i.e an XHR
* that didn't change the DOM at all), the event will be measured until all networking
* activity has completed.
*
* This means if your click generated an XHR that triggered an updated view to fetch
* more HTML that added images to the page, the entire event will be measured
* from the click to the last image completing.
*
* ## Usage
*
* To enable AutoXHR, you should set {@link BOOMR.plugins.AutoXHR.init|instrument_xhr} to `true`:
*
* ```
* BOOMR.init({
* instrument_xhr: true
* });
* ```
*
* Once enabled and initialized, the `window.XMLHttpRequest` object will be
* replaced with a "proxy" object that instruments all XHRs.
*
* ## Monitoring XHRs
*
* After `AutoXHR` is enabled, any `XMLHttpRequest.send` will be monitored:
*
* ```
* xhr = new XMLHttpRequest();
* xhr.open("GET", "/api/foo");
* xhr.send(null);
* ```
*
* If this XHR triggers DOM changes, a beacon will eventually be sent.
*
* This beacon will have `http.initiator=xhr` and the beacon parameters will differ
* from a Page Load beacon. See {@link BOOMR.plugins.RT} and
* {@link BOOMR.plugins.NavigationTiming} for details.
*
* ## Combining XHR Beacons
*
* By default `AutoXHR` groups all XHR activity that happens in the same event together.
*
* If you have one XHR that immediately triggers a second XHR, you will get a single
* XHR beacon. The `u` (URL) will be of the first XHR.
*
* If you don't want this behavior, and want to measure *every* XHR on the page, you
* can enable {@link BOOMR.plugins.AutoXHR.init|alwaysSendXhr=true}. When set, every
* distinct XHR will get its own XHR beacon.
*
* ```
* BOOMR.init({
* AutoXHR: {
* alwaysSendXhr: true
* }
* });
* ```
*
* {@link BOOMR.plugins.AutoXHR.init|alwaysSendXhr} can also be a list of strings
* (matching URLs), regular expressions (matching URLs), or a function which returns
* true for URLs to always send XHRs for.
*
* ```
* BOOMR.init({
* AutoXHR: {
* alwaysSendXhr: [
* "http://domain.com/url/to/match",
* /regexmatch/,
* ]
* }
* });
*
* // or
*
* BOOMR.init({
* AutoXHR: {
* alwaysSendXhr: function(url) {
* return url.indexOf("domain.com") !== -1;
* }
* }
* });
* ```
*
*
* ### Compatibility and Browser Support
*
* Currently supported Browsers and platforms that AutoXHR will work on:
*
* - IE 9+ (not in quirks mode)
* - Chrome 38+
* - Firefox 25+
*
* In general we support all browsers that support
* [MutationObserver]{@link https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver}
* and [XMLHttpRequest]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest}.
*
* We will not use MutationObserver in IE 11 due to several browser bugs.
* See {@link BOOMR.utils.isMutationObserverSupported} for details.
*
* ## Excluding Certain Requests or DOM Elements From Instrumentation
*
* Whenever Boomerang intercepts an `XMLHttpRequest` (or Fetch), it will check if that request
* matches anything in the XHR exclude list. If it does, Boomerang will not
* instrument, time, send a beacon for that request, or include it in the
* {@link BOOMR.plugins.SPA} calculations.
*
* There are two methods of excluding XHRs: Defining the `BOOMR.xhr_excludes` array,
* or using the {@link BOOMR.plugins.AutoXHR.init|excludeFilters} option.
*
* The `BOOMR.xhr_excludes` XHR excludes list is defined by creating a map, and
* adding URL parts that you would like to exclude from instrumentation. You
* can put any of the following in `BOOMR.xhr_excludes`:
*
* 1. A full HREF
* 2. A hostname
* 3. A path
*
* Example:
*
* ```
* BOOMR = window.BOOMR || {};
*
* BOOMR.xhr_excludes = {
* "www.mydomain.com": true,
* "a.anotherdomain.net": true,
* "/api/v1/foobar": true,
* "https://mydomain.com/dashboard/": true
* };
* ```
*
* The {@link BOOMR.plugins.AutoXHR.init|excludeFilters} gives you more control by allowing you
* to specify one or more callbacks that will be run for each XHR/Fetch. If any callback
* returns `true`, the XHR/Fetch will *not* be instrumented.
*
* ```
* BOOMR.init({
* AutoXHR: {
* excludeFilters: [
* function(anchor) {
* return anchor.href.match(/non-trackable/);
* }
* ]
* }
* });
* ```
*
* Finally, the {@link BOOMR.plugins.AutoXHR.init|domExcludeFilters} DOM filters can be used to filter out
* specific DOM elements from being tracked (marked as "uninteresting").
*
* ```
* BOOMR.init({
* AutoXHR: {
* domExcludeFilters: [
* function(elem) {
* return elem.id === "ignore";
* }
* ]
* }
* });
* ```
*
* ## Beacon Parameters
*
* XHR beacons have different parameters in general than Page Load beacons.
*
* - Many of the timestamps will differ, see {@link BOOMR.plugins.RT}
* - All of the `nt_*` parameters are ResourceTiming, see {@link BOOMR.plugins.NavigationTiming}
* - `u`: the URL of the resource that was fetched
* - `pgu`: The URL of the page the resource was fetched on
* - `http.initiator`: `xhr` for both XHR and Fetch requests
*
* ## Interesting Nodes
*
* A `MutationObserver` is used to detect "interesting" nodes. Interesting nodes are
* new IMG/IMAGE/IFRAME/LINK (rel=stylesheet) nodes or existing nodes that are
* changing their source URL.
*
* We consider the following "uninteresting" nodes:
* - Nodes that have either a width or height <= 1px.
* - Nodes with either a width or height of 0px.
* - Nodes that have display:none.
* - Nodes that have visibility:hidden.
* - Nodes with an opacity:0.
* - Nodes that update their source URL to the same value.
* - Nodes that have a blank source URL.
* - Images with a loading="lazy" attribute
* - Nodes that have a source URL starting with `about:`, `javascript:` or `data:`.
* - SCRIPT nodes because there is no consistent way to detect when they have loaded.
* - Existing IFRAME nodes that are changing their source URL because is no consistent
* way to detect when they have loaded.
* - Nodes that have a source URL that matches a AutoXHR exclude filter rule.
* - Nodes that have been manually excluded
*
* ## Algorithm
*
* Here's how the general AutoXHR algorithm works:
*
* - `0.0` SPA hard route change (page navigation)
*
* - Monitor for XHR resource requests and interesting Mutation resource requests
* for 1s or at least until page onload. We extend our 1s timeout after all
* interesting resource requests have completed.
*
* - `0.1` SPA soft route change from a synchronous call (eg. History changes as a
* result of a pushState or replaceState call)
*
* - In this case we get the new URL when the developer calls pushState or
* replaceState.
* - We create a pending event with the start time and the new URL.
* - We do not know if they plan to make an XHR call or use a dynamic script
* node, or do nothing interesting (eg: just make a div visible/invisible).
* - We also do not know if they will do this before or after they've called
* pushState/replaceState.
* - Our best bet is to monitor if either a XHR resource requests or interesting
* Mutation resource requests will happen in the next 1s.
* - When interesting resources are detected, we wait until they complete.
* - We restart our 1s timeout after all interesting resources have completed.
* - If something uninteresting happens, we set the timeout for 1 second if
* it wasn't already started.
* - We'll only do this once per event since we don't want to continuously
* extend the timeout with each uninteresting event.
* - If nothing happens during the additional timeout, we stop watching and fire the
* event. The event end time will be end time of the last tracked resource.
* - If nothing interesting was detected during the first timeout and the URL has not
* changed then we drop the event.
*
* - `0.2` SPA soft route change from an asynchronous call (eg. History changes as a
* result of the user hitting Back/Forward and we get a window.popstate event)
*
* - In this case we get the new URL from location.href when our event listener
* runs.
* - We do not know if this event change will result in some interesting network
* activity or not.
* - We do not know if the developer's event listener has already run before
* ours or if it will run in the future or even if they do have an event listener.
* - Our best bet is the same as 0.1 above.
*
* - `0.3` SPA soft route change from a click that triggers a XHR before the state is
* changed (when {@link BOOMR.plugins.AutoXHR.init|spaStartFromClick} is enabled).
*
* - We store the time of the click
* - If any additional XHRs come next, we track those
* - When a pushState comes after the XHRs (before the timeout), we will "migrate" the
* click to a SPA event
*
* - `1` Click initiated (Only available when no SPA plugins are enabled)
*
* - User clicks on something.
* - We create a pending event with the start time and no URL.
* - We turn on DOM observer, and wait up to 50 milliseconds for activity.
* - If nothing happens during the first timeout, we stop watching and clear the
* event without firing it.
* - Else if something uninteresting happens, we set the timeout for 1s
* if it wasn't already started.
* - We'll only do this once per event since we don't want to continuously
* extend the timeout with each uninteresting event.
* - Else if an interesting node is added, we add load and error listeners
* and turn off the timeout but keep watching.
* - Once all listeners have fired, we start waiting again up to 50ms for activity.
* - If nothing happens during the additional timeout, we stop watching and fire the event.
*
* - `2` XHR/Fetch initiated
*
* - XHR or Fetch request is sent.
* - We create a pending event with the start time and the request URL.
* - We watch for all changes in XHR state (for async requests) and for load (for
* all requests).
* - We turn on DOM observer at XHR Onload or when the Fetch Promise resolves.
* We then wait up to 50 milliseconds for activity.
* - If nothing happens during the first timeout, we stop watching and clear the
* event without firing it.
* - If something uninteresting happens, we set the timeout for 1 second if
* it wasn't already started.
* - We'll only do this once per event since we don't want to continuously
* extend the timeout with each uninteresting event.
* - Else if an interesting node is added, we add load and error listeners
* and turn off the timeout.
* - Once all listeners have fired, we start waiting again up to 50ms for activity.
* - If nothing happens during the additional timeout, we stop watching and fire the event.
*
* What about overlap?
*
* - `3.1` XHR/Fetch initiated while a click event is pending
*
* - If first click watcher has not detected anything interesting or does not
* have a URL, abort it
* - If the click watcher has detected something interesting and has a URL, then
* - Proceed with 2 above.
* - Concurrently, click stops watching for new resources
* - Once all resources click is waiting for have completed then fire the event.
*
* - `3.2` Click initiated while XHR/Fetch event is pending
*
* - Ignore click
*
* - `3.3` Click initiated while a click event is pending
*
* - If first click watcher has not detected anything interesting or does not
* have a URL, abort it.
* - Else proceed with parallel event steps from 3.1 above.
*
* - `3.4` XHR/Fetch initiated while an XHR/Fetch event is pending
*
* - Add the second XHR/Fetch as an interesting resource to be tracked by the
* XHR pending event in progress.
*
* - `3.5` XHR/Fetch initiated while SPA event is pending
*
* - Add the second XHR/Fetch as an interesting resource to be tracked by the
* XHR pending event in progress.
*
* - `3.6` SPA event initiated while an XHR event is pending
* - Proceed with 0 above.
* - Concurrently, XHR event stops watching for new resources. Once all resources
* the XHR event is waiting for have completed, fire the event.
*
* - `3.7` SPA event initiated while a SPA event is pending
* - If the pending SPA event had detected something interesting then send an aborted
* SPA beacon. If not, drop the pending event.
* - Proceed with 0 above.
*
* @class BOOMR.plugins.AutoXHR
*/
(function() {
var w, d, handler, a, impl,
readyStateMap = ["uninitialized", "open", "responseStart", "domInteractive", "responseEnd"];
/**
* Single Page Applications get an additional timeout for all XHR Requests to settle in.
* This is used after collecting resources for a SPA routechange.
* Default is 1000ms, overridable with spaIdleTimeout
* @type {number}
* @constant
* @default
*/
var SPA_TIMEOUT = 1000;
/**
* @constant
* @desc
* For early beacons.
* Single Page Applications get an additional timeout after the resource requests in
* flight reaches 0 for the first time.
* We want to allow some time for the SPA to fill in any custom dimensions that we
* capture on the early beacon.
* @type {number}
* @default
*/
var SPA_EARLY_TIMEOUT = 100;
/**
* Clicks and XHR events get 50ms for an interesting thing to happen before
* being cancelled.
* Default is 50ms, overridable with xhrIdleTimeout
* @type {number}
* @constant
* @default
*/
var CLICK_XHR_TIMEOUT = 50;
/**
* Fetch events that don't read the body of the response get an extra wait time before
* we look for it's corresponding ResourceTiming entry.
* Default is 200ms, overridable with fetchBodyUsedWait
* @type {number}
* @constant
* @default
*/
var FETCH_BODY_USED_WAIT_DEFAULT = 200;
/**
* If we get a Mutation event that doesn't have any interesting nodes after
* a Click or XHR event started, wait up to 1,000ms for an interesting one
* to happen before cancelling the event.
* @type {number}
* @constant
* @default
*/
var UNINTERESTING_MUTATION_TIMEOUT = 1000;
/**
* How long to wait if we're not ready to send a beacon to try again.
* @constant
* @type {number}
* @default
*/
var READY_TO_SEND_WAIT = 500;
/**
* Timeout event fired for XMLHttpRequest resource
* @constant
* @type {number}
* @default
*/
var XHR_STATUS_TIMEOUT = -1001;
/**
* XMLHttpRequest was aborted
* @constant
* @type {number}
* @default
*/
var XHR_STATUS_ABORT = -999;
/**
* An error occured fetching XMLHttpRequest/Fetch resource
* @constant
* @type {number}
* @default
*/
var XHR_STATUS_ERROR = -998;
/**
* An exception occured as we tried to request resource
* @constant
* @type {number}
* @default
*/
var XHR_STATUS_OPEN_EXCEPTION = -997;
/**
* Default resources to count as Back-End during a SPA nav
* @constant
* @type {string[]}
*/
var SPA_RESOURCES_BACK_END = ["xmlhttprequest", "script", "fetch"];
BOOMR = window.BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
if (BOOMR.plugins.AutoXHR) {
return;
}
w = BOOMR.window;
// If this browser cannot support XHR, we'll just skip this plugin which will
// save us some execution time.
// XHR not supported or XHR so old that it doesn't support addEventListener
// (IE 6, 7, 8, as well as newer running in quirks mode.)
if (!w || !w.XMLHttpRequest || !(new w.XMLHttpRequest()).addEventListener) {
// Nothing to instrument
return;
}
/* BEGIN_DEBUG */
function debugLog(msg) {
BOOMR.debug(msg, "AutoXHR");
}
/* END_DEBUG */
/**
* Tries to resolve `href` links from relative URLs.
*
* This implementation takes into account a bug in the way IE handles relative
* paths on anchors and resolves this by assigning `a.href` to itself which
* triggers the URL resolution in IE and will fix missing leading slashes if
* necessary.
*
* @param {string} anchor The anchor object to resolve
*
* @returns {string} The unrelativized URL href
* @memberof BOOMR.plugins.AutoXHR
*/
function getPathName(anchor) {
if (!anchor) {
return null;
}
/*
correct relativism in IE
anchor.href = "./path/file";
anchor.pathname == "./path/file"; //should be "/path/file"
*/
anchor.href = anchor.href;
/*
correct missing leading slash in IE
anchor.href = "path/file";
anchor.pathname === "path/file"; //should be "/path/file"
*/
var pathName = anchor.pathname;
if (pathName.charAt(0) !== "/") {
pathName = "/" + pathName;
}
return pathName;
}
/**
* Based on the contents of BOOMR.xhr_excludes check if the URL that we instrumented as XHR request
* matches any of the URLs we are supposed to not send a beacon about.
*
* @param {HTMLAnchorElement} anchor HTML anchor element with URL of the element
* checked against `BOOMR.xhr_excludes`
*
* @returns {boolean} `true` if intended to be excluded, `false` if it is not in the list of excludables
* @memberof BOOMR.plugins.AutoXHR
*/
function shouldExcludeXhr(anchor) {
var urlIdx;
if (anchor.href) {
if (anchor.href.match(/^(about:|javascript:|data:)/i)) {
return true;
}
// don't track our own beacons (allow for protocol-relative URLs)
if (typeof BOOMR.getBeaconURL === "function" && BOOMR.getBeaconURL()) {
urlIdx = anchor.href.indexOf(BOOMR.getBeaconURL());
if (urlIdx === 0 || urlIdx === 5 || urlIdx === 6) {
return true;
}
}
}
return BOOMR.xhr_excludes.hasOwnProperty(anchor.href) ||
BOOMR.xhr_excludes.hasOwnProperty(anchor.hostname) ||
BOOMR.xhr_excludes.hasOwnProperty(getPathName(anchor));
}
/**
* Handles the MutationObserver for {@link BOOMR.plugins.AutoXHR}.
*
* @class MutationHandler
*/
function MutationHandler() {
this.watch = 0;
this.timer = null;
this.timerEarlyBeacon = null;
this.pending_events = [];
this.lastSpaLocation = null;
}
/**
* Disable internal MutationObserver instance. Use this when uninstrumenting the site we're on.
*
* @method
* @memberof MutationHandler
* @static
*/
MutationHandler.stop = function() {
MutationHandler.pause();
MutationHandler.observer = null;
};
/**
* Pauses the MutationObserver. Call [resume]{@link handler#resume} to start it back up.
*
* @method
* @memberof MutationHandler
* @static
*/
MutationHandler.pause = function() {
if (MutationHandler.observer &&
MutationHandler.observer.observer &&
!MutationHandler.isPaused) {
MutationHandler.isPaused = true;
MutationHandler.observer.observer.disconnect();
}
};
/**
* Resumes the MutationObserver after a [pause]{@link handler#pause}.
*
* @method
* @memberof MutationHandler
* @static
*/
MutationHandler.resume = function() {
if (MutationHandler.observer &&
MutationHandler.observer.observer &&
MutationHandler.isPaused) {
MutationHandler.isPaused = false;
MutationHandler.observer.observer.observe(d, MutationHandler.observer.config);
}
};
/**
* Initiate {@link MutationHandler.observer} on the
* [outer parent document]{@link BOOMR.window.document}.
*
* Uses [addObserver]{@link BOOMR.utils.addObserver} to instrument.
*
* [Our internal handler]{@link handler#mutation_cb} will be called if
* something happens.
*
* @method
* @memberof MutationHandler
* @static
*/
MutationHandler.start = function() {
if (MutationHandler.observer) {
// don't start twice
return;
}
var config = {
childList: true,
attributes: true,
subtree: true,
attributeFilter: ["src", "href"]
};
/* eslint-disable no-inline-comments */
// Add a perpetual observer
MutationHandler.observer = BOOMR.utils.addObserver(
d,
config,
null, // no timeout
handler.mutation_cb, // will always return true
null, // no callback data
handler
);
/* eslint-enable no-inline-comments */
if (MutationHandler.observer) {
MutationHandler.observer.config = config;
BOOMR.subscribe("page_unload", MutationHandler.stop, null, MutationHandler);
}
};
/**
* If an event has triggered a resource to be fetched we add it to the list of pending events
* here and wait for it to eventually resolve.
*
* @param {object} resource - [Resource]{@link AutoXHR#Resource} object we are waiting for
*
* @returns {?index} If we are already waiting for an event of this type null
* otherwise index in the [queue]{@link MutationHandler#pending_event}.
* @method
* @memberof MutationHandler
*/
MutationHandler.prototype.addEvent = function(resource) {
/* eslint-disable no-inline-comments */
var ev = {
type: resource.initiator,
resource: resource,
nodes_to_wait: 0, // MO resources + xhrs currently outstanding + wait filter (max: 1)
total_nodes: 0, // total MO resources + xhrs + wait filter (max: 1)
resources: [], // resources reported by MO handler (no xhrs)
xhr_resources: [], // resources reported by xhr monitoring (for debugging only)
complete: false,
aborted: false, // this event was aborted
firedEarlyBeacon: false
},
i,
last_ev,
last_ev_index,
index = this.pending_events.length,
self = this;
/* eslint-enable no-inline-comments */
for (i = index - 1; i >= 0; i--) {
if (this.pending_events[i] && !this.pending_events[i].complete) {
last_ev = this.pending_events[i];
last_ev_index = i;
break;
}
}
if (last_ev) {
if (last_ev.type === "click" &&
impl.singlePageApp &&
impl.spaStartFromClick &&
ev.type === "xhr") {
// spaStartFromClick active, we had a click, and this is an XHR
// mark that this XHR came from a click event
ev.resource.fromClick = true;
// transition XHR start time from the click
ev.resource.timing.click = last_ev.resource.timing.requestStart;
ev.resource.timing.requestStart = last_ev.resource.timing.requestStart;
ev.interesting = last_ev.interesting || 0;
ev.total_nodes += last_ev.total_nodes;
ev.resources = last_ev.resources.concat(ev.resources);
ev.xhr_resources = last_ev.xhr_resources.concat(ev.xhr_resources);
// stop the click event
this.pending_events[last_ev_index] = undefined;
this.watch--;
}
else if ((last_ev.type === "click" || last_ev.resource.fromClick) &&
impl.singlePageApp &&
impl.spaStartFromClick &&
ev.type === "spa") {
// spaStartFromClick active, we have a click (or XHR from click) active,
// and this is a SPA
// transition all XHR times
ev.resource.timing = last_ev.resource.timing;
ev.interesting = last_ev.interesting || 0;
ev.total_nodes += last_ev.total_nodes;
ev.resources = last_ev.resources.concat(ev.resources);
// add previous XHR URL
if (last_ev.resource.url) {
ev.xhr_resources.push(last_ev.resource.url);
}
// add any other XHRs tracked in the previous
ev.xhr_resources = ev.xhr_resources.concat(last_ev.xhr_resources);
// stop the previous event
this.pending_events[last_ev_index] = undefined;
this.watch--;
}
else if (last_ev.type === "click") {
// 3.1 & 3.3
if (last_ev.nodes_to_wait === 0 || !last_ev.resource.url) {
this.pending_events[last_ev_index] = undefined;
this.watch--;
// continue with new event
}
// last_ev will no longer receive watches as ev will receive them
// last_ev will wait for all interesting nodes and then send event
}
else if (last_ev.type === "xhr") {
// 3.2
if (ev.type === "click") {
return null;
}
// 3.4
// add this resource to the current event
else if (ev.type === "xhr") {
handler.add_event_resource(resource);
return null;
}
}
else if (BOOMR.utils.inArray(last_ev.type, BOOMR.constants.BEACON_TYPE_SPAS)) {
// add this resource to the current event
if (ev.type === "xhr") {
handler.add_event_resource(resource);
return null;
}
// if we have a pending SPA event, send an aborted load beacon before
// adding the new SPA event
if (BOOMR.utils.inArray(ev.type, BOOMR.constants.BEACON_TYPE_SPAS)) {
debugLog("Aborting previous SPA navigation");
// mark the end of this navigation as now
last_ev.resource.timing.loadEventEnd = BOOMR.now();
last_ev.aborted = true;
// send the previous SPA
this.sendEvent(last_ev_index);
}
}
}
if (ev.type === "click") {
// if we don't have a MutationObserver then we just abort.
// If an XHR starts later then it will be tracked as its own new event
if (!MutationHandler.observer) {
return null;
}
// give Click events 50ms to see if they resulted
// in DOM mutations (and thus it is an 'interesting event').
this.setTimeout(impl.xhrIdleTimeout, index);
}
else if (ev.type === "xhr") {
// XHR events will not set a timeout yet.
// The XHR's load finished callback will start the timer.
// Increase node count since we are waiting for the XHR that started this event
ev.nodes_to_wait++;
ev.total_nodes++;
// we won't track mutations yet, we'll monitor only when at least one of the
// tracked xhr nodes has had a response
ev.ignoreMO = true;
}
else if (BOOMR.utils.inArray(ev.type, BOOMR.constants.BEACON_TYPE_SPAS)) {
// try to start the MO, in case we haven't had the chance to yet
MutationHandler.start();
if (!resource.wait) {
// give SPAs a bit more time to do something since we know this was
// an interesting event.
this.setTimeout(impl.spaIdleTimeout, index);
}
else {
// a wait filter isn't a node, but we'll use the same logic.
// Increase node count since we are waiting for the waitComplete call.
ev.nodes_to_wait++;
ev.total_nodes++;
// waitComplete() should be called once the held beacon is complete.
// The caller is responsible for clearing the .wait flag
resource.waitComplete = function() {
self.load_finished(index);
resource.waitComplete = undefined;
};
}
}
this.watch++;
this.pending_events.push(ev);
resource.index = index;
return index;
};
/**
* If called with an event in the [pending events list]{@link MutationHandler#pending_events}
* trigger a beacon for this event.
*
* When the beacon is sent for this event is depending on either having a crumb, in which case this
* beacon will be sent immediately. If that is not the case we wait 5 seconds and attempt to send the
* event again.
*
* @param {number} index Index in event list to send
*
* @returns {undefined} Returns early if the event already completed
* @method
* @memberof MutationHandler
*/
MutationHandler.prototype.sendEvent = function(index) {
var ev = this.pending_events[index],
self = this,
now = BOOMR.now();
if (!ev || ev.complete) {
return;
}
this.clearTimeout(index);
if (BOOMR.readyToSend()) {
ev.complete = true;
this.watch--;
// for SPA events, the resource's URL may be set to the previous navigation's URL.
// reset it to the current document URL
if (BOOMR.utils.inArray(ev.type, BOOMR.constants.BEACON_TYPE_SPAS)) {
ev.resource.url = d.URL;
}
// if this was a SPA soft nav with no URL change and did not trigger additional resources
// then we will not send a beacon
if (ev.type === "spa" && ev.total_nodes === 0 && ev.resource.url === self.lastSpaLocation) {
debugLog("SPA beacon cancelled, no URL change or resources triggered");
BOOMR.fireEvent("spa_cancel");
this.pending_events[index] = undefined;
return;
}
// if click event did not trigger additional resources or doesn't have
// a url then do not send a beacon
if (ev.type === "click" && (ev.total_nodes === 0 || !ev.resource.url)) {
debugLog("Click beacon cancelled, no resources triggered or no resource URL");
this.pending_events[index] = undefined;
return;
}
// when in spaStartFromClick mode, if we're tracking clicks, transition them to an 'xhr' event
if (ev.type === "click" && impl.singlePageApp && impl.spaStartFromClick) {
ev.type = ev.resource.initiator = "xhr";
}
// if this was a XHR that did not trigger additional resources then we will not send a beacon,
// note that XHRs start out with total_nodes=1 to account for itself
if (impl.xhrRequireChanges &&
ev.type === "xhr" &&
ev.total_nodes === 1 &&
(typeof ev.interesting === "undefined" || ev.interesting === 0) &&
!matchesAlwaysSendXhr(ev.resource.url, impl.alwaysSendXhr)) {
debugLog("XHR beacon cancelled, no resources triggered");
this.pending_events[index] = undefined;
return;
}
if (BOOMR.utils.inArray(ev.type, BOOMR.constants.BEACON_TYPE_SPAS)) {
// save the last SPA location
self.lastSpaLocation = ev.resource.url;
if (!ev.forced) {
// if this was a SPA soft nav that triggered no additional resources, call it
// a 1ms duration
if (ev.type === "spa" && ev.total_nodes === 0) {
ev.resource.timing.loadEventEnd = ev.resource.timing.requestStart + 1;
}
// if the event wasn't forced then the SPA hard nav should have the page's
// onload end as its minimum load end time
if (ev.type === "spa_hard") {
var p = BOOMR.getPerformance();
if (p && p.timing && p.timing.loadEventEnd && ev.resource.timing.loadEventEnd &&
ev.resource.timing.loadEventEnd < p.timing.loadEventEnd) {
ev.resource.timing.loadEventEnd = p.timing.loadEventEnd;
}
}
}
}
this.sendResource(ev.resource, index);
}
else {
// No crumb, so try again after 500ms seconds
setTimeout(function() {
self.sendEvent(index);
}, READY_TO_SEND_WAIT);
}
};
/**
* Creates and triggers sending a beacon for a Resource that has finished loading.
*
* @param {Resource} resource The Resource to send a beacon on
* @param {number} eventIndex index of the event in the pending_events array
*
* @method
* @memberof MutationHandler
*/
MutationHandler.prototype.sendResource = function(resource, eventIndex) {
var self = this,
ev = self.pending_events[eventIndex];
// Use 'requestStart' as the startTime of the resource, if given
var startTime = resource.timing ? resource.timing.requestStart : undefined;
/**
* Called once the resource can be sent
* @param {boolean} [markEnd] Sets loadEventEnd once the function is run
* @param {number} [endTimestamp] End timestamp
*/
var sendResponseEnd = function(markEnd, endTimestamp) {
if (markEnd) {
resource.timing.loadEventEnd = endTimestamp || BOOMR.now();
}
// send any queued beacons first
BOOMR.real_sendBeacon();
// If the resource has an onComplete event, trigger it.
if (resource.onComplete) {
resource.onComplete(resource);
}
// Add ResourceTiming data to the beacon, starting at when 'requestStart'
// was for this resource.
if (BOOMR.plugins.ResourceTiming &&
BOOMR.plugins.ResourceTiming.is_enabled() &&
resource.timing &&
resource.timing.requestStart) {
// Save resourceTiming data onto beacon as it may not go out right away
resource.restiming = BOOMR.plugins.ResourceTiming.getCompressedResourceTiming(
resource.timing.requestStart,
resource.timing.loadEventEnd
);
}
// For SPAs, calculate Back-End and Front-End timings
if (BOOMR.utils.inArray(resource.initiator, BOOMR.constants.BEACON_TYPE_SPAS)) {
self.calculateSpaTimings(resource);
// If the SPA load was aborted, set the rt.quit and rt.abld flags
if (typeof eventIndex === "number" && self.pending_events[eventIndex].aborted) {