Skip to content

Commit b8d1878

Browse files
authored
Merge pull request #10776 from marmelab/fix/edit-redirect-loop
Fix(show/edit): Avoid a redirect loop when useGetOne returns an error in shadcn-admin-kit
2 parents 8f25468 + b3758ae commit b8d1878

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

packages/ra-core/src/controller/edit/useEditController.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { useParams } from 'react-router-dom';
33

44
import { useAuthenticated, useRequireAccess } from '../../auth';
55
import { RaRecord, MutationMode, TransformData } from '../../types';
6-
import { useRedirect, RedirectionSideEffect } from '../../routing';
6+
import {
7+
useRedirect,
8+
RedirectionSideEffect,
9+
useCreatePath,
10+
} from '../../routing';
711
import { useNotify } from '../../notification';
812
import {
913
useGetOne,
@@ -82,6 +86,7 @@ export const useEditController = <
8286
const getRecordRepresentation = useGetRecordRepresentation(resource);
8387
const translate = useTranslate();
8488
const notify = useNotify();
89+
const createPath = useCreatePath();
8590
const redirect = useRedirect();
8691
const refresh = useRefresh();
8792
const { id: routeId } = useParams<'id'>();
@@ -121,7 +126,15 @@ export const useEditController = <
121126
notify('ra.notification.item_doesnt_exist', {
122127
type: 'error',
123128
});
124-
redirect('list', resource);
129+
// We need to flushSync to ensure the redirect happens before the refresh
130+
// Otherwise this can cause an infinite loop when the record is not found
131+
redirect(() => ({
132+
pathname: createPath({
133+
resource,
134+
type: 'list',
135+
}),
136+
flushSync: true,
137+
}));
125138
refresh();
126139
},
127140
refetchOnReconnect: false,

packages/ra-core/src/controller/show/useShowController.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
UseGetOneOptions,
1010
} from '../../dataProvider';
1111
import { useTranslate } from '../../i18n';
12-
import { useRedirect } from '../../routing';
12+
import { useCreatePath, useRedirect } from '../../routing';
1313
import { useNotify } from '../../notification';
1414
import {
1515
useResourceContext,
@@ -80,6 +80,7 @@ export const useShowController = <
8080
const getRecordRepresentation = useGetRecordRepresentation(resource);
8181
const translate = useTranslate();
8282
const notify = useNotify();
83+
const createPath = useCreatePath();
8384
const redirect = useRedirect();
8485
const refresh = useRefresh();
8586
const { id: routeId } = useParams<'id'>();
@@ -109,7 +110,16 @@ export const useShowController = <
109110
notify('ra.notification.item_doesnt_exist', {
110111
type: 'error',
111112
});
112-
redirect('list', resource);
113+
114+
// We need to flushSync to ensure the redirect happens before the refresh
115+
// Otherwise this can cause an infinite loop when the record is not found
116+
redirect(() => ({
117+
pathname: createPath({
118+
resource,
119+
type: 'list',
120+
}),
121+
flushSync: true,
122+
}));
113123
refresh();
114124
},
115125
retry: false,

0 commit comments

Comments
 (0)