aboutsummaryrefslogtreecommitdiff
path: root/infra/perfetto-ci.appspot.com/static/service_worker.js
diff options
context:
space:
mode:
Diffstat (limited to 'infra/perfetto-ci.appspot.com/static/service_worker.js')
-rw-r--r--infra/perfetto-ci.appspot.com/static/service_worker.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/infra/perfetto-ci.appspot.com/static/service_worker.js b/infra/perfetto-ci.appspot.com/static/service_worker.js
new file mode 100644
index 000000000..75403d8e0
--- /dev/null
+++ b/infra/perfetto-ci.appspot.com/static/service_worker.js
@@ -0,0 +1,56 @@
+/**
+ * Copyright (c) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you
+ * may not use this file except in compliance with the License. You may
+ * obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+'use strict';
+
+const CACHE_NAME = 'travis-cache';
+const JOBS_URL = 'https://api.travis-ci.org/jobs/';
+
+async function FetchAndCacheIfJob(event) {
+ if (!event.request.url.startsWith(JOBS_URL)) {
+ return fetch(event.request);
+ }
+
+ // Try and retrieve from the cache.
+ const cachedResponse = await caches.match(event.request);
+ if (cachedResponse) {
+ return cachedResponse;
+ }
+
+ // If network request failed just return the response.
+ const response = await fetch(event.request);
+ if (!response || response.status !== 200) {
+ return response;
+ }
+
+ // Extract the JSON from the response.
+ const json = await response.clone().json();
+ if (json.state !== 'cancelled' && json.state !== 'finished') {
+ return response;
+ }
+
+ var responseToCache = response.clone();
+ caches.open(CACHE_NAME)
+ .then(cache => {
+ cache.put(event.request, responseToCache);
+ });
+
+ return response;
+}
+
+self.addEventListener('fetch', event => {
+ event.respondWith(FetchAndCacheIfJob(event));
+});