Merge pull request #4192 from overleaf/jpa-clear-state

[misc] serviceWorker: clear client state

GitOrigin-RevId: c5b48c20e8482c1a673d15fe65d5b73ad1e00e43
This commit is contained in:
Jakob Ackermann 2021-06-14 10:11:52 +02:00 committed by Copybot
parent c12fc1293d
commit 25d3231ce7

View file

@ -5,6 +5,7 @@ const OError = require('@overleaf/o-error')
// adjusting metrics collection.
const VERSION = 2
const CLEAR_CACHE_REQUEST_MATCHER = /^\/project\/[0-9a-f]{24}\/output$/
const COMPILE_REQUEST_MATCHER = /^\/project\/[0-9a-f]{24}\/compile$/
const PDF_REQUEST_MATCHER = /^\/project\/[0-9a-f]{24}\/.*\/output.pdf$/
const PDF_JS_CHUNK_SIZE = 128 * 1024
@ -56,9 +57,14 @@ function getClientContext(clientId) {
*/
function registerPdfContext(clientId, path, pdfContext) {
const clientContext = getClientContext(clientId)
const { pdfs, metrics, cached } = clientContext
const { pdfs, metrics, cached, clsiServerId } = clientContext
pdfContext.metrics = metrics
pdfContext.cached = cached
if (pdfContext.clsiServerId !== clsiServerId) {
// VM changed, this invalidates all browser caches.
clientContext.clsiServerId = pdfContext.clsiServerId
cached.clear()
}
// we only need to keep the last 3 contexts
for (const key of pdfs.keys()) {
if (pdfs.size < 3) {
@ -162,9 +168,24 @@ function onFetch(event) {
}
}
if (
event.request.method === 'DELETE' &&
path.match(CLEAR_CACHE_REQUEST_MATCHER)
) {
return processClearCacheRequest(event)
}
// other request, ignore
}
/**
* @param {FetchEvent} event
*/
function processClearCacheRequest(event) {
CLIENT_CONTEXT.delete(event.clientId)
// use default request proxy.
}
/**
* @param {FetchEvent} event
*/