소스 검색

Cache HTML page with Service Worker (#6802)

This is the first step to make Mastodon work offline. It is also required
by Chromium to trigger Web Manifest automated install prompt.
master
Akihiko Odaki 6 년 전
committed by Eugen Rochko
부모
커밋
f0cd957c7a
1개의 변경된 파일21개의 추가작업 그리고 1개의 파일을 삭제
  1. +21
    -1
      app/javascript/mastodon/service_worker/entry.js

+ 21
- 1
app/javascript/mastodon/service_worker/entry.js 파일 보기

@@ -1,10 +1,30 @@
import './web_push_notifications';

function fetchRoot() {
return fetch('/', { credentials: 'include' });
}

// Cause a new version of a registered Service Worker to replace an existing one
// that is already installed, and replace the currently active worker on open pages.
self.addEventListener('install', function(event) {
event.waitUntil(self.skipWaiting());
const promises = Promise.all([caches.open('mastodon-web'), fetchRoot()]);
const asyncAdd = promises.then(([cache, root]) => cache.put('/', root));

event.waitUntil(asyncAdd);
});
self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', function(event) {
const url = new URL(event.request.url);

if (url.pathname.startsWith('/web/')) {
event.respondWith(fetchRoot().then(response => {
if (response.ok) {
return response;
}

throw null;
}).catch(() => caches.match('/')));
}
});

불러오는 중...
취소
저장