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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Phone app: leaving a workout no longer sends a stray "left" beacon to https://localhost/api/activity; only the web app sends the beacon, and the phone's signal goes to its paired server as before
Duarte Santos7a69a49fce7f
diff --git a/frontend/src/lib/api-base.test.js b/frontend/src/lib/api-base.test.jsindex aa0dccba6..9944a1a4d 100644--- a/frontend/src/lib/api-base.test.js+++ b/frontend/src/lib/api-base.test.js@@ -2,8 +2,8 @@// Issue #238: openGym behind a reverse proxy that serves it under a subpath. The assets were// already relative; the API call was not, so it went to the proxy's own root where nothing// answers it. The base is read from where the app is being served.-import { describe, it, expect } from 'vitest'-import { appBase } from './api.js'+import { describe, it, expect, vi, afterEach } from 'vitest'+import { appBase, beacon } from './api.js'const at = pathname => appBase({ pathname })@@ -30,3 +30,23 @@ describe('the app knows where it is served from', () => {expect(at('')).toBe('/')})})++describe('the "left" beacon of the web app', () => {+ afterEach(() => { vi.unstubAllGlobals(); history.replaceState(null, '', '/') })++ it('goes to the API of the copy it was served from, as JSON', async () => {+ history.replaceState(null, '', '/myGym/')+ const sendBeacon = vi.fn(() => true)+ vi.stubGlobal('navigator', { ...navigator, sendBeacon })+ expect(beacon('/api/activity', { active: false })).toBe(true)+ const [url, blob] = sendBeacon.mock.calls[0]+ expect(url).toBe('/myGym/api/activity')+ expect(blob.type).toBe('application/json')+ expect(JSON.parse(await blob.text())).toEqual({ active: false })+ })++ it('a browser without sendBeacon just skips it', () => {+ vi.stubGlobal('navigator', { ...navigator, sendBeacon: undefined })+ expect(beacon('/api/activity', { active: false })).toBe(false)+ })+})diff --git a/frontend/src/lib/api.js b/frontend/src/lib/api.jsindex c9d7bfa9e..5f67c9dd5 100644--- a/frontend/src/lib/api.js+++ b/frontend/src/lib/api.js@@ -81,6 +81,19 @@ async function exchange(url, init) {return body}+// A "left" signal sent as the page goes: sendBeacon outlives a closing tab, where fetch may not.+// Only the web app sends one, to its own origin with its session cookie. A beacon carries no+// Authorization header, so a paired phone's server could not tell whose it is, and the phone's own+// origin is Capacitor's asset server, which answers every path with index.html: the phones sent+// every "left" to https://localhost/api/activity. The api() call beside it reaches the server.+export function beacon(path, body) {+ if (MOBILE) return false+ try {+ if (typeof navigator === 'undefined' || typeof navigator.sendBeacon !== 'function') return false+ return !!navigator.sendBeacon(appBase().replace(/\/$/, '') + path, new Blob([JSON.stringify(body)], { type: 'application/json' }))+ } catch { return false }+}+// Bootstraps the connection itself: the base isn't configured yet (that's what this call decides),// so it talks straight to the server the user typed in, no Authorization header.export async function pairRedeem(serverBase, code) {diff --git a/frontend/src/lib/api.mobile.test.js b/frontend/src/lib/api.mobile.test.jsindex 7c1b23768..d36018c57 100644--- a/frontend/src/lib/api.mobile.test.js+++ b/frontend/src/lib/api.mobile.test.js@@ -8,7 +8,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'vi.mock('./mobile.js', () => ({ MOBILE: true }))-import { api, setRemoteAuth } from './api.js'+import { api, beacon, setRemoteAuth } from './api.js'afterEach(() => { vi.unstubAllGlobals(); setRemoteAuth('', null) })@@ -30,3 +30,16 @@ describe('api() on the phone build', () => {expect(fetch.mock.calls[0][1].headers.Authorization).toBe('Bearer TOKEN')})})++// The workout screen's "left" beacon went to https://localhost/api/activity on every phone, paired+// or not: Capacitor's asset server answered it with index.html (Android QA, v1.3.9).+describe('the "left" beacon on the phone build', () => {+ it('is never sent, paired or not: the api() call beside it is the one that reaches the server', () => {+ const sendBeacon = vi.fn(() => true)+ vi.stubGlobal('navigator', { ...navigator, sendBeacon })+ expect(beacon('/api/activity', { active: false })).toBe(false)+ setRemoteAuth('https://gym.example.com', 'TOKEN')+ expect(beacon('/api/activity', { active: false })).toBe(false)+ expect(sendBeacon).not.toHaveBeenCalled()+ })+})diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsxindex a0e309742..db342cf98 100644--- a/frontend/src/views/Workout.jsx+++ b/frontend/src/views/Workout.jsx@@ -12,7 +12,7 @@ import { fmtNum, fmtPlate, capWords, fmtDate, todayISO, exCount, DAYN } from '..import { speedUnitOf, toSpeed, fromSpeed } from '../lib/speed.js'import { beep, vibrate, unlock } from '../lib/sound.js'import { t, exerciseNameFor, exerciseNameClass } from '../lib/i18n.js'-import { api, appBase } from '../lib/api.js'+import { api, beacon } from '../lib/api.js'import { insertionIndexAfterCurrentUnit, nextUnfinishedUnit, setProgressHighWater, supersetFlowStep, restAfterSet, restOnRecheck, restSecFor, warmupRestSecFor } from '../lib/supersetFlow.js'import Media from '../components/Media.jsx'import { startFlow, exercisePicker, exConfigSheet, exerciseDetailSheet, finishWorkout, exitWorkoutEdit, workoutCompleteSheet, confirmSheet, exerciseNoteSheet, sessionNoteSheet, renameWorkoutSheet, swapActiveWorkoutExercise, barWeightSheet, menuSheet, effortPickerSheet, exerciseHistorySheet, addRoutineToSessionSheet } from '../sheets.jsx'@@ -1234,8 +1234,8 @@ function ActiveWorkout() {const iv = setInterval(() => { if (!stopped) ping(true) }, 20000)return () => {stopped = true; clearInterval(iv)- // best-effort "left" signal: sendBeacon survives a tab close, fetch covers in-app nav- try { navigator.sendBeacon?.(appBase().replace(/\/$/, '') + '/api/activity', new Blob([JSON.stringify({ active: false })], { type: 'application/json' })) } catch { /* */ }+ // best-effort "left" signal: the beacon survives a tab close, fetch covers in-app nav+ beacon('/api/activity', { active: false })api('/api/activity', { method: 'POST', body: JSON.stringify({ active: false }) }).catch(() => {})}}, [])