Skip to content

Fix router recreation on Admin rerender - #11434

Closed
inshu5757 wants to merge 1 commit into
marmelab:masterfrom
inshu5757:fix/11431-router-instance
Closed

inshu5757 wants to merge 1 commit into
marmelab:masterfrom
inshu5757:fix/11431-router-instance

Conversation

@inshu5757

Copy link
Copy Markdown

Problem

Fixes #11431

When InternalRouter rerenders, it creates a new router instance. During logout, if the <Admin> component rerenders while authProvider.logout() is pending, navigation can target an outdated router instance, resulting in a blank page.

Solution

Initialize the hash router only once using lazy React.useState. This prevents the router from being recreated on every render.

Added a unit test to verify that rerendering the parent component does not recreate the router.

How To Test

  1. Run:
    yarn test-unit packages/ra-core/src/routing --runInBand
  2. Verify that all 5 test suites and 29 tests pass.
  3. Verify that the new regression test confirms createHashRouter is called only once after a parent rerender.

Additional Checks

  • The PR targets master for a bugfix or a documentation fix, or next for a feature
  • The PR includes unit tests (if not possible, describe why)
  • The PR includes one or several stories (not applicable: this is an internal router lifecycle bugfix)
  • The documentation is up to date (no documentation changes required)

@fzaninotto

Copy link
Copy Markdown
Member

Thanks for the PR, and for digging into this.

The diagnosis is right - creating the router in the render body is the bug - but useState(() => createHashRouter(...)) has a side effect that makes it unsafe to merge as is.

The route element is <>{children}</>, and the router keeps the element it was created with. If the router is only created on the first render, that element stays the first render's snapshot: the whole tree under <Admin> is frozen, and anything coming from above <Admin> stops updating. For instance:

const App = () => {
    const [config, setConfig] = useState(null);
    useEffect(() => {
        fetchConfig().then(setConfig);
    }, []);
    return (
        <Admin dataProvider={dataProvider}>
            <Resource name="posts" {...posts} />
            {config?.betaEnabled && <Resource name="experiments" {...experiments} />}
        </Admin>
    );
};

With this patch, the experiments resource never appears. I checked it in a browser: I applied your change to a build of react-admin 5.15.5, running React 19.3.1 and react-router 7.18.4. The blank page from the issue is indeed fixed, but a label passed as children of <Admin> stays on its initial value after a parent state change, where it updates without the patch. The basename is frozen for the same reason.

The unit test can't catch this, because it counts calls to createHashRouter: a router created once with frozen children passes it just as well as a correct one.

I've opened #11435 with an alternative: the router is still created once, but the route element reads the children from a context placed above the RouterProvider, so children updates keep reaching the router, and RouterWrapper remounts the router when basename changes. It comes with three tests, each of which fails without the corresponding part of the fix - including one that reproduces the lost router state with the versions used in our CI (a navigation blocked by useBlocker disappearing on a parent rerender).

Your analysis of the root cause is what made that fix straightforward, so thank you for it.

@fzaninotto fzaninotto closed this Nov 1, 2026
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.

Blank page after logout when the app re-renders <Admin> during authProvider.logout()

2 participants