-
Notifications
You must be signed in to change notification settings - Fork 40
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
detect offline client #129
Conversation
packages/webapp/src/pages/index.tsx
Outdated
@@ -37,6 +38,8 @@ const Home: FablePage = ({ isHydrated }) => { | |||
const isRightNetwork = !notConnected && chainSupported | |||
const isWrongNetwork = !notConnected && !chainSupported | |||
|
|||
useOfflineCheck(); | |||
|
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.
Sorry I forgot this one is the last review, could you add this to _app.tsx
instead, so it applies to all pages?
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.
sure
}); | ||
return response.ok; | ||
} catch (error) { | ||
throw error; // Rethrow for retries |
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 redundant, you could remove the whole try-catch
statement and it would work the same.
}, retryDelay); | ||
} else { | ||
setIsOffline(true); | ||
toast.error("App is offline. Please check your internet connection."); |
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.
Add an option object here with { dismissible: false, duration: Infinity }
which prevents the toast from closing.
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'm wondering if we need this at all — can't we just handle the "online" and "offline" events? On "online" do what you do in handleOnline
, on "offline" do setIsOffline
and the toast. What's the reasoning here? Is there some documentation around the behaviour and reliability of these events?
.catch(() => { | ||
setIsOffline(true); | ||
toast.error("App is offline. Please check your internet connection."); | ||
}); |
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.
The toast is triggered many times, which we should fix.
I'm wondering if this isn't the problem, as this is rerun every time useOfflineCheck
is rerun, which is every time that the page is rendered.
Another possible source of the issue is that the "offline" event is fired multiple times? Is that the case?
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 think i figured why the toast is rendering multiple times, it is being called both in the performNetworkCheck()
function and the handleOffline.
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.
Did you try the codebase and shut down your wifi to see if this works correctly?
function useOfflineCheck(options: any = {}) { | ||
const { | ||
// Optional configuration properties: | ||
threshold = 1000, // Minimum interval between offline checks (ms) |
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 never used.
performNetworkCheck() | ||
.then(() => handleOnline()) // online | ||
.catch(() => { | ||
setRetries(retries + 1); // Retry if error occurs |
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'm quite dubious on the retry logic here, as we don't actually enqueue any retry! The only way retries can be attempted is if the "offline" event fire multiple times. Is that the intention here? But if so, the retryDelay
variable is a little bit misleading, as it simply adds to the delay between multiple firings of "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.
for the retry logic, we only want to show our app is offline if `retries === maxRetries. so the reason for that.
@norswap please i need a review on this PR. |
Hey, time is a bit tight right now, but I'll get to it soon. Don't worry, this'll be considered for ODHack. |
I understand. Thank you. |
} | ||
}; | ||
|
||
window.addEventListener("online", handleOffline); |
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 seems unecessary to recheck the network state when receiving "online".
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 checked to dismiss the toast.
}); | ||
}, retryDelay); | ||
} | ||
}; |
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.
The retry logic still doesn't make sense.
handleOffline
is calledperformNetworkCheck
is called- in case of failure (
catch
) the retry count is incremented by 1 - ??? — nothing in the code makes
performNetworkCheck
orhandleOffline
be called again
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.
Do we really need retry logic in the first place? We could just assume offline after a single failure.
The risk of assuming after single failure is that it's a fluke and the offline
event and subsequent online
events are never sent, in which case we stay in offline mode forever.
Do we even need performNetworkCheck
at all? Can't we only rely on online
and offline
?
It's possible that these are not always reliable and it's recommend to perform explicit checks, but I'd like to hear your reasoning about this + some documentation showing that indeed online
/ offline
is not to be relied on.
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.
Hmmmm, it might be complicating things. The first option I opted for was using the online
and offline
, but seeing some documentation that suggests online/offline might not be relied on that's why I opted for doing a network check and retrying the request if it fails the first time.
In my opinion, the unreliability of theonline
/ offline
event might be rare and we can tackle it with listening for both online and offline. I implemented a version that uses the online
/ offline
and they both work. I will push the changes for review.
Cc: @norswap
.catch(() => { | ||
setIsOffline(true); | ||
toast.error("App is offline. Please check your internet connection."); | ||
}); |
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.
Did you try the codebase and shut down your wifi to see if this works correctly?
Hey, any updates on this? |
Yess. I have fixed the implementation to just rely on offline and online events. And it works when I switched off WiFi and turned it on. |
cc @norswap |
Sorry, I missed that extra commit. Looks good, could you rebase on top of master and make sure |
Done ✅. Cc: @norswap |
Hi norswap.Just reminding you of this PR, make check have passed. |
Hey, I see you merged instead of rebasing, do you think you could get that fixed? If not, it's okay, just let me know and I'll perform the rebase myself :) |
I am running into some issues with it. could you help please? thank you. |
it's me again 🙂...just wanna check if the or is merged. Have a productive week ahead. Cc: @norswap |
Hey, I'm unfortunately super buy and hadn't time to deal with this, but I'll get it sorted! |
8fc4b62
to
4680398
Compare
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.
Let's go! 🚀
finally 😃!!I feel so elated looking forward to contributing more to this project. |
Context (Problem, Motivation, Solution)
Link related issues!
i ran into git conflict so i had to pull. open another PR
#126 #35
Describe Your Changes
Checklist
make check
and fixed resulting issuesTesting
I implemented a modal to display when our app is offline. so how did i test this, i tested this by throttling the network in my devtools and also disconneCTING my device from wifi and this modal appears on the index.tsx and play.tsx page. when i toggle back or reconnect, the modal disaappears
CC: @norswap