question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide snippet so bugs that occur when bugsnag is still loading are still captured

See original GitHub issue

When you load bugsnag asynchronously, bugs can still occur that are not captured.

import scriptjs from 'scriptjs';

window.addEventListener('unhandledrejection', (event) => {
  Bugsnag.notifyException(event.reason);
});

scriptjs('https://d2wy8f7a9ursnm.cloudfront.net/bugsnag-2.min.js', () => {
  Bugsnag.apiKey = 'xx';
});

// in this scenario, unhandledrejection happens before Bugsnag is loaded

We can fix this by using a snippet inspired by Google Analytics.

  1. Provide a small snippet that created Bugsnag inline and stores any exceptions.
  2. When bugsnag loads, it detects Bugsnag and the stored exceptions.
  3. If those exceptions exist, it sends them immediately

I’d like to avoid blocking the page from loading by not loading it async.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
eanakashimacommented, Oct 21, 2016

@jacobmarshall thanks, that’s a great summary of some of the nuances.

0reactions
jmshalcommented, Oct 21, 2016

The problem is if the Bugsnag notifier doesn’t get a chance to inject itself into the various JavaScript functions, timers & event hooks before other code on the page utilises them, you end up losing the guarantee that if/when an error occurs that it will be picked up by the notifier and sent to Bugsnag.

As @eanakashima is suggesting, if you need the notifier loaded async, it needs to still be invoked before any other code loads. Bundling the notifier with your own code is the best (and easiest) way to do this.

I wouldn’t say this goes against the web best practices. Much like you may decide to depend upon a version of jQuery hosted on a CDN within your project, it needs to load before any of your code does.

Additionally, there should be no issues using the notifier with the defer attribute attached to the script element, as long as all your script tags have it as well. I’ve done this on previous projects which didn’t use code bundling (ie. webpack or browserify) with no problems.

<script
    src="//d2wy8f7a9ursnm.cloudfront.net/bugsnag-3.min.js"
    data-apikey="YOUR-API-KEY-HERE"
    defer>
</script>
<script src="/bundle.js" defer></script>

I think it’s also important to remember that Bugsnag is only really totally beneficial if it able to capture any errors that occur during any part your web app/website’s lifecycle. For instance, if you deployed a change which accidentally included a typo right at the beginning of your code bundle, which subsequently caused your app to fail to load entirely, there is no way Bugsnag would be able to do anything if your code asynchronously loaded Bugsnag somewhere after that typo.

You should be able to rely upon Bugsnag for notifying you of all errors that occur, and loading async does not offer that. Which is super important especially if you do rapid deployments.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to focus on errors originating from your codebase and ...
Segmenting errors in the application based on code ownership, and then fine-tuning alerts and workflow integrations can help drive team ...
Read more >
README — Documentation for bugsnag (3.0.0) - RubyDoc.info
Bugsnag captures errors in real-time from your web, mobile and desktop applications, helping you to understand and resolve them as fast as possible....
Read more >
Loading chunk 77 failed due to timeout but Error reported on ...
I had a similar issue (if not the same) some time ago which was seen in the cases where a user is active(in...
Read more >
Track Errors in Ruby on Rails Application with Sentry
Most popular ones are Rollbar, Sentry, Bugsnag etc. ... So, we will start with a very basic setup and that's as simple as,...
Read more >
Tenon.io Documentation | Changelog
2018 saw the resolution of 575 user stories, improvements, and bugs in Tenon. ... Bug TEN-4074 Result is not displaying the number of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found