New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
createScript / fetch / shouldFetch hooks #2058
Conversation
This also fixes up the MIME checks in module types to properly enforce full MIME validation too. |
function windowErrorListener(evt) { | ||
if (evt.filename === url) | ||
err = evt.error; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've also refactored the global error listener.
}); | ||
} | ||
else { | ||
throw new Error('Unknown module type "' + contentType + '"'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With strict MIME checking, this now catches all unknown MIMEs, including the HTML error.
@@ -10,39 +10,40 @@ systemJSPrototype.register = function (deps, declare) { | |||
systemRegister.call(this, deps, declare); | |||
}; | |||
|
|||
systemJSPrototype.createScript = function (url) { | |||
const script = document.createElement('script'); | |||
script.charset = 'utf-8'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it save bytes to do Object.assign(script, {charset: 'utf-8', ...})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No Object.assign in IE11 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uncaught SyntaxError: Unexpected token export when import ES6 module
es6.js file
export var demo = "hello";
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @appsou, could you clarify what your comment means? Are you reporting a bug with a current version of systemjs? Or are you reporting a problem with this PR?
Note that systemjs does not support loading ES modules, as explained here and here. It primarily supports loading System.register
modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
For what it's worth, I've wondered about adding integrity to the scripts that systemjs creates. Haven't necessarily needed it, but I wondered about how and if I could do it :) |
Is anything blocking this PR? |
@joeldenning what are your thoughts here? |
|
This doesn't just apply to module types though - it also permits an alternative to the transform loader model, and enables features such as custom fetch protocols (eg if someone wanted to implement the ability to fetch over RPC / with authorization etc). To be honest those features are the more useful part of this PR than its effect on module types architecture, although for the module types architecture it does also handle #2086 as per #2086 (comment). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough - in that case I think these are good changes.
This feature would be helpful for me - I'm currently experiencing an issue where the browser isn't sending necessary cookies when requesting scripts, and it would be solved if I could set |
Is there anything blocking the merge? |
No - just forgot to merge it. I'll merge master in and then merge this in. |
I'll publish this as part of 6.2.0 once #2114 is reviewed and merged. |
do we have any examples of using this
What should I do here to add the auth header? |
Hi @jsoriao - we discussed this in the single-spa slack workspace at https://single-spa.slack.com/archives/C8R6U7MT7/p1586336163337900. You cannot configure arbitrary headers to be sent with script tags, but you can tell the browser to automatically send the |
How would I send a JWT token when fetching a script? That's not clear to me. const applications = constructApplications({
routes,
loadApp({ name }) {
// return System.import(name);
// what do I do here instead of System.import?
},
}); |
@sscaff1 to tell SystemJS to use |
This implements three new hooks to open up the loader a little more. The docs contains the full explanation, but the gist is:
System.shouldFetch()
, and then does afetch
based on that check. This allows eg setting up CSS modules for files not ending in .css, or ensuring all modules are fetched.fetch
,System.fetch
is a global fetch alias, allowing the fetch function itself to be hooked. Effectively opening up to any custom transforms etc as an alternative to the transform hook.createScript
hooking allows hooking the script creation. This can be useful to inject custom authentication / integrity etc.The hope is the above enables new types of userland plugins. The reality is no one will likely notice this PR even landed until one person in 6 months time decides they want the feature, and are grateful to find it. But hey I had a fun Sunday afternoon celebrating 10k stars.