Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Installed web app: a sign-in page an older build cached under an exercise animation is not carried into the lasting media cache (#281)

Duarte Santos05d1d2c91650
diff --git a/frontend/public/sw.js b/frontend/public/sw.js
index 59ce57c1c..e99797aaf 100644
--- a/frontend/public/sw.js
+++ b/frontend/public/sw.js
@@ -88,6 +88,14 @@ self.addEventListener('activate', e => {
})())
})
+// Only a real answer is kept. A gated instance answers a lapsed session with 401, and an auth
+// proxy in front with its login page, a 200 of HTML after a redirect; stored under an image's
+// URL, that would stand in for the animation for good, since nothing sweeps this cache.
+const realMedia = res => {
+ const type = (res.headers && res.headers.get('content-type')) || ''
+ return res.ok && !res.redirected && !/text\/html/i.test(type)
+}
+
async function adoptMedia(name) {
const from = await caches.open(name)
const media = (await from.keys()).filter(r => { try { return isMediaPath(new URL(r.url || r, location.href).pathname) } catch { return false } })
@@ -95,8 +103,10 @@ async function adoptMedia(name) {
const to = await caches.open(MEDIA)
for (const r of media) {
if (await to.match(r)) continue
+ // A build before this one kept any ok answer, a login page included. It went with that
+ // build's cache; carried into this one, it would stay.
const res = await from.match(r)
- if (res) await to.put(r, res)
+ if (res && realMedia(res)) await to.put(r, res)
}
}
@@ -128,11 +138,7 @@ function media(e) {
return hit
}
return fetch(e.request).then(res => {
- // Only a real answer is kept. A gated instance answers a lapsed session with 401, and an
- // auth proxy in front with its login page, a 200 of HTML after a redirect; stored under an
- // image's URL, that would stand in for the animation for good.
- const type = (res.headers && res.headers.get('content-type')) || ''
- if (res.ok && !res.redirected && !/text\/html/i.test(type)) {
+ if (realMedia(res)) {
touched.add(e.request.url)
const copy = res.clone()
e.waitUntil(c.put(e.request, copy).then(() => { if (++mediaPuts >= MEDIA_TRIM_EVERY) { mediaPuts = 0; return trimMedia() } }).catch(() => {}))
diff --git a/frontend/src/lib/sw-media.test.js b/frontend/src/lib/sw-media.test.js
index 7953f1586..b97d1a226 100644
--- a/frontend/src/lib/sw-media.test.js
+++ b/frontend/src/lib/sw-media.test.js
@@ -95,6 +95,26 @@ describe('sw.js exercise media', () => {
expect(await (await w.get('/gif/0003.gif')).text()).toBe('old gif')
})
+ it('does not adopt a login page an older build had cached under an image\'s URL', async () => {
+ const w = worker()
+ const old = await w.caches.open('opengym-rt-oldbuild')
+ // v1.3.8 kept any ok answer, so a proxy's sign-in page could sit under a gif's URL. This
+ // small cache's clone() drops a `redirected` flag, so the page's own type is what is
+ // checked here.
+ await old.put(ORIGIN + '/gif/0004.gif', new Response('<html>sign in</html>', { headers: { 'content-type': 'text/html' } }))
+ await old.put(ORIGIN + '/gif/0005.gif', new Response('<html>sign in</html>', { headers: { 'content-type': 'Text/HTML; charset=utf-8' } }))
+ await old.put(ORIGIN + '/gif/0006.gif', new Response('real gif', { headers: { 'content-type': 'image/gif' } }))
+ const shell = await w.caches.open(BUILD)
+ await shell.put('index.html', new Response('<html></html>'))
+
+ await w.activate()
+ expect(w.media().urls()).toEqual([ORIGIN + '/gif/0006.gif'])
+ // With the old build gone, the next view with a network fetches the real animation.
+ await w.get('/gif/0005.gif')
+ expect(w.fetched).toEqual([ORIGIN + '/gif/0005.gif'])
+ expect(await (await w.caches.open(MEDIA)).match(ORIGIN + '/gif/0005.gif')).toBeTruthy()
+ })
+
it('an activate with no media anywhere creates no media cache', async () => {
const w = worker()
const shell = await w.caches.open(BUILD)