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.

web: a fresh load goes to the top now, not a frame later

Boris 'B' Kurktchiev3eefe4ee2345
diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx
index b31cb0c89..2de9b6d5f 100644
--- a/frontend/src/App.jsx
+++ b/frontend/src/App.jsx
@@ -152,9 +152,14 @@ function Shell() {
return () => window.removeEventListener('scroll', onScroll)
}, [])
useLayoutEffect(() => {
+ const first = pathRef.current === null
const samePath = pathRef.current === loc.pathname
pathRef.current = loc.pathname
- if (navType !== 'POP') { window.scrollTo(0, 0); return }
+ // A fresh load arrives as a POP with nothing remembered. It goes to the top like any new
+ // route — now, not a frame later: that frame landed on top of a scroll a view made on mount
+ // (the superset card centring its row, views/Workout.jsx), and a smooth scroll cut off by a
+ // scrollTo is simply gone.
+ if (navType !== 'POP' || first) { window.scrollTo(0, 0); return }
// A POP that stays on the route we are on is not a back-navigation: it is the history
// entry a sheet pushed (Modals.jsx, #63) being unwound as the sheet closes. Nothing new
// mounted, Modals puts the page back where it was itself, and a view that scrolled on
diff --git a/frontend/src/App.scroll.test.jsx b/frontend/src/App.scroll.test.jsx
new file mode 100644
index 000000000..eddd6f20b
--- /dev/null
+++ b/frontend/src/App.scroll.test.jsx
@@ -0,0 +1,48 @@
+// @vitest-environment happy-dom
+// The route's scroll restore on the very first load. A fresh load arrives as a POP with nothing
+// remembered, and used to scroll to the top a frame later — on top of a scroll a view had made on
+// mount (the superset card centring its row, views/Workout.jsx), cutting a smooth scroll off. It
+// goes to the top now, in the same pass, so a view's own scroll runs after it and stands.
+import React, { act } from 'react'
+import { createRoot } from 'react-dom/client'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import App from './App.jsx'
+import { DEF, useStore } from './store/useStore.js'
+
+vi.mock('./lib/sound.js', () => ({ setPlayOnSilent: vi.fn(), setVibrate: vi.fn(), setAlarmBuzzer: vi.fn() }))
+vi.mock('./lib/api.js', async importOriginal => {
+ const actual = await importOriginal()
+ return { ...actual, api: vi.fn(() => Promise.resolve({})), beacon: vi.fn() }
+})
+
+globalThis.IS_REACT_ACT_ENVIRONMENT = true
+
+let host, root, originalS, scrolls, frames
+beforeEach(() => {
+ originalS = useStore.getState().S
+ host = document.createElement('div')
+ document.body.appendChild(host)
+ root = createRoot(host)
+ scrolls = []; frames = []
+ window.scrollTo = vi.fn((x, y) => scrolls.push([x, y]))
+ // Frames never run on their own here: what is asked for now is all that has landed.
+ window.requestAnimationFrame = vi.fn(cb => { frames.push(cb); return frames.length })
+ window.cancelAnimationFrame = vi.fn()
+})
+afterEach(() => {
+ act(() => root.unmount())
+ host.remove()
+ useStore.setState({ S: originalS })
+})
+
+describe('the first load', () => {
+ it('goes to the top now, not a frame later', async () => {
+ useStore.setState({ S: JSON.parse(JSON.stringify(DEF)) })
+ await act(async () => { root.render(<App />) })
+ expect(scrolls).toContainEqual([0, 0])
+ // …and no top-of-page scroll is left waiting in a frame, where it would land on a view's own.
+ const before = scrolls.length
+ for (const cb of frames) cb()
+ expect(scrolls.length).toBe(before)
+ })
+})
diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx
index 39a96e3d8..50ebfc717 100644
--- a/frontend/src/views/Workout.jsx
+++ b/frontend/src/views/Workout.jsx
@@ -1042,7 +1042,7 @@ function ActiveWorkout() {
//
// The scroll waits for the next frame rather than running in the effect itself. App.jsx
// restores the route's remembered position in a frame it asked for during the same commit
- // (a reload, a back navigation), and a sheet closing (⋯ → Layout → List) puts the page back
+ // (a back navigation), and a sheet closing (⋯ → Layout → List) puts the page back
// where it was before the sheet opened, in an effect cleanup that runs before this one —
// both would win over a scroll made right here. A frame asked for now runs after theirs.
const listRef = useRef(null)