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.

Request: allow override to retrieve auto-imported script element

See original GitHub issue

Description of Proposed Feature

Auto-import is a great feature we rely on to load our app inlined. However, in some cases, the retrieval of the current running script will not be correct, as the last script might not always be the running script. (https://github.com/systemjs/systemjs/blob/master/src/features/script-load.js#L40)

systemJSPrototype.register = function (deps, declare) {
  if (hasDocument && document.readyState === 'loading' && typeof deps !== 'string') {
    var scripts = document.getElementsByTagName('script');
    var lastScript = scripts[scripts.length - 1]; // not always the running script
    var url = lastScript && lastScript.src;
    if (url) {
      lastAutoImportUrl = url;

This proposal would extract the retrieval of the current script into a separate, overridable, hook:

systemJSPrototype.getCurrentScript = function () {
  var scripts = document.getElementsByTagName('script');
  return scripts[scripts.length - 1];
};

systemJSPrototype.register = function (deps, declare) {
  if (hasDocument && document.readyState === 'loading' && typeof deps !== 'string') {
    var lastScript = systemJSPrototype.getCurrentScript();
    var url = lastScript && lastScript.src;
    if (url) {
      lastAutoImportUrl = url;

In what way would you use it?

In the end state, when we drop support for IE 11 in our app, we would use document.currentScript in order to always have the correct current script:

systemJSPrototype.getCurrentScript = function () {
  return document.currentScript;
};

In the meantime, we would use the hook to return the correct script element dynamically. It would also allow us to prevent the import of incorrectly matched last scripts when dynamic loads have been used.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
tmsnscommented, Sep 7, 2020

I was finally able to reproduce it! 😃 https://plnkr.co/edit/dMPuci6LJh0smbyu image

Some remarks about the repro:

  • the document.write in index.html is used in order to fake a sync third party library, but I wanted to use a file in the same project for it (animals/chicken.js)
  • in order to micic the slower call I used flash.siwalik.in (a https variant of Slowwly)
  • the core of the issue lies in the retrieval of the dependencies of a dynamic import (eg. triton.js). These dependencies are retrieved with a url that is constructed using the wrong base url. That wrong base url is coming from document.getElementsByTagName('scripts') which retrieves the last script instead of the current script.
  • notice that nothing is wrong on the page. In most cases the wrong url will not resolve (if it does not exist). Users do get errors though.

Let me know if can help more!

0reactions
guybedfordcommented, Sep 13, 2020

@tmsns this fix was released in 6.6.0 and 6.6.1, just let me know if you hit any further issues here or need to reopen.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How can I access built-in methods of an Object that ...
I'm looking for a way to somehow restore the reference to the method in my userscript. The problem is that the website can...
Read more >
The Script element - HTML: HyperText Markup Language
Provides a hint of the relative priority to use when fetching an external script. Allowed values: high. Signals a high-priority fetch relative ...
Read more >
Documentation - TypeScript 4.0
Labeled Tuple Elements. Improving the experience around tuple types and parameter lists is important because it allows us to get strongly typed validation ......
Read more >
Announcing TypeScript 4.0 - Microsoft Developer Blogs
Today we are thrilled to announce the availability of TypeScript 4.0! This version of the language represents our next generation of ...
Read more >
sbt Reference Manual — Plugins
Managed dependencies declared by the project/ project are retrieved and are ... We call these triggered plugins, and they are created by overriding...
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