-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add support for offline/local first applications #10545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Conversation
EditPostPage.setInputValue( | ||
'input', | ||
'title', | ||
'Lorem Ipsum again{enter}' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because react-query now persist queries and mutations for offline mode, the previous test now leaks into the second (e.g. this post has its title changed to Lorem Ipsum). I tried to configure testIsolation in Cypress but our version is probably too old
d440e8e
to
9d66e05
Compare
I can't see a way to test this except e2e. Not sure if we can simulate network loss with cypress though |
@djhi since the simple demo has the react-query DevTools enabled, you can use them to simulate network loss I believe (clicking on the 'Wifi' icon does the job). A bit ugly but it should work! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Praise: very understandable doc !!
packages/ra-core/src/controller/input/useReferenceInputController.ts
Outdated
Show resolved
Hide resolved
The 'how to test' instructions must be updated, as the tester must select the optimistic mutation mode before submitting the form. |
When doing an optimistic offline mutation, the loader in the app bar keeps spinning forever even tough the optimistic update is already applied. Is it a good UI? Instead, I think we should have a way to mention the pending updates, e.g. using a badge on the loader icon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're getting closer!
examples/simple/src/Layout.tsx
Outdated
</AppBar> | ||
); | ||
const MyAppBar = () => { | ||
const isOnline = useIsOnline(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd name the hook useIsOffline
instead, since you only ever check that it's false.
) | ||
``` | ||
{% endraw %} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're missing a sentence to explain that the setup is done here - the rest is only for developers with custom methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
@@ -95,6 +117,10 @@ const Root = styled('div', { | |||
[`& .${EditClasses.noActions}`]: { | |||
marginTop: '1em', | |||
}, | |||
[`& .${EditClasses.offline}`]: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code smell: The Offline component should work as is, without CSS customization.
<div> | ||
<Stack direction="row" alignItems="center" gap={1}> | ||
<ErrorIcon role="presentation" color="error" fontSize="small" /> | ||
<span style={visuallyHidden}> | ||
{typeof error === 'string' ? error : error?.message} | ||
</span> | ||
</div> | ||
<Typography | ||
component="span" | ||
variant="body2" | ||
sx={{ color: 'error.main' }} | ||
> | ||
<Translate i18nKey="ra.notification.http_error"> | ||
Server communication error | ||
</Translate> | ||
</Typography> | ||
</Stack> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've changed the content and design here. Can you elaborate on why you did that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only had an icon for error before with no message for users not using assistive tools. We discussed this together
} | ||
|
||
if ( | ||
!record || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is already in the previous condition
{...rest} | ||
offline={ | ||
offline ?? ( | ||
<Labeled {...rest}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels weird to repeat rest
several times, plus some aren't compatible with Labeled
. I wouldn't apply them here, and pass the source
and label
props explicitly instead.
@@ -13,6 +13,7 @@ export * from './ListGuesser'; | |||
export * from './ListNoResults'; | |||
export * from './ListToolbar'; | |||
export * from './ListView'; | |||
export * from '../Offline'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reexported from the wrong directory (the ..
is weird here)
Co-authored-by: Francois Zaninotto <[email protected]>
The loading indicator now has a badge when users are offline and there are pending mutations. This badge also has a tooltip that explains it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only reviewed the docs and the simple demo so far...
|
||
queryClient.setMutationDefaults('banUser', { | ||
mutationFn: async (userId) => { | ||
return dataProviderFn.banUser(userId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return dataProviderFn.banUser(userId); | |
return dataProvider.banUser(userId); |
"@tanstack/query-sync-storage-persister": "5.47.0", | ||
"@tanstack/react-query": "^5.21.7", | ||
"@tanstack/react-query-devtools": "^5.21.7", | ||
"@tanstack/react-query-persist-client": "5.47.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"@tanstack/query-sync-storage-persister": "5.47.0", | |
"@tanstack/react-query": "^5.21.7", | |
"@tanstack/react-query-devtools": "^5.21.7", | |
"@tanstack/react-query-persist-client": "5.47.0", | |
"@tanstack/query-sync-storage-persister": "^5.47.0", | |
"@tanstack/react-query": "^5.21.7", | |
"@tanstack/react-query-devtools": "^5.21.7", | |
"@tanstack/react-query-persist-client": "^5.47.0", |
/** | ||
* When react-query resumes persisted mutations through their default functions (provided in the getOfflineFirstQueryClient file) after the browser tab | ||
* has been closed, it cannot handle their side effects unless we set up some defaults. In order to leverage the react-admin notification system | ||
* we add a default onSettled function to the mutation defaults here. | ||
*/ | ||
const NotificationsFromQueryClient = () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bump
|
||
export const queryClient = new QueryClient(); | ||
|
||
const queryClientWithOfflineSupport = addOfflineSupportToQueryClient({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point of queryClientWithOfflineSupport
variable? Why is it not exported instead of queryClient
? Is queryClient
mutated when you use addOfflineSupportToQueryClient
, so exporting it is not required? Current variable naming makes me think that queryClient
doesn't have offline support.
EDIT: in following examples, it's much clearer.
const queryClientWithOfflineSupport = addOfflineSupportToQueryClient({ | |
const baseQueryClient = new QueryClient(); | |
export const queryClient = addOfflineSupportToQueryClient({ | |
queryClient, | |
dataProvider, | |
resources: ['posts', 'comments'], | |
}); |
const translate = useTranslate(); | ||
|
||
if (isPaused && (data == null || isPlaceholderData)) return <Offline />; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a quick example about this in docs? At least something mentioning that you can use isPaused
and isPlaceholderData
to detect that you're in offline mode.
// be possible if the app was also a PWA. | ||
// We only do that for optimistic and undoable modes in order to not break any existing tests that expect | ||
// the id to be generated by the server (e.g. by FakeRest). | ||
let next_id = 100; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 100? You could technically already have another post with ID 100, isn't it?
For client-generated ids, I use negative numbers, to quickly notice that they are not real IDs, and is usually enough to ensure that they are not conflicting with persisted data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But they are real ids. They won't be replaced on the backend. I would use uuid but that's too much changes of the current data. Besides, we actually control the simple example data and know we don't have ids greater than 100. I believe this is enough to showcase and test the feature
@@ -61,6 +61,7 @@ export const useReferenceArrayInputController = < | |||
error: errorGetMany, | |||
isLoading: isLoadingGetMany, | |||
isFetching: isFetchingGetMany, | |||
isPaused: isPausedGetMany, | |||
isPending: isPendingGetMany, | |||
refetch: refetchGetMany, | |||
} = useGetManyAggregate<RecordType>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No isPlaceholderData
here? What's the difference with useReferenceArrayFieldController
?
* </PersistQueryClientProvider> | ||
* ); | ||
* | ||
* @example <caption>Adding offline support with custom mutations</caption> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't these examples mixed up? The first one shows with a custom mutation and the second one without it.
} | ||
|
||
interface ComponentsPropsList { | ||
RaOffline: Partial<Offline>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use [PREFIX]
instead of 'RaOffline'
.
|
||
interface Components { | ||
RaOffline?: { | ||
defaultProps?: ComponentsPropsList['RaOffline']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use typeof PREFIX
instead of 'RaOffline'
.
</Edit> | ||
); | ||
|
||
export const Offline = ({ dataProvider = dataProviderWithAuthors }) => ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested it and the default AutocompleteInput
shows "Loading..." instead of the offline component that I expected. I think you need to change useChoicesContext
to propagate isPaused
and isPlaceholderData
to show a better message in offline mode.
title={ | ||
<Translate | ||
i18nKey="ra.notifications.pending_operations" | ||
options={{ smart_count: 1 }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why { smart_count: 1 }
? Shouldn't it be { smart_count: pendingMutations.length }
?
); | ||
|
||
export const Offline = () => ( | ||
<Wrapper listContext={{ isPaused: true }}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This story shows nothing. Is it supposed to show the offline component?
Problem
React-admin currently don't handle well the loss of network connectivity: in any demo, if you go to a list page, then simulate being offline using the devtool and try to go to the second page of the same list, it will still display the first page data while the pagination will indeed indicate you are on page 2.
We handled it a bit better by at least showing users a notification when they try to load new data while offline but we can do better and allow for resumable mutations.
Solution
ListContext
include theisPaused
andisPlaceholderData
props from react-query and update components/hooks accordinglyempty
component when those two props are true (ideally we should have a dedicated component for that or handle the case in the default empty but this is achievable in userland)mutationKey
to all mutations so that we can provide default functions for them at thequeryClient
level (required by react-query: https://tanstack.com/query/latest/docs/framework/react/guides/mutations#persisting-offline-mutations)offline
prop to reference fields componentsoffline
prop to list componentsoffline
prop to show and edit viewsoffline
prop to reference inputs componentsScreenshots
Edit view

Show view

DataTable (same for all list child components

How To Test
Then:
optimistic
option in the form toolbar first.undoable
option in the form toolbar first.f00bar
as the post title to trigger an error once the network is online.In stories for list components, reference components and detail views, simply switch to
offline
and hit the storybook refresh buttonAdditional Checks
master
for a bugfix, ornext
for a featureAlso, please make sure to read the contributing guidelines.