Repository navigation
fix: prevent IS redirect hang in 3.x - #3538
Conversation
| // during render re-fires on every state update (e.g. scrollRestoration's | ||
| // resetInfiniteScroll) and loops until the tab hangs. | ||
| useEffect(() => { | ||
| if (!redirectUrl || lastRedirectedUrl.current === redirectUrl) return |
There was a problem hiding this comment.
lastRedirectedUrl is never cleared when redirectUrl goes back to undefined. If an IS redirect points to another search URL (say tv → /s?q=televisao), Next keeps the /s page mounted, so the ref still holds /s?q=televisao. Searching tv again returns the cached redirect from SWR, the effect bails on the ref check, and the page stays on EmptySearch without navigating.
| if (!redirectUrl || lastRedirectedUrl.current === redirectUrl) return | |
| if (!redirectUrl) { | |
| lastRedirectedUrl.current = null | |
| return | |
| } | |
| if (lastRedirectedUrl.current === redirectUrl) return |
There was a problem hiding this comment.
Updated the effect to clear the redirect ref when redirectUrl is no longer available. This allows a subsequent search to redirect to the same destination while SearchWrapper remains mounted.
| if (!redirectUrl || lastRedirectedUrl.current === redirectUrl) return | ||
|
|
||
| lastRedirectedUrl.current = redirectUrl | ||
| void router.replace(redirectUrl) |
There was a problem hiding this comment.
void drops the promise, and the ref is set before the navigation finishes. In Next 13 a cancelled route change, or one that fails to load the target chunk, rejects (e.g. Abort fetching component for route). That turns into an unhandled rejection, and since the ref is already set, nothing retries while the user is still on /s. Could we catch it and reset the ref?
| void router.replace(redirectUrl) | |
| router.replace(redirectUrl).catch(() => { | |
| lastRedirectedUrl.current = null | |
| }) |
There was a problem hiding this comment.
Added handling for rejected router.replace() calls and cancellations that resolve to false. Both release the failed attempt so a future effect execution can try again, without introducing automatic retries. Each attempt is tracked by object identity, preventing an older navigation failure from clearing a newer attempt, even when both target the same URL.
| const redirectUrl = pageProductGalleryData?.redirect?.url | ||
| const lastRedirectedUrl = useRef<string | null>(null) | ||
|
|
||
| // Intelligent Search redirects must run in an effect — calling router.replace |
There was a problem hiding this comment.
The reasoning in this code comment also applies to resetInfiniteScroll(0) at L93, which still writes to the global search store during render. When searchTotalPages < stateTotalPages, React warns Cannot update a component while rendering a different component, and the store subscribers (SearchProvider's onChange, this component) re-render mid-render. Not a loop today because of the condition, but moving it into a useEffect on searchTotalPages/stateTotalPages would close the same class of bug. Fine as a follow-up if you want to keep this a straight backport.
There was a problem hiding this comment.
Keeping this as a follow-up to preserve the scope of the redirect backport and validate the pagination timing change independently.
| // Intelligent Search redirects must run in an effect — calling router.replace | ||
| // during render re-fires on every state update (e.g. scrollRestoration's | ||
| // resetInfiniteScroll) and loops until the tab hangs. | ||
| useEffect(() => { |
There was a problem hiding this comment.
The original fix (#3437) shipped SearchWrapper.browser.test.tsx with three cases: replace called once from the effect, not called again on rerender, not called without a redirect. Can we port those to the 3.x jest setup under packages/core/test? Without them 3.x has nothing guarding against the render loop coming back.
There was a problem hiding this comment.
Adapted the original regression scenarios from #3437 to the existing Jest setup and extended coverage for the review fixes
There was a problem hiding this comment.
The effect changes look good, thanks. One thing still needs to change before this can merge: the new test file doesn't run on 3.x.
On 929e552, pnpm jest test/components/templates/SearchPage/SearchWrapper.browser.test.tsx in packages/core fails before any case runs. The test itself is fine. This is the first .tsx test in core on 3.x, and the test setup isn't ready for JSX yet. Two changes fix it:
-
In
packages/core/test/tsconfig.json, add"jsx": "react-jsx"tocompilerOptions. The base tsconfig setsjsx: preserve, so ts-jest leaves the JSX as is and Jest fails withSyntaxError: Unexpected token '<'."compilerOptions": { "types": ["jest"], "jsx": "react-jsx" }
-
Add
@types/react-domto the devDependencies ofpackages/core(18.2.21is already in the lockfile). Without it ts-jest stops with TS7016 on thereact-dom/clientandreact-dom/test-utilsimports.
With both changes all 10 cases pass locally. The 3.x CI doesn't run jest, which is why the PR is still green.
There was a problem hiding this comment.
Fixed. All 10 tests should pass now
| if (!redirectUrl || lastRedirectedUrl.current === redirectUrl) return | ||
|
|
||
| lastRedirectedUrl.current = redirectUrl | ||
| void router.replace(redirectUrl) |
There was a problem hiding this comment.
Dropping shallow: true changes what happens when the redirect target is the /s page itself: it's now a full client transition with a getStaticProps data request where 3.x used to just update the URL. Probably fine and it matches 4.x, but the test plan only covers redirects to a different page, so worth checking a /s?q=... redirect on a store too.
| shallow: true, | ||
| }) | ||
|
|
||
| if (redirectUrl) { |
There was a problem hiding this comment.
Nit: this returns the same tree as the !pageProductGalleryData branch above, so if (!pageProductGalleryData || redirectUrl) would cover both. Main has the same duplication, so feel free to skip it to keep parity.
|
I installed this version in a store that has this looping problem: Looping scenario: https://drive.google.com/file/d/1CcDywHPfkdLnHlIu1B21usB-77bdcHRj/view?usp=drivesdk |
What's the purpose of this pull request?
Backport the Intelligent Search redirect fix from #3437, released in FastStore 4.6.0, to the 3.x branch.
When search redirects interact with scroll restoration, calling router.replace during rendering can trigger an infinite render and redirect loop, freezing the UI. Version 3.x still contained this behavior.
This behavior is causing the UI to freeze in production stores that are still using version 3.x, whenever an IS redirect is employed.
How it works?
This PR replicates exactly the SearchWrapper.tsx implementation introduced in #3437:
How to test it?
/admin/search/v4/redirectsLocal validation completed: Biome and git diff --check passed. Store validation is pending.
Starters Deploy Preview
Pending.
References
#3437 — fix(core): prevent IS redirect hang and improve scroll restoration: original fix in FastStore 4.x. This PR replicates its SearchWrapper.tsx changes exactly in 3.x.
Checklist
PR Title and Commit Messages
feat,fix,chore,docs,style,refactor,ciandtestPR Description
breaking change,bug,contributing,performance,documentation..Dependencies
pnpm-lock.yamlfile when there were changes to the packagesDocumentation
@Mariana-Caetanoto review and update (Or submit a doc request)