2018-12-11 16:31:48 +01:00
|
|
|
import {
|
|
|
|
assets as __assets__,
|
|
|
|
shell as __shell__,
|
|
|
|
routes as __routes__
|
|
|
|
} from '../__sapper__/service-worker.js'
|
2021-07-05 05:19:04 +02:00
|
|
|
import { get, post } from './routes/_utils/ajax.js'
|
|
|
|
import { setWebShareData, closeKeyValIDBConnection } from './routes/_database/webShare.js'
|
2018-12-16 02:13:46 +01:00
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
const timestamp = process.env.SAPPER_TIMESTAMP
|
2018-03-26 06:31:40 +02:00
|
|
|
const ASSETS = `assets_${timestamp}`
|
2018-04-01 02:26:10 +02:00
|
|
|
const WEBPACK_ASSETS = `webpack_assets_${timestamp}`
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2019-10-23 05:45:30 +02:00
|
|
|
const ON_DEMAND_CACHE = [
|
|
|
|
{
|
|
|
|
regex: /tesseract-core\.wasm/,
|
|
|
|
cache: WEBPACK_ASSETS
|
|
|
|
},
|
|
|
|
{
|
|
|
|
regex: /traineddata\.gz/,
|
|
|
|
cache: ASSETS
|
2021-02-16 01:47:18 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
regex: /\$polyfill\$/,
|
|
|
|
cache: WEBPACK_ASSETS
|
2022-03-28 05:59:02 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
regex: /TwemojiCountryFlags\.woff2/,
|
|
|
|
cache: ASSETS
|
2022-04-10 19:34:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
regex: /_splash\.png$/,
|
|
|
|
cache: ASSETS
|
2019-10-23 05:45:30 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
// `static` is an array of everything in the `static` directory
|
2018-03-26 06:31:40 +02:00
|
|
|
const assets = __assets__
|
|
|
|
.map(file => file.startsWith('/') ? file : `/${file}`)
|
2018-12-08 20:21:54 +01:00
|
|
|
.filter(filename => !filename.endsWith('.map'))
|
2018-12-16 18:35:02 +01:00
|
|
|
.filter(filename => filename !== '/robots.txt')
|
2019-10-23 05:45:30 +02:00
|
|
|
.filter(filename => !filename.includes('traineddata.gz')) // cache on-demand
|
2019-09-24 09:50:42 +02:00
|
|
|
.filter(filename => !filename.endsWith('.webapp')) // KaiOS manifest
|
2020-12-19 05:02:36 +01:00
|
|
|
.filter(filename => !/emoji-.*?\.json$/.test(filename)) // useless to cache; it already goes in IndexedDB
|
2021-03-14 21:39:11 +01:00
|
|
|
.filter(filename => !/screenshot-.*?\.png/.test(filename)) // only used during PWA installation, don't bother caching
|
2022-03-28 05:59:02 +02:00
|
|
|
.filter(filename => !/TwemojiCountryFlags\.woff2/.test(filename)) // cache on-demand
|
2022-04-10 19:34:56 +02:00
|
|
|
.filter(filename => !/_splash\.png$/.test(filename)) // only used for iOS PWA splash screen, cache on-demand
|
2018-03-26 06:31:40 +02:00
|
|
|
|
|
|
|
// `shell` is an array of all the files generated by webpack
|
|
|
|
// also contains '/index.html' for some reason
|
2018-04-01 02:26:10 +02:00
|
|
|
const webpackAssets = __shell__
|
2018-12-16 02:13:46 +01:00
|
|
|
.filter(filename => !filename.endsWith('.map')) // don't bother with sourcemaps
|
2019-08-26 03:33:44 +02:00
|
|
|
.filter(filename => !filename.includes('tesseract-core.wasm')) // cache on-demand
|
2020-09-20 23:31:15 +02:00
|
|
|
.filter(filename => !filename.includes('LICENSE')) // don't bother with license files
|
2021-02-16 01:47:18 +01:00
|
|
|
.filter(filename => !filename.includes('$polyfill$')) // polyfills are cached dynamically
|
2018-03-26 06:31:40 +02:00
|
|
|
|
2018-01-07 00:51:25 +01:00
|
|
|
// `routes` is an array of `{ pattern: RegExp }` objects that
|
2018-12-11 16:31:48 +01:00
|
|
|
// match the pages in your src
|
2018-01-14 23:54:26 +01:00
|
|
|
const routes = __routes__
|
2018-01-07 00:51:25 +01:00
|
|
|
|
|
|
|
self.addEventListener('install', event => {
|
2018-03-23 06:30:48 +01:00
|
|
|
event.waitUntil((async () => {
|
2018-04-01 02:26:10 +02:00
|
|
|
await Promise.all([
|
|
|
|
caches.open(WEBPACK_ASSETS).then(cache => cache.addAll(webpackAssets)),
|
|
|
|
caches.open(ASSETS).then(cache => cache.addAll(assets))
|
|
|
|
])
|
2019-06-01 22:07:38 +02:00
|
|
|
// We shouldn't have to do this, but the previous page could be an old one,
|
|
|
|
// which would not send us a postMessage to skipWaiting().
|
|
|
|
// See https://github.com/nolanlawson/pinafore/issues/1243
|
2019-06-01 21:17:12 +02:00
|
|
|
self.skipWaiting()
|
2018-03-15 08:03:22 +01:00
|
|
|
})())
|
2018-01-14 23:54:26 +01:00
|
|
|
})
|
2018-01-07 00:51:25 +01:00
|
|
|
|
|
|
|
self.addEventListener('activate', event => {
|
2018-03-23 06:30:48 +01:00
|
|
|
event.waitUntil((async () => {
|
2019-08-03 22:49:37 +02:00
|
|
|
const keys = await caches.keys()
|
2018-03-26 06:31:40 +02:00
|
|
|
|
|
|
|
// delete old asset/ondemand caches
|
2019-08-03 22:49:37 +02:00
|
|
|
for (const key of keys) {
|
2018-04-01 02:26:10 +02:00
|
|
|
if (key !== ASSETS &&
|
2019-05-26 00:20:09 +02:00
|
|
|
!key.startsWith('webpack_assets_')) {
|
2018-03-15 08:03:22 +01:00
|
|
|
await caches.delete(key)
|
2018-01-14 23:54:26 +01:00
|
|
|
}
|
2018-03-15 08:03:22 +01:00
|
|
|
}
|
2018-03-26 06:31:40 +02:00
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
// for webpack static, keep the two latest builds because we may need
|
2018-04-01 02:26:10 +02:00
|
|
|
// them when the service worker has installed but the page has not
|
|
|
|
// yet reloaded (e.g. when it gives the toast saying "please reload"
|
|
|
|
// but then you don't refresh and instead load an async chunk)
|
2019-08-03 22:49:37 +02:00
|
|
|
const webpackKeysToDelete = keys
|
2018-04-01 02:26:10 +02:00
|
|
|
.filter(key => key.startsWith('webpack_assets_'))
|
|
|
|
.sort((a, b) => {
|
2019-08-03 22:49:37 +02:00
|
|
|
const aTimestamp = parseInt(a.substring(15), 10)
|
|
|
|
const bTimestamp = parseInt(b.substring(15), 10)
|
2018-04-01 02:26:10 +02:00
|
|
|
return bTimestamp < aTimestamp ? -1 : 1
|
|
|
|
})
|
|
|
|
.slice(2)
|
|
|
|
|
2019-08-03 22:49:37 +02:00
|
|
|
for (const key of webpackKeysToDelete) {
|
2018-04-01 02:26:10 +02:00
|
|
|
await caches.delete(key)
|
|
|
|
}
|
|
|
|
|
2018-03-15 08:03:22 +01:00
|
|
|
await self.clients.claim()
|
|
|
|
})())
|
2018-01-14 23:54:26 +01:00
|
|
|
})
|
2018-01-07 00:51:25 +01:00
|
|
|
|
|
|
|
self.addEventListener('fetch', event => {
|
2018-01-15 02:13:42 +01:00
|
|
|
const req = event.request
|
|
|
|
const url = new URL(req.url)
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-01-14 23:54:26 +01:00
|
|
|
// don't try to handle e.g. data: URIs
|
|
|
|
if (!url.protocol.startsWith('http')) {
|
2018-02-09 07:29:29 +01:00
|
|
|
return
|
2018-01-14 23:54:26 +01:00
|
|
|
}
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-03-23 06:30:48 +01:00
|
|
|
event.respondWith((async () => {
|
2019-08-03 22:49:37 +02:00
|
|
|
const sameOrigin = url.origin === self.origin
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-04-05 07:52:04 +02:00
|
|
|
if (sameOrigin) {
|
2021-03-14 18:20:23 +01:00
|
|
|
if (req.method === 'POST' && url.pathname === '/share') {
|
|
|
|
// handle Web Share Target requests (see manifest.json)
|
|
|
|
const formData = await req.formData()
|
|
|
|
const title = formData.get('title')
|
|
|
|
const text = formData.get('text')
|
|
|
|
const url = formData.get('url')
|
|
|
|
const file = formData.get('file')
|
|
|
|
await setWebShareData({ title, text, url, file })
|
|
|
|
await closeKeyValIDBConnection() // don't need to keep the IDB connection open
|
2021-03-18 15:00:48 +01:00
|
|
|
return Response.redirect(
|
2021-03-21 21:49:59 +01:00
|
|
|
'/?pwa=true&compose=true', // pwa=true because this can only be invoked from a PWA
|
2021-03-18 15:00:48 +01:00
|
|
|
303 // 303 recommended by https://web.dev/web-share-target/
|
|
|
|
)
|
2021-03-14 18:20:23 +01:00
|
|
|
}
|
|
|
|
|
2018-04-05 07:52:04 +02:00
|
|
|
// always serve webpack-generated resources and
|
2018-12-11 16:31:48 +01:00
|
|
|
// static from the cache if possible
|
2019-08-03 22:49:37 +02:00
|
|
|
const response = await caches.match(req)
|
2018-04-05 07:52:04 +02:00
|
|
|
if (response) {
|
|
|
|
return response
|
|
|
|
}
|
2019-08-26 03:33:44 +02:00
|
|
|
|
2019-10-23 05:45:30 +02:00
|
|
|
for (const { regex, cache } of ON_DEMAND_CACHE) {
|
|
|
|
if (regex.test(url.pathname)) {
|
|
|
|
// cache this on-demand
|
|
|
|
const response = await fetch(req)
|
|
|
|
if (response && response.status >= 200 && response.status < 300) {
|
|
|
|
const clonedResponse = response.clone()
|
|
|
|
/* no await */
|
|
|
|
caches.open(cache).then(cache => cache.put(req, clonedResponse))
|
|
|
|
}
|
|
|
|
return response
|
2019-08-26 03:33:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
// for routes, serve the /service-worker-index.html file from the most recent
|
|
|
|
// static cache
|
2018-04-05 07:52:04 +02:00
|
|
|
if (routes.find(route => route.pattern.test(url.pathname))) {
|
2019-08-03 22:49:37 +02:00
|
|
|
const response = await caches.match('/service-worker-index.html')
|
2018-08-24 17:51:09 +02:00
|
|
|
if (response) {
|
|
|
|
return response
|
|
|
|
}
|
2018-04-05 07:52:04 +02:00
|
|
|
}
|
2018-03-15 08:03:22 +01:00
|
|
|
}
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-03-15 08:03:22 +01:00
|
|
|
// for everything else, go network-only
|
|
|
|
return fetch(req)
|
|
|
|
})())
|
2018-01-14 23:54:26 +01:00
|
|
|
})
|
2018-10-06 22:06:10 +02:00
|
|
|
|
|
|
|
self.addEventListener('push', event => {
|
|
|
|
event.waitUntil((async () => {
|
|
|
|
const data = event.data.json()
|
2019-12-08 18:56:23 +01:00
|
|
|
const { origin } = event.target
|
2018-10-06 22:06:10 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const notification = await get(`${origin}/api/v1/notifications/${data.notification_id}`, {
|
2019-08-03 22:49:37 +02:00
|
|
|
Authorization: `Bearer ${data.access_token}`
|
2018-10-06 22:06:10 +02:00
|
|
|
}, { timeout: 2000 })
|
|
|
|
|
|
|
|
await showRichNotification(data, notification)
|
|
|
|
} catch (e) {
|
|
|
|
await showSimpleNotification(data)
|
|
|
|
}
|
|
|
|
})())
|
|
|
|
})
|
|
|
|
|
|
|
|
async function showSimpleNotification (data) {
|
|
|
|
await self.registration.showNotification(data.title, {
|
|
|
|
icon: data.icon,
|
2019-08-08 05:38:38 +02:00
|
|
|
body: data.body,
|
|
|
|
data: {
|
|
|
|
url: `${self.origin}/notifications`
|
|
|
|
}
|
2018-10-06 22:06:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function showRichNotification (data, notification) {
|
2019-05-26 00:20:09 +02:00
|
|
|
const { icon, body } = data
|
|
|
|
const tag = notification.id
|
|
|
|
const { origin } = self.location
|
2019-05-27 23:25:45 +02:00
|
|
|
const badge = '/icon-push-badge.png'
|
2018-10-06 22:06:10 +02:00
|
|
|
|
|
|
|
switch (notification.type) {
|
|
|
|
case 'follow': {
|
|
|
|
await self.registration.showNotification(data.title, {
|
2019-05-27 23:25:45 +02:00
|
|
|
badge,
|
2019-05-26 00:20:09 +02:00
|
|
|
icon,
|
|
|
|
body,
|
|
|
|
tag,
|
2018-10-06 22:06:10 +02:00
|
|
|
data: {
|
2019-05-26 00:20:09 +02:00
|
|
|
url: `${origin}/accounts/${notification.account.id}`
|
2018-10-06 22:06:10 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
2019-05-26 00:20:09 +02:00
|
|
|
case 'reblog':
|
|
|
|
case 'favourite':
|
2022-04-28 17:18:50 +02:00
|
|
|
case 'status':
|
2019-08-20 04:08:59 +02:00
|
|
|
case 'poll': {
|
2019-05-26 00:20:09 +02:00
|
|
|
await self.registration.showNotification(data.title, {
|
2019-05-27 23:25:45 +02:00
|
|
|
badge,
|
2019-05-26 00:20:09 +02:00
|
|
|
icon,
|
|
|
|
body,
|
|
|
|
tag,
|
|
|
|
data: {
|
|
|
|
url: `${origin}/statuses/${notification.status.id}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
break
|
2019-08-20 04:08:59 +02:00
|
|
|
}
|
|
|
|
case 'mention': {
|
2019-05-26 00:20:09 +02:00
|
|
|
const isPublic = ['public', 'unlisted'].includes(notification.status.visibility)
|
|
|
|
const actions = [
|
|
|
|
isPublic && {
|
2018-10-28 23:28:55 +01:00
|
|
|
action: 'reblog',
|
2019-05-27 23:01:02 +02:00
|
|
|
icon: '/icon-push-fa-retweet.png', // generated manually from font-awesome-svg
|
2020-11-29 23:13:27 +01:00
|
|
|
title: 'intl.reblog'
|
2019-05-27 23:01:02 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
action: 'favourite',
|
|
|
|
icon: '/icon-push-fa-star.png', // generated manually from font-awesome-svg
|
2020-11-29 23:13:27 +01:00
|
|
|
title: 'intl.favorite'
|
2019-05-26 00:20:09 +02:00
|
|
|
}
|
|
|
|
].filter(Boolean)
|
2018-10-28 23:28:55 +01:00
|
|
|
|
2018-10-06 22:06:10 +02:00
|
|
|
await self.registration.showNotification(data.title, {
|
2019-05-27 23:25:45 +02:00
|
|
|
badge,
|
2019-05-26 00:20:09 +02:00
|
|
|
icon,
|
|
|
|
body,
|
|
|
|
tag,
|
2018-10-06 22:06:10 +02:00
|
|
|
data: {
|
2019-05-26 00:20:09 +02:00
|
|
|
instance: new URL(data.icon).origin,
|
2018-10-06 22:06:10 +02:00
|
|
|
status_id: notification.status.id,
|
|
|
|
access_token: data.access_token,
|
2019-05-26 00:20:09 +02:00
|
|
|
url: `${origin}/statuses/${notification.status.id}`
|
2018-10-06 22:06:10 +02:00
|
|
|
},
|
|
|
|
actions
|
|
|
|
})
|
|
|
|
break
|
2019-08-20 04:08:59 +02:00
|
|
|
}
|
2018-10-06 22:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cloneNotification = notification => {
|
2019-05-26 00:20:09 +02:00
|
|
|
const clone = {}
|
2018-10-06 22:06:10 +02:00
|
|
|
|
2019-08-03 22:49:37 +02:00
|
|
|
for (const k in notification) {
|
2019-05-27 23:01:02 +02:00
|
|
|
// deliberately not doing a hasOwnProperty check, but skipping
|
|
|
|
// functions and null props like onclick and onshow and showTrigger
|
|
|
|
if (typeof notification[k] !== 'function' && notification[k] !== null) {
|
|
|
|
clone[k] = notification[k]
|
|
|
|
}
|
2018-10-06 22:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateNotificationWithoutAction = (notification, action) => {
|
|
|
|
const newNotification = cloneNotification(notification)
|
|
|
|
|
|
|
|
newNotification.actions = newNotification.actions.filter(item => item.action !== action)
|
|
|
|
|
|
|
|
return self.registration.showNotification(newNotification.title, newNotification)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.addEventListener('notificationclick', event => {
|
|
|
|
event.waitUntil((async () => {
|
|
|
|
switch (event.action) {
|
|
|
|
case 'reblog': {
|
2019-05-26 00:20:09 +02:00
|
|
|
const url = `${event.notification.data.instance}/api/v1/statuses/${event.notification.data.status_id}/reblog`
|
|
|
|
await post(url, null, {
|
2019-08-03 22:49:37 +02:00
|
|
|
Authorization: `Bearer ${event.notification.data.access_token}`
|
2019-05-26 00:20:09 +02:00
|
|
|
})
|
2018-10-06 22:06:10 +02:00
|
|
|
await updateNotificationWithoutAction(event.notification, 'reblog')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'favourite': {
|
2019-05-26 00:20:09 +02:00
|
|
|
const url = `${event.notification.data.instance}/api/v1/statuses/${event.notification.data.status_id}/favourite`
|
|
|
|
await post(url, null, {
|
2019-08-03 22:49:37 +02:00
|
|
|
Authorization: `Bearer ${event.notification.data.access_token}`
|
2019-05-26 00:20:09 +02:00
|
|
|
})
|
2018-10-06 22:06:10 +02:00
|
|
|
await updateNotificationWithoutAction(event.notification, 'favourite')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
await self.clients.openWindow(event.notification.data.url)
|
|
|
|
await event.notification.close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})())
|
|
|
|
})
|
2019-06-01 22:07:38 +02:00
|
|
|
|
|
|
|
self.addEventListener('message', (event) => {
|
|
|
|
switch (event.data) {
|
|
|
|
case 'skip-waiting':
|
|
|
|
self.skipWaiting()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|