Raygun before or after JQuery?

Alec

Posted on
Mar 07 2015

I have been loading raygun.js very early to ensure it can capture any errors loading other libraries or Javascript code, but I found that it wasn't catching JQuery $.ajax errors. When I dug into it I saw this recommendation that raygun should be loaded after JQuery:

https://raygun.io/blog/2013/04/raygun4js-gets-bigger-error-messages-jquery-support/

So, what would be the best way to handle this then? Would anything be amiss if I simple call Raygun.attach again later on the page after JQuery is loaded so I can potentially get the best of both worlds?


Alec

Posted on
Mar 07 2015

1) I spent a few hours trying the idea of calling .attach later. That won't work because Raygun burns in $document based on $ being present from JQuery. That variable set doesn't occur again on .attach.

2) I tried to set the variable but raygun.js runs in a Closure so no clean way to access it.

3) I also tried simply loading raygun.js twice on a page and that just gave an error: Uncaught TypeError: Cannot set property 'parseUrl' of undefined (line 1919 of raygun.js 3.1.0)

I think supporting this scenario needs the Raygun team as I cannot seem to do a remotely clean workaround.


Callum

Posted on
Mar 09 2015

Hi Alec,

One solution would be to bundle jQuery, then Raygun (calling attach which binds to $.ajaxError), then all other library and app code, ensuring a deterministic load order and minimizing HTTP request overhead.

At this stage Raygun4JS's Ajax error handling functionality requires $ to be loaded previously as you've discovered. As a workaround if you wish to call it twice you may be able to call Raygun.constructNewRaygun(), which will return a new Raygun instance which you can call init().attach() on after all the libraries and app code have loaded. I have added a note to investigate the implications of re-storing jQuery so that attach() can be called again when $ is present.


Alec

Posted on
Mar 10 2015

Raygun.constructNewRaygun() did allow me to attach again, but unfortunately now it double logs any error.

UPDATE: Hence the detach() method... I think I can get this going now.


Callum

Posted on
Mar 10 2015

Good to hear! I'll investigate improving the experience for this scenario on our next pass over this provider.


Reply