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.

Boot watchdog: say why on screen when the app cannot start, with a Reload button (P1)

Duarte Santosc175124fb9b3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 637d9b431..71cbf937f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -181,6 +181,12 @@ little Ready / Recovering / Fatigued bar, plus when it drops back to Recovering
you leave it alone. Handy for deciding whether those legs are really ready (thanks @sg41).
**Fixes**
+- **openGym starts again on iPhones with iOS 15.5 to 16.3** (P1). 1.4.2 and the web demo sat on
+ the splash forever there, because one tiny link check used regex syntax Safari only learned in
+ 16.4. Rewritten, the build now targets Safari 15 like the app does, and a test reads every built
+ file for syntax older iPhones can't handle. Should a browser still choke, openGym now says so
+ on screen with the error and a Reload button instead of lifting the logo forever. Thanks
+ h097327 for the excellent report.
- Deadlift variants that forgot your lower back remember it now: the kettlebell suitcase
deadlift and the plate deadlift lunge press (#693, #700, thanks @JeromeTDev and @choksi2212).
- Equipment profiles know more about what an exercise needs. An inverted row needs a bar (unless
diff --git a/frontend/index.html b/frontend/index.html
index 34d683eb2..cac094e23 100644
--- a/frontend/index.html
+++ b/frontend/index.html
@@ -13,6 +13,53 @@
document.documentElement.lang = l;
document.documentElement.dir = l === 'ar' ? 'rtl' : 'ltr';
} catch (e) {}
+ // Boot watchdog. A chunk this browser cannot parse (1.4.2's regex lookbehind on iOS 15.5 to
+ // 16.3) kills the module graph before any of the app runs, and the logo lifted forever. Here,
+ // outside the bundle and in plain old syntax, so it still runs then: a script that fails to
+ // load or parse, or no app after 10 s, puts the reason and a Reload button where the logo was.
+ // English only, as the packs are in the bundle that did not load. lib/boot.js removes #boot
+ // (message included) once the app draws, so a slow start that gets there still gets there.
+ (function () {
+ var shown = false, last = '';
+ function fail(title, detail) {
+ var b = document.getElementById('boot');
+ // Too early for the boot screen (a script in <head>): say it once the page is there.
+ if (!b && document.readyState === 'loading') return document.addEventListener('DOMContentLoaded', function () { fail(title, detail); });
+ if (shown || !b || b.getAttribute('data-leaving')) return;
+ shown = true;
+ b.className = 'fail';
+ b.removeAttribute('aria-hidden');
+ b.setAttribute('role', 'alert');
+ b.setAttribute('dir', 'ltr');
+ b.innerHTML = '';
+ var box = document.createElement('div'), h = document.createElement('p'), p = document.createElement('p');
+ h.className = 'h';
+ h.textContent = title;
+ p.textContent = 'Reloading often helps. If it keeps happening, please send us this:';
+ var c = document.createElement('code');
+ c.textContent = (detail || 'No error, it just never finished loading.') + ' (' + navigator.userAgent + ')';
+ var r = document.createElement('button');
+ r.type = 'button';
+ r.textContent = 'Reload';
+ r.onclick = function () { location.reload(); };
+ box.appendChild(h); box.appendChild(p); box.appendChild(c); box.appendChild(r);
+ b.appendChild(box);
+ }
+ window.addEventListener('error', function (e) {
+ var t = e && e.target;
+ if (t && t.tagName === 'SCRIPT') return fail('openGym could not start on this browser.', 'Could not load ' + (t.src || 'a script'));
+ if (!e || !e.message) return;
+ last = e.message + (e.filename ? ' at ' + e.filename.split('/').pop() + ':' + e.lineno : '');
+ if (/SyntaxError|Unexpected|Invalid regular expression/i.test(e.message)) fail('openGym could not start on this browser.', last);
+ }, true);
+ window.addEventListener('unhandledrejection', function (e) {
+ var r = e && e.reason;
+ if (r) last = String((r && r.message) || r);
+ });
+ setTimeout(function () {
+ fail(last ? 'openGym could not start on this browser.' : 'openGym is taking too long to start.', last);
+ }, 10000);
+ })();
</script>
<title>openGym</title>
<meta name="description" content="Personal gym & body weight tracker">
@@ -50,6 +97,11 @@
@keyframes boot-plate-r{from{transform:translateX(40px);opacity:0}}
@keyframes boot-rep{50%{transform:translateY(-26px)}}
@keyframes boot-shadow{50%{transform:scaleX(.72);opacity:.05}}
+ #boot.fail{place-items:center;padding:24px}
+ #boot.fail div{max-width:340px;font:15px/1.4 -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,sans-serif;color:#e6e8ec;text-align:center}
+ #boot.fail .h{font-size:19px;font-weight:600;margin:0 0 8px}
+ #boot.fail code{display:block;margin:14px 0 18px;padding:10px;border-radius:10px;background:#1a1d24;color:#a9b0bc;font-size:12px;text-align:left;word-break:break-word;-webkit-user-select:text;user-select:text}
+ #boot.fail button{font:inherit;font-weight:600;color:#0c0e12;background:#a3e635;border:0;border-radius:12px;min-height:44px;padding:0 28px}
@media (prefers-reduced-motion:reduce){
#boot .bar,#boot .pl,#boot .pr,#boot ellipse{animation:none}
#boot .db{animation:boot-breathe 1.6s ease-in-out infinite}
diff --git a/frontend/src/lib/boot-watchdog.test.js b/frontend/src/lib/boot-watchdog.test.js
new file mode 100644
index 000000000..6b2fc0428
--- /dev/null
+++ b/frontend/src/lib/boot-watchdog.test.js
@@ -0,0 +1,103 @@
+// @vitest-environment happy-dom
+// The boot watchdog, the inline script in index.html. 1.4.2 sat on its splash forever on iOS 15.5
+// to 16.3: one chunk did not parse, nothing of the app ran, and nothing said why. The script is
+// read off index.html itself and run against the boot screen it guards.
+import { readFileSync } from 'node:fs'
+import { dirname, join } from 'node:path'
+import { fileURLToPath } from 'node:url'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { hideBoot } from './boot.js'
+
+const html = readFileSync(join(dirname(fileURLToPath(import.meta.url)), '../../index.html'), 'utf8')
+const inline = [...html.matchAll(/<script(?![^>]*\bsrc=)[^>]*>([\s\S]*?)<\/script>/g)].map(m => m[1])
+const bootMarkup = html.match(/<div id="boot"[\s\S]*?<\/svg>\s*<\/div>/)[0]
+
+let added = []
+const realAdd = window.addEventListener.bind(window)
+beforeEach(() => {
+ vi.useFakeTimers()
+ added = []
+ // Each test runs the script afresh; its listeners are taken off again afterwards.
+ vi.spyOn(window, 'addEventListener').mockImplementation((type, fn, opts) => { added.push([type, fn, opts]); realAdd(type, fn, opts) })
+ document.body.innerHTML = bootMarkup + '<div id="root"></div>'
+ for (const body of inline) new Function(body)()
+})
+afterEach(() => {
+ for (const [type, fn, opts] of added) window.removeEventListener(type, fn, opts)
+ vi.restoreAllMocks()
+ vi.useRealTimers()
+})
+
+const boot = () => document.getElementById('boot')
+const syntaxError = () => new ErrorEvent('error', {
+ message: 'SyntaxError: Invalid regular expression: invalid group specifier name',
+ filename: 'https://gym.test/demo/assets/media-refs-CaxawJNr.js', lineno: 1,
+})
+
+describe('boot watchdog', () => {
+ it('replaces the logo with the reason and a Reload button when a chunk does not parse', () => {
+ window.dispatchEvent(syntaxError())
+ const b = boot()
+ expect(b.className).toBe('fail')
+ expect(b.querySelector('svg')).toBeNull()
+ expect(b.getAttribute('role')).toBe('alert')
+ expect(b.hasAttribute('aria-hidden')).toBe(false)
+ expect(b.textContent).toContain('openGym could not start on this browser.')
+ expect(b.querySelector('code').textContent).toContain('Invalid regular expression: invalid group specifier name at media-refs-CaxawJNr.js:1')
+ const reload = b.querySelector('button')
+ expect(reload.textContent).toBe('Reload')
+ expect(b.textContent).not.toContain('—')
+ })
+
+ it('says which script could not be loaded', () => {
+ const s = document.createElement('script')
+ s.type = 'text/plain' // happy-dom would try to fetch it otherwise; the event is the same
+ s.src = 'https://gym.test/assets/index-B93PyrcZ.js'
+ document.body.appendChild(s)
+ s.dispatchEvent(new Event('error'))
+ expect(boot().className).toBe('fail')
+ expect(boot().querySelector('code').textContent).toContain('Could not load https://gym.test/assets/index-B93PyrcZ.js')
+ })
+
+ it('ignores an image that failed to load, and an ordinary error until the 10 s are up', () => {
+ const img = document.createElement('img')
+ document.body.appendChild(img)
+ img.dispatchEvent(new Event('error'))
+ window.dispatchEvent(new ErrorEvent('error', { message: "TypeError: undefined is not an object (evaluating 'a.b')", filename: 'https://gym.test/assets/x.js', lineno: 3 }))
+ expect(boot().className).toBe('')
+ vi.advanceTimersByTime(10000)
+ expect(boot().textContent).toContain('openGym could not start on this browser.')
+ expect(boot().querySelector('code').textContent).toContain("undefined is not an object (evaluating 'a.b') at x.js:3")
+ })
+
+ it('says it is taking too long when nothing failed but the app never came', () => {
+ vi.advanceTimersByTime(9999)
+ expect(boot().querySelector('svg')).not.toBeNull()
+ vi.advanceTimersByTime(1)
+ expect(boot().textContent).toContain('openGym is taking too long to start.')
+ })
+
+ it('waits for the boot screen when the failure comes before the page is there', () => {
+ document.getElementById('boot').remove()
+ vi.spyOn(document, 'readyState', 'get').mockReturnValue('loading')
+ window.dispatchEvent(syntaxError())
+ document.body.insertAdjacentHTML('afterbegin', bootMarkup)
+ document.dispatchEvent(new Event('DOMContentLoaded'))
+ expect(boot().className).toBe('fail')
+ })
+
+ it('stays quiet once the app has drawn', () => {
+ hideBoot()
+ vi.advanceTimersByTime(10000)
+ window.dispatchEvent(syntaxError())
+ expect(document.getElementById('boot')).toBeNull()
+ })
+
+ it('leaves the message to lib/boot.js, which takes it away once a slow start gets there', () => {
+ vi.advanceTimersByTime(10000)
+ expect(boot().className).toBe('fail')
+ hideBoot()
+ vi.advanceTimersByTime(1000)
+ expect(document.getElementById('boot')).toBeNull()
+ })
+})
diff --git a/web/Dockerfile b/web/Dockerfile
index fe123fd56..49bd5a07d 100644
--- a/web/Dockerfile
+++ b/web/Dockerfile
@@ -107,7 +107,7 @@ ENV MEDIA_UPLOAD_MAX=48m
# chose, an Ollama on their LAN included, and the update check asks GitHub. Behind a proxy that
# injects a script of its own (analytics), set this to a policy that allows it, or to
# "frame-ancestors 'none'" for the old header.
-ENV CONTENT_SECURITY_POLICY="default-src 'self'; script-src 'self' 'sha256-z/iOLe5i8fD4VNlIXz/4hEayGLTQzoTc52dIY51OjYc='; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; media-src 'self' blob: https:; font-src 'self' data:; connect-src 'self' https: http:; worker-src 'self'; manifest-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'"
+ENV CONTENT_SECURITY_POLICY="default-src 'self'; script-src 'self' 'sha256-xUZtZS1nWJaAyUqA7ZKgkLhjdEisnY0MiRmax16aGFg='; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; media-src 'self' blob: https:; font-src 'self' data:; connect-src 'self' https: http:; worker-src 'self'; manifest-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'"
# Probe the port nginx was actually told to listen on (NGINX_PORT is configurable, so a
# hardcoded http://localhost/ breaks on any instance that changed it), and address it as