From 705bd582ffe5c936c44b5137cdef6cf6347d2f43 Mon Sep 17 00:00:00 2001 From: AnandNair4 Date: Wed, 4 Nov 2026 12:49:37 +0530 Subject: [PATCH] fix(pages): preserve scroll after action submissions --- packages/void/src/pages/client.ts | 5 ++- packages/void/test/unit/action-result.test.ts | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/void/src/pages/client.ts b/packages/void/src/pages/client.ts index 5748b9f2e..fb4664403 100644 --- a/packages/void/src/pages/client.ts +++ b/packages/void/src/pages/client.ts @@ -251,7 +251,10 @@ export async function submitAction( options: VisitOptions, ): Promise { try { - const pageData = await router.visit(url, options); + const pageData = await router.visit(url, { + preserveScroll: true, + ...options, + }); if ( typeof pageData === 'object' && pageData !== null && diff --git a/packages/void/test/unit/action-result.test.ts b/packages/void/test/unit/action-result.test.ts index f9e43eff9..d58899313 100644 --- a/packages/void/test/unit/action-result.test.ts +++ b/packages/void/test/unit/action-result.test.ts @@ -124,6 +124,37 @@ describe('action result handling', () => { expect(isAbortError(new DOMException('Navigation aborted', 'AbortError'))).toBe(true); expect(isAbortError(new Error('Boom'))).toBe(false); }); + + it('preserves scroll by default when submitting an action', async () => { + const visit = vi.fn(async () => ({ props: {} })); + const router = routerWithVisit(visit); + + await submitAction(router, '/users', { + method: 'POST', + data: { name: 'Alice' }, + }); + + expect(visit).toHaveBeenCalledWith('/users', { + preserveScroll: true, + method: 'POST', + data: { name: 'Alice' }, + }); + }); + + it('allows explicitly disabling scroll preservation', async () => { + const visit = vi.fn(async () => ({ props: {} })); + const router = routerWithVisit(visit); + + await submitAction(router, '/users', { + method: 'POST', + preserveScroll: false, + }); + + expect(visit).toHaveBeenCalledWith('/users', { + preserveScroll: false, + method: 'POST', + }); + }); }); describe('action clients', () => { @@ -136,6 +167,7 @@ describe('action clients', () => { method: 'POST', data: undefined, _resolveOnShell: true, + preserveScroll: true, }); }); @@ -167,6 +199,7 @@ describe('action clients', () => { expect(firstVisit).toHaveBeenCalledWith('/__action-test/static', { method: 'POST', data: undefined, + preserveScroll: true, }); }); @@ -184,6 +217,7 @@ describe('action clients', () => { expect(visit).toHaveBeenCalledWith('/__action-test/users/a%2Fb%3Fc?save', { method: 'PATCH', data: { name: 'Alice' }, + preserveScroll: true, }); });