Skip to content

fix: prevent IS redirect hang in 3.x - #3538

Merged
lariciamota merged 3 commits into
vtex:3.xfrom
jackson-avanti:feat/3.x-core-scroll-restoration
Nov 4, 2026
Merged

lariciamota merged 3 commits into
vtex:3.xfrom
jackson-avanti:feat/3.x-core-scroll-restoration

Conversation

@jackson-avanti

@jackson-avanti jackson-avanti commented Oct 22, 2026 •

Copy link
Copy Markdown

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:

  • Moves router.replace into a useEffect.
  • Uses a useRef to track the last redirected URL and prevent repeated redirects to the same destination.
  • Removes the shallow: true option, matching the original fix.
  • Preserves the empty search UI while the redirect takes place.

How to test it?

  • Install this branch's @faststore/core package in a store using FastStore 3.x.
  • Configure an Intelligent Search redirect in VTEX Admin for a specific search term. Path: /admin/search/v4/redirects
  • With scroll restoration enabled, load additional search result pages, then search for the configured term.
  • Verify that the store navigates to the configured destination without freezing or entering a redirect loop.
  • Repeat the search flow and confirm that redirects continue to work.
  • Search for a term without a configured redirect and confirm that regular results and infinite scrolling still work.
    Local 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

  • PR title and commit messages follow the Conventional Commits specification
    • Available prefixes: feat, fix, chore, docs, style, refactor, ci and test

PR Description

  • Added a label according to the PR goal - breaking change, bug, contributing, performance, documentation..

Dependencies

  • Committed the pnpm-lock.yaml file when there were changes to the packages

Documentation

  • PR description
  • For documentation changes, ping @Mariana-Caetano to review and update (Or submit a doc request)

@jackson-avanti
jackson-avanti requested a review from a team as a code owner October 22, 2026 21:15
@jackson-avanti
jackson-avanti requested review from gabpaladino and lucasfp13 and removed request for a team October 22, 2026 21:15
// 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if (!redirectUrl || lastRedirectedUrl.current === redirectUrl) return
if (!redirectUrl) {
lastRedirectedUrl.current = null
return
}
if (lastRedirectedUrl.current === redirectUrl) return

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Suggested change
void router.replace(redirectUrl)
router.replace(redirectUrl).catch(() => {
lastRedirectedUrl.current = null
})

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adapted the original regression scenarios from #3437 to the existing Jest setup and extended coverage for the review fixes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. In packages/core/test/tsconfig.json, add "jsx": "react-jsx" to compilerOptions. The base tsconfig sets jsx: preserve, so ts-jest leaves the JSX as is and Jest fails with SyntaxError: Unexpected token '<'.

    "compilerOptions": {
      "types": ["jest"],
      "jsx": "react-jsx"
    }
  2. Add @types/react-dom to the devDependencies of packages/core (18.2.21 is already in the lockfile). Without it ts-jest stops with TS7016 on the react-dom/client and react-dom/test-utils imports.

With both changes all 10 cases pass locally. The 3.x CI doesn't run jest, which is why the PR is still green.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. All 10 tests should pass now

if (!redirectUrl || lastRedirectedUrl.current === redirectUrl) return

lastRedirectedUrl.current = redirectUrl
void router.replace(redirectUrl)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skiping it for now

@jackson-avanti

Copy link
Copy Markdown
Author

@lariciamota
lariciamota merged commit 8b30c48 into vtex:3.x Nov 4, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants