2022-12-19 09:51:56 +03:00
|
|
|
import { CacheableResponsePlugin } from 'workbox-cacheable-response';
|
|
|
|
import { ExpirationPlugin } from 'workbox-expiration';
|
|
|
|
import { RegExpRoute, registerRoute, Route } from 'workbox-routing';
|
2023-01-10 12:08:10 +03:00
|
|
|
import {
|
|
|
|
CacheFirst,
|
|
|
|
NetworkFirst,
|
|
|
|
StaleWhileRevalidate,
|
|
|
|
} from 'workbox-strategies';
|
2022-12-19 09:51:56 +03:00
|
|
|
|
2023-01-02 09:22:31 +03:00
|
|
|
self.__WB_DISABLE_DEV_LOGS = true;
|
|
|
|
|
2022-12-19 09:51:56 +03:00
|
|
|
const imageRoute = new Route(
|
|
|
|
({ request, sameOrigin }) => {
|
2022-12-20 08:21:08 +03:00
|
|
|
const isRemote = !sameOrigin;
|
|
|
|
const isImage = request.destination === 'image';
|
|
|
|
const isAvatar = request.url.includes('/avatars/');
|
|
|
|
const isEmoji = request.url.includes('/emoji/');
|
|
|
|
return isRemote && isImage && (isAvatar || isEmoji);
|
2022-12-19 09:51:56 +03:00
|
|
|
},
|
|
|
|
new CacheFirst({
|
|
|
|
cacheName: 'remote-images',
|
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
2022-12-20 08:21:08 +03:00
|
|
|
maxEntries: 100,
|
2022-12-19 09:51:56 +03:00
|
|
|
maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
|
|
|
|
purgeOnQuotaError: true,
|
|
|
|
}),
|
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [0, 200],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
registerRoute(imageRoute);
|
|
|
|
|
2023-02-23 15:54:23 +03:00
|
|
|
// 1-day cache for
|
|
|
|
// - /api/v1/instance
|
|
|
|
// - /api/v1/custom_emojis
|
|
|
|
// - /api/v1/preferences
|
|
|
|
// - /api/v1/lists/:id
|
2022-12-19 09:51:56 +03:00
|
|
|
const apiExtendedRoute = new RegExpRoute(
|
2023-02-23 15:54:23 +03:00
|
|
|
/^https?:\/\/[^\/]+\/api\/v\d+\/(instance|custom_emojis|preferences|lists\/\d+)/,
|
2022-12-19 09:51:56 +03:00
|
|
|
new StaleWhileRevalidate({
|
|
|
|
cacheName: 'api-extended',
|
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
|
|
|
maxAgeSeconds: 24 * 60 * 60, // 1 day
|
|
|
|
}),
|
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [0, 200],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
registerRoute(apiExtendedRoute);
|
|
|
|
|
2023-01-02 09:23:00 +03:00
|
|
|
const apiRoute = new RegExpRoute(
|
|
|
|
// Matches:
|
|
|
|
// - statuses/:id/context - some contexts are really huge
|
|
|
|
/^https?:\/\/[^\/]+\/api\/v\d+\/(statuses\/\d+\/context)/,
|
2023-01-09 20:28:52 +03:00
|
|
|
new NetworkFirst({
|
2023-01-02 09:23:00 +03:00
|
|
|
cacheName: 'api',
|
2023-01-09 20:28:52 +03:00
|
|
|
networkTimeoutSeconds: 5,
|
2023-01-02 09:23:00 +03:00
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
|
|
|
maxAgeSeconds: 5 * 60, // 5 minutes
|
|
|
|
}),
|
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [0, 200],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
registerRoute(apiRoute);
|