Skip to content

Commit

Permalink
Onerror Callback Support for Errors Fetching External Import Map (#2324)
Browse files Browse the repository at this point in the history
  • Loading branch information
kykwak committed Apr 28, 2021
1 parent f9b43bc commit f34cdc6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/import-maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ Any existing mappings are replaced, although in future this may be an error.

Previous versions of the import maps spec had support for multiple import maps in a single web page ([explanation](https://github.com/WICG/import-maps/issues/199)). SystemJS added support for multiple import maps during that time and has decided to keep support for multiple import maps as an experimental feature ([discussion](https://github.com/systemjs/systemjs/issues/2095)). Note that the Chrome implementation of import maps does not yet allow for multiple maps, and use of multiple import maps within SystemJS should be considered experimental and subject to change.

### Handling Import Map Errors

For handling errors when fetching external import maps (specifically for [SystemJS Warning #W4](https://github.com/systemjs/systemjs/blob/master/docs/errors.md#w4)), we can use the `onerror` attribute in the `<script type="systemjs-importmap">` tag.

In the `onerror` attribute, specify a script to execute when there is an error.

```html
<script type="systemjs-importmap" onerror="handleError()" src="unable-to-reach/importmap.json"></script>

<script type="text/javascript">
function handleError() {
// Put error handling/retry logic here.
}
</script>

<!-- Can also have inline definition for error handling script -->
<script type="systemjs-importmap" onerror='console.log("Running onerror script")' src="unable-to-reach/importmap.json"></script>
```

#### Spec and Implementation Feedback

Part of the benefit of giving users a working version of an early spec is being able to get real user feedback on the [import maps specification](https://github.com/wicg/import-maps/blob/master/spec.md).
Expand Down
3 changes: 3 additions & 0 deletions src/features/import-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ function processScripts () {
}).catch(function (err) {
err.message = errMsg('W4', process.env.SYSTEM_PRODUCTION ? script.src : 'Error fetching systemjs-import map ' + script.src) + '\n' + err.message;
console.warn(err);
if (typeof script.onerror === 'function') {
script.onerror();
}
return '{}';
}) : script.innerHTML;
importMapPromise = importMapPromise.then(function () {
Expand Down
6 changes: 6 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@

<script type="systemjs-importmap" src="https://hostname.invalid/importmap.json"></script>
<script type="systemjs-importmap" src="fixtures/browser/importmap-invalid.json"></script>
<script type="systemjs-importmap" onerror="retryImportMap()" src="fixtures/browser/importmap-invalid.json"></script>
<script type="text/javascript">
function retryImportMap() {
document.write('<scr'+'ipt type="systemjs-importmap" src="fixtures/browser/importmap-invalid.json"></sc'+'ript>');
}
</script>
<script type="systemjs-importmap" src="fixtures/browser/importmap.json" integrity="sha384-+t/f9i0vQYwxmcczlIpgCCTQz2mA1sMGYGErJ4fZb/Lcx/yYrFSiedO0XpSIX8oX"></script>
<script type="systemjs-module" src="/test/fixtures/browser/systemjs-module-early.js"></script>
<script src="../dist/system.js"></script>
Expand Down

0 comments on commit f34cdc6

Please sign in to comment.