Commit 14420bf2 authored by Vincent Bechu's avatar Vincent Bechu

[erp5_officejs] Bootloader Service Worker, fetch from network when referrer is the scope

Allowed change cache to update application.
parent 8ab1add8
...@@ -25,6 +25,20 @@ var global = self, window = self; ...@@ -25,6 +25,20 @@ var global = self, window = self;
}); });
} }
function getFromLocal(relative_url) {
if (self.storage.get === undefined) {
self.storage = createStorage("ojs_source_code");
}
return self.storage.getAttachment(self.registration.scope, relative_url)
.push(function (blob) {
return new Response(blob, {
'headers': {
'content-type': blob.type
}
});
});
}
self.addEventListener('install', function (event) { self.addEventListener('install', function (event) {
event.waitUntil(self.skipWaiting()); event.waitUntil(self.skipWaiting());
}); });
...@@ -43,33 +57,41 @@ var global = self, window = self; ...@@ -43,33 +57,41 @@ var global = self, window = self;
event.respondWith(new Response(self.cache_list)); event.respondWith(new Response(self.cache_list));
return; return;
} }
event.respondWith( else if (event.request !== undefined && event.request.referrer === self.registration.scope) {
new self.RSVP.Queue() event.respondWith(
.push(function () { new self.RSVP.Queue()
if (self.storage.get === undefined) { .push(function () {
self.storage = createStorage("ojs_source_code"); return fetch(event.request);
} })
return self.storage.getAttachment(self.registration.scope, relative_url) .push(function (response) {
.push(function (blob) { if (response.status === 200) {
return new Response(blob, { return response;
'headers': { }
'content-type': blob.type return getFromLocal(relative_url);
})
.push(undefined, function (error) {
return new Response(error, {"statusText": error.message, "status": 500});
})
);
} else {
event.respondWith(
new self.RSVP.Queue()
.push(function () {
return getFromLocal(relative_url);
})
.push(undefined, function (error) {
if (error instanceof self.jIO.util.jIOError) {
if (relative_url.indexOf('http') === -1) {
if (self.cache_list.indexOf(relative_url) === -1) {
self.cache_list.push(relative_url);
} }
});
});
})
.push(undefined, function (error) {
if (error instanceof self.jIO.util.jIOError) {
if (relative_url.indexOf('http') === -1) {
if (self.cache_list.indexOf(relative_url) === -1) {
self.cache_list.push(relative_url);
} }
return fetch(event.request);
} }
return fetch(event.request); return new Response(error, {"statusText": error.message, "status": 500});
} })
return new Response(error, {"statusText": error.message, "status": 500}); );
}) }
);
}); });
}(self, fetch, Request, Response)); }(self, fetch, Request, Response));
\ No newline at end of file
...@@ -227,7 +227,7 @@ ...@@ -227,7 +227,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>961.10686.10882.5973</string> </value> <value> <string>961.30858.43041.57309</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -245,7 +245,7 @@ ...@@ -245,7 +245,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1502096517.05</float> <float>1502980874.99</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment