Skip to content

Commit 193a1b4

Browse files
committed
Fix Show and Edit controllers can trigger a redirect loop if getOne rejects too quickly
1 parent 0d1be4c commit 193a1b4

File tree

2 files changed

+4
-33
lines changed

2 files changed

+4
-33
lines changed

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

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

44
import { useAuthenticated, useRequireAccess } from '../../auth';
55
import { RaRecord, MutationMode, TransformData } from '../../types';
6-
import {
7-
useRedirect,
8-
RedirectionSideEffect,
9-
useCreatePath,
10-
} from '../../routing';
6+
import { useRedirect, RedirectionSideEffect } from '../../routing';
117
import { useNotify } from '../../notification';
128
import {
139
useGetOne,
1410
useUpdate,
15-
useRefresh,
1611
UseGetOneHookValue,
1712
HttpError,
1813
UseGetOneOptions,
@@ -86,9 +81,7 @@ export const useEditController = <
8681
const getRecordRepresentation = useGetRecordRepresentation(resource);
8782
const translate = useTranslate();
8883
const notify = useNotify();
89-
const createPath = useCreatePath();
9084
const redirect = useRedirect();
91-
const refresh = useRefresh();
9285
const { id: routeId } = useParams<'id'>();
9386
if (!routeId && !propsId) {
9487
throw new Error(
@@ -126,16 +119,7 @@ export const useEditController = <
126119
notify('ra.notification.item_doesnt_exist', {
127120
type: 'error',
128121
});
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-
}));
138-
refresh();
122+
redirect('list', resource);
139123
},
140124
refetchOnReconnect: false,
141125
refetchOnWindowFocus: false,

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import { useAuthenticated, useRequireAccess } from '../../auth';
44
import { RaRecord } from '../../types';
55
import {
66
useGetOne,
7-
useRefresh,
87
UseGetOneHookValue,
98
UseGetOneOptions,
109
} from '../../dataProvider';
1110
import { useTranslate } from '../../i18n';
12-
import { useCreatePath, useRedirect } from '../../routing';
11+
import { useRedirect } from '../../routing';
1312
import { useNotify } from '../../notification';
1413
import {
1514
useResourceContext,
@@ -80,9 +79,7 @@ export const useShowController = <
8079
const getRecordRepresentation = useGetRecordRepresentation(resource);
8180
const translate = useTranslate();
8281
const notify = useNotify();
83-
const createPath = useCreatePath();
8482
const redirect = useRedirect();
85-
const refresh = useRefresh();
8683
const { id: routeId } = useParams<'id'>();
8784
if (!routeId && !propsId) {
8885
throw new Error(
@@ -110,17 +107,7 @@ export const useShowController = <
110107
notify('ra.notification.item_doesnt_exist', {
111108
type: 'error',
112109
});
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-
}));
123-
refresh();
110+
redirect('list', resource);
124111
},
125112
retry: false,
126113
...otherQueryOptions,

0 commit comments

Comments
 (0)