Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache important API requests #216

Merged
merged 1 commit into from
May 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion web/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { clientsClaim } from 'workbox-core';
import { ExpirationPlugin } from 'workbox-expiration';
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { StaleWhileRevalidate } from 'workbox-strategies';
import {NetworkFirst, StaleWhileRevalidate} from 'workbox-strategies';

declare const self: ServiceWorkerGlobalScope;

Expand Down Expand Up @@ -82,6 +82,23 @@ registerRoute(
})
)

const apiEndpointsForCaching = new Set([
'/api/backends/info',
'/api/version'
]);

// Cache available Go versions from Go playground
registerRoute(
({url}) => (
url.origin === self.location.origin &&
apiEndpointsForCaching.has(url.pathname)
),
new NetworkFirst({
cacheName: 'api',
networkTimeoutSeconds: 15,
})
)

// This allows the web app to trigger skipWaiting via
// registration.waiting.postMessage({type: 'SKIP_WAITING'})
self.addEventListener('message', (event) => {
Expand Down