Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit 754e35b

Browse files
committed
Add support for new async fetch API
This commit adds a fetchTextFromURL strategy using the new fetch API required for service workers. #374 It can be used in a service worker like this: ``` importScripts('babel-browser.js') importScripts('es6-module-loader-dev.js') self.addEventListener('install', function(event) { event.waitUntil(System.import('sw.jsx').then(function(sw) { return sw.install(event); })); }); // [..] ```
1 parent 83f6984 commit 754e35b

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/system-fetch.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
}
6060
};
6161
}
62-
else if (typeof require != 'undefined') {
62+
else if (typeof require != 'undefined' && typeof process != 'undefined') {
6363
var fs;
6464
fetchTextFromURL = function(url, authorization, fulfill, reject) {
6565
if (url.substr(0, 8) != 'file:///')
@@ -84,6 +84,29 @@
8484
});
8585
};
8686
}
87+
else if (typeof self != 'undefined' && typeof self.fetch != 'undefined') {
88+
fetchTextFromURL = function(url, authorization, fulfill, reject) {
89+
var opts = {
90+
headers: {'Accept': 'application/x-es-module, */*'}
91+
};
92+
93+
if (authorization) {
94+
if (typeof authorization == 'string')
95+
opts.headers['Authorization'] = authorization;
96+
opts.credentials = 'include';
97+
}
98+
99+
fetch(url, opts)
100+
.then(function (r) {
101+
if (r.ok) {
102+
return r.text();
103+
} else {
104+
throw new Error('Fetch error: ' + r.status + ' ' + r.statusText);
105+
}
106+
})
107+
.then(fulfill, reject);
108+
}
109+
}
87110
else {
88111
throw new TypeError('No environment fetch API available.');
89112
}

0 commit comments

Comments
 (0)