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.

Toolbar introduces race condition on requireJS sites

See original GitHub issue

On version 1.2.1, do not know for master nor older releases. Opening an issue so other people with the same problem can find this. It turns out google was not my friend on that one.

Root cause: In file debug_toolbar/templates/debug_toolbar/base.html (the one that gets injected into site’s pages), lines 8 to 10, the code hijacks window.define and restores it later.

However, as it is a global and requireJS does aynchronous loading, a whole bunch of funny stuff can happen randomly.

Some effects: Here is what I experienced until I eventually found out debug_toolbar was the culprit:

  • some other module gets ready and tries to call define at this very time, breaking with a define is not defined, aborting module load.
  • more fun: the main jQuery completing its loading asynchronously, registering itself as window.jQuery only to have debug toolbar call noConflict on it, before requireJS has time to grab the reference. Therefore, everything appears to load fine, except module that use jQuery will get undefined instead of jQuery.

Workaround: As a workaround, it is possible to set JQUERY_URL to None in the settings. This forces debug_toolbar to use the main jquery and not try to load its own. However, doing so requires that the main jQuery be loaded before the toolbar kicks in so either:

  • load jQuery synchronously
  • or wrap the whole scripting part of debug_toolbar in a require(['jquery'], function(jQuery){ }) block, then wrap toolbar’s script in define blocks. That’s messy though, I would not recommend it.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
spectrascommented, Dec 31, 2014

I found another approach that seems to work fine for me. That would probably require some kind of setting or something. I replaced all the script loading in base.html with this:

<script>
require(['jquery'], function ($) {
    window.djdt = {jQuery: $};
    require(['{% static "debug_toolbar/js/toolbar.js" %}']);
});
</script>

That makes the loading of toolbar.js get deferred until jQuery has been loaded through AMD, and injects the jQuery reference into djdt, where the toolbar expects it.

1reaction
NotSqrtcommented, Dec 4, 2014

I confirm that the following works :

  • set JQUERY_URL to None in toolbar settings
  • load jQuery before requireJS
{% if debug %}<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>{% endif %}
Read more comments on GitHub >

github_iconTop Results From Across the Web

requirejs race condition - how to block until previous file ...
requirejs race condition - how to block until previous file retrieved · My suspicion on this failure is that there is some sort...
Read more >
RequireJS API
RequireJS waits for all dependencies to load, figures out the right order in which to call the functions that define the modules, then...
Read more >
Magento Open Source 2.4.5 release notes | Adobe Commerce
Magento Open Source introduces improvements to platform quality, ... Race conditions no longer interrupt the creation of the contentUpdated event listener.
Read more >
T156346 VE plugins from user scripts suffer from race condition
VE continues initialization (assuming no other plugins aren't ready yet), which will initialize the toolbar. The init function is executed, but it's too...
Read more >
mocha-qunit-ui | Yarn - Package Manager
... Move checkboxes into toolbar and give them labels and descriptions (as ... (it'll set the timeout when necessary), fixes timing race conditions....
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