-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Search bar performance improvements #4072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
general-adhoc
wants to merge
2
commits into
main
Choose a base branch
from
fix-search-enter-debounce
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // @vitest-environment happy-dom | ||
| import { createElement } from "react"; | ||
| import { flushSync } from "react-dom"; | ||
| import { createRoot } from "react-dom/client"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| // Mock fns shared with the next/navigation mock factory (hoisted above imports). | ||
| const mocks = vi.hoisted(() => ({ | ||
| push: vi.fn(), | ||
| replace: vi.fn(), | ||
| searchParams: new URLSearchParams(), | ||
| })); | ||
|
|
||
| vi.mock("next/navigation", () => ({ | ||
| useRouter: () => ({ push: mocks.push, replace: mocks.replace }), | ||
| usePathname: () => "/test", | ||
| useSearchParams: () => mocks.searchParams, | ||
| })); | ||
|
|
||
| // `useRouterStuff` imports `AppRouterInstance` (type only) from this module; mock | ||
| // it defensively in case the transpiler emits a runtime import for it. | ||
| vi.mock("next/dist/shared/lib/app-router-context.shared-runtime", () => ({})); | ||
|
|
||
| // Import the hook directly from source (not the @dub/ui barrel) to keep the test | ||
| // light and avoid pulling the whole UI library into the test environment. | ||
| import { useRouterStuff } from "../../../../packages/ui/src/hooks/use-router-stuff"; | ||
|
|
||
| /** Render the hook once and return its API (no effects involved here). */ | ||
| function renderQueryParams() { | ||
| let api: ReturnType<typeof useRouterStuff> | undefined; | ||
| function Probe() { | ||
| api = useRouterStuff(); | ||
| return null; | ||
| } | ||
| const container = document.createElement("div"); | ||
| document.body.appendChild(container); | ||
| flushSync(() => createRoot(container).render(createElement(Probe))); | ||
| if (!api) throw new Error("hook did not render"); | ||
| return api; | ||
| } | ||
|
|
||
| describe("useRouterStuff queryParams — shallow routing", () => { | ||
| let replaceState: ReturnType<typeof vi.spyOn>; | ||
| let pushState: ReturnType<typeof vi.spyOn>; | ||
|
|
||
| beforeEach(() => { | ||
| mocks.push.mockClear(); | ||
| mocks.replace.mockClear(); | ||
| mocks.searchParams = new URLSearchParams(); | ||
| replaceState = vi.spyOn(window.history, "replaceState"); | ||
| pushState = vi.spyOn(window.history, "pushState"); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| // This is the invariant the search-perf fix depends on: a `shallow` write must | ||
| // update the URL via the History API and must NOT trigger a router navigation | ||
| // (which would re-render Server Components — a wasted RSC round-trip for our | ||
| // client-fetched, SWR-keyed tables). SearchBoxPersisted relies on this. | ||
| it("shallow + replace writes the URL via history.replaceState, never the router", () => { | ||
| const { queryParams } = renderQueryParams(); | ||
|
|
||
| queryParams({ | ||
| set: { search: "foo" }, | ||
| del: "page", | ||
| shallow: true, | ||
| replace: true, | ||
| }); | ||
|
|
||
| expect(replaceState).toHaveBeenCalledWith({}, "", "/test?search=foo"); | ||
| expect(pushState).not.toHaveBeenCalled(); | ||
| expect(mocks.replace).not.toHaveBeenCalled(); | ||
| expect(mocks.push).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("shallow without replace uses history.pushState (mirrors router.push)", () => { | ||
| const { queryParams } = renderQueryParams(); | ||
|
|
||
| queryParams({ set: { search: "foo" }, shallow: true }); | ||
|
|
||
| expect(pushState).toHaveBeenCalledWith({}, "", "/test?search=foo"); | ||
| expect(mocks.push).not.toHaveBeenCalled(); | ||
| expect(mocks.replace).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("non-shallow falls back to a router navigation (the RSC round-trip we avoid for search)", () => { | ||
| const { queryParams } = renderQueryParams(); | ||
|
|
||
| queryParams({ set: { search: "foo" } }); | ||
|
|
||
| expect(mocks.push).toHaveBeenCalledWith("/test?search=foo", { | ||
| scroll: false, | ||
| }); | ||
| expect(replaceState).not.toHaveBeenCalled(); | ||
| expect(pushState).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("no-ops when the resulting URL is unchanged (no router nav, no history write)", () => { | ||
| mocks.searchParams = new URLSearchParams("search=foo"); | ||
| const { queryParams } = renderQueryParams(); | ||
|
|
||
| // `del` a param that isn't present → identical URL. This is the pagination | ||
| // case: after a search clears `page`, a pagination sync re-issues del:page, | ||
| // which must not fire a (same-URL) navigation / RSC round-trip. | ||
| queryParams({ del: "page" }); | ||
|
|
||
| expect(mocks.push).not.toHaveBeenCalled(); | ||
| expect(mocks.replace).not.toHaveBeenCalled(); | ||
| expect(replaceState).not.toHaveBeenCalled(); | ||
| expect(pushState).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Unmount the React root to keep tests isolated.
renderQueryParamscreates and mounts a root but never unmounts/removes it. Over multiple tests, this leaks mounted trees and can cause cross-test interference once the hook grows effects.Suggested fix
function renderQueryParams() { let api: ReturnType<typeof useRouterStuff> | undefined; function Probe() { api = useRouterStuff(); return null; } const container = document.createElement("div"); document.body.appendChild(container); - flushSync(() => createRoot(container).render(createElement(Probe))); + const root = createRoot(container); + flushSync(() => root.render(createElement(Probe))); if (!api) throw new Error("hook did not render"); - return api; + return { + ...api, + cleanup: () => { + root.unmount(); + container.remove(); + }, + }; }🤖 Prompt for AI Agents