diff --git a/packages/ui/src/elements/EditMany/DrawerContent.tsx b/packages/ui/src/elements/EditMany/DrawerContent.tsx index 526a79d7a57..cc9b13b4411 100644 --- a/packages/ui/src/elements/EditMany/DrawerContent.tsx +++ b/packages/ui/src/elements/EditMany/DrawerContent.tsx @@ -270,7 +270,11 @@ export const EditManyDrawerContent: React.FC = (prop router.replace( qs.stringify( { - ...parseSearchParams(searchParams), + // Read the live location instead of `useSearchParams()`, which is stale + // here: `ListQueryProvider` syncs the list query to the URL through the + // History API to avoid a re-render, and the hook never observes that. + // Spreading the stale value wipes columns/filters/limit off the URL. + ...parseSearchParams(new URLSearchParams(window.location.search)), _r: Date.now(), // Cache buster to force fresh data fetch. Prevents an e2e race condition where sometimes the data is not updated. page: selectAll ? '1' : undefined, }, diff --git a/packages/ui/src/utilities/parseSearchParams.ts b/packages/ui/src/utilities/parseSearchParams.ts index d6631321010..03e4cbefaa8 100644 --- a/packages/ui/src/utilities/parseSearchParams.ts +++ b/packages/ui/src/utilities/parseSearchParams.ts @@ -1,5 +1,3 @@ -import type { ReadonlyURLSearchParams } from 'next/navigation.js' - import * as qs from 'qs-esm' /** @@ -7,10 +5,10 @@ import * as qs from 'qs-esm' * This function is a wrapper around the `qs` library. * In Next.js, the `useSearchParams()` hook from `next/navigation` returns a `URLSearchParams` object. * This function can be used to parse that object into a more usable format. - * @param {ReadonlyURLSearchParams} searchParams - The URLSearchParams object to parse. + * @param {URLSearchParams} searchParams - The URLSearchParams object to parse. * @returns {qs.ParsedQs} - The parsed query string object. */ -export function parseSearchParams(searchParams: ReadonlyURLSearchParams): qs.ParsedQs { +export function parseSearchParams(searchParams: URLSearchParams): qs.ParsedQs { const search = searchParams.toString() return qs.parse(search, { diff --git a/test/bulk-edit/e2e.spec.ts b/test/bulk-edit/e2e.spec.ts index 7a4a3fdc958..39ea55104aa 100644 --- a/test/bulk-edit/e2e.spec.ts +++ b/test/bulk-edit/e2e.spec.ts @@ -682,6 +682,45 @@ test.describe('Bulk Edit', () => { } }) + test('should keep the list query params in the URL after a successful edit', async () => { + await deleteAllPosts() + await createPost({ title: 'Post 1' }) + + await page.goto(postsUrl.list) + + // `ListQueryProvider` writes these through the History API rather than the + // router, so `useSearchParams()` never observes them. Waiting for `limit` + // makes that the explicit precondition instead of an incidental one. + await expect.poll(() => page.url(), { timeout: POLL_TOPASS_TIMEOUT }).toContain('limit=') + + await page.locator('input#select-all').check() + await page.locator('.list-selection__button[aria-label="Edit"]').click() + + const editDrawer = page.locator('dialog#edit-posts') + await expect(editDrawer).toBeVisible() + + await selectInput({ + multiSelect: true, + options: ['Title'], + selectLocator: editDrawer.locator('.react-select'), + }) + + await editDrawer.locator('input#field-title').fill('test') + + // Assert on the refresh request rather than the settled URL: the provider + // re-adds the params on the next render, so the address bar self-heals and + // would hide a refresh that went out bare. + const refreshRequest = page.waitForRequest( + (request) => request.url().includes('/collections/posts') && request.url().includes('_r='), + ) + + await editDrawer.locator('button[type="submit"]:has-text("Publish changes")').click() + + await expect(page.locator('.payload-toast-container .toast-success')).toBeVisible() + + expect((await refreshRequest).url()).toContain('limit=') + }) + test('should not delete nested un-named tab array data', async () => { const originalDoc = await payload.create({ collection: tabsSlug,