diff --git a/e2e/mobile-swipe.spec.ts b/e2e/mobile-swipe.spec.ts
index 5b304d2..4338dff 100644
--- a/e2e/mobile-swipe.spec.ts
+++ b/e2e/mobile-swipe.spec.ts
@@ -412,6 +412,24 @@ test.describe('Mobile Swipe Viewer', () => {
expect(ids.filter((id) => mockTopItemIds.includes(id))).toEqual([]);
});
+ test('a feed opened from its tab starts at the top, not where the last story was scrolled to', async ({ page }) => {
+ await page.goto('/#/');
+ await waitForSwipeReady(page, 6);
+ await expect(page).toHaveURL(/\/item\/12345/, { timeout: 5000 });
+
+ // Both stories taller than the viewport, so a short one can't clamp the
+ // page back to the top on its own.
+ await page.addStyleTag({ content: '[data-testid="swipe-panel"].active { min-height: 3000px !important; }' });
+ await page.evaluate(() => window.scrollTo({ top: 1000, behavior: 'instant' }));
+ await expect.poll(() => page.evaluate(() => window.scrollY)).toBe(1000);
+
+ await page.getByRole('link', { name: 'best', exact: true }).click();
+
+ await expect(page).toHaveURL(/\/item\/33001/, { timeout: 5000 });
+ await expect(getActiveSwipePanel(page)).toHaveAttribute('data-item-id', '33001');
+ await expect.poll(() => page.evaluate(() => window.scrollY)).toBe(0);
+ });
+
test('a feed refresh that lands after a swipe keeps the story on screen', async ({ page }) => {
// An hour-old cached Top feed in the reverse of the live order, and Firebase
// reads that take 1.5s, so the refresh lands after the swipe.
diff --git a/src/components/SwipeStoryViewer.test.tsx b/src/components/SwipeStoryViewer.test.tsx
index 7baf5f3..a0827e1 100644
--- a/src/components/SwipeStoryViewer.test.tsx
+++ b/src/components/SwipeStoryViewer.test.tsx
@@ -476,6 +476,8 @@ describe('SwipeStoryViewerCore — swipe-position restore', () => {
document.dispatchEvent(new Event('visibilitychange'));
});
};
+ // Opening the feed scrolled to the top already; only the restore counts.
+ vi.mocked(window.scrollTo).mockClear();
try {
showTab('hidden');
showTab('visible');
@@ -591,6 +593,24 @@ describe('SwipeStoryViewerCore — swipe-position restore', () => {
expect(readSwipePosition({ viewer: { from: 'best' } })).toBeNull();
expect(readSwipePosition({ viewer: { from: 'top' } })?.storyId).toBe(2);
});
+
+ it('opens a feed from its tab at the top of the page', () => {
+ const stories = [createStoryItem({ id: 1 }), createStoryItem({ id: 2 })];
+
+ render();
+
+ expect(window.scrollTo).toHaveBeenCalledWith(0, 0);
+ });
+
+ it('leaves the page where it is when the viewer opens on a story', () => {
+ const stories = [createStoryItem({ id: 1 }), createStoryItem({ id: 2 })];
+
+ render(
+ ,
+ );
+
+ expect(window.scrollTo).not.toHaveBeenCalled();
+ });
});
describe('SwipeStoryViewerCore — end-of-feed panel', () => {
diff --git a/src/components/SwipeStoryViewer.tsx b/src/components/SwipeStoryViewer.tsx
index 1c1a2d4..88b4eca 100644
--- a/src/components/SwipeStoryViewer.tsx
+++ b/src/components/SwipeStoryViewer.tsx
@@ -368,6 +368,12 @@ export function SwipeStoryViewerCore({
useLayoutEffect(() => {
if (opensFresh) clearSwipePosition(backState);
}, [opensFresh, backState]);
+
+ // The page is still scrolled where the previous screen left it, and panels
+ // don't reset it.
+ useLayoutEffect(() => {
+ if (opensFresh) window.scrollTo(0, 0);
+ }, [opensFresh]);
useEffect(() => {
// Save position + scrollY when page is about to be hidden/cached