5 ms·
> If a user is currently offline, we’ll hold off submitting the form for now Be careful not to completely rely on navigator.onLine for this as your next reques
by dspillett 9y ago
> If a user is currently offline, we’ll hold off submitting the form for now
Be careful not to completely rely on navigator.onLine for this as your next request could be the first to fail, so you need to deal with submission errors and timeouts too (or safer: always put the data in local storage and remove once you have confirmation of a successful save).
The article seems good overall though, I can forgive leaving out some of the nitty-gritty like this to keep the main points clear.
- hire_charts 9y agoOn top of that, I'd consider adding a bit of idempotency to the form submission -- to make sure that if the request did in fact succeed even though you did not get the confirmation of a success, subsequent retry/resubmit attempts won't result in duplicate entries. A rough way to do this would be to add a hidden UUID value in the form data that is generated once up front and used by both the client and the server to ensure that the result of the first successful request can be cached and re-returned.
- agentultra 9y agoThe approach of using versioned objects with Etag/If-None-Match and appropriate responses works rather well: http://www.clairereynaud.net/blog/adding-offline-mode-to-your-mobile-app/ http://www.clairereynaud.net/blog/adding-offline-mode-to-you...
- nfriedly 9y agoAgreed, although if you look at the code, they do in fact do that - it's only removed from localStorage after receiving a 200 status response. Any other status code will leave the data in localStorage and display an error message.
- marksomnian 9y agoThe problem then becomes what happens when the server receives the request, processes and stores the data, but loses connection before the client can receive the 200. In this case, the client will assume the submission failed and will retry the submission - creating two records.
- jaggederest 9y agoYou really have to pick whether you want possible additional copies or possibly zero copies. This isn't really a problem with this particular implementation. I think it's interesting to have the possibility of additional copies versus the standard web form behavior of at-most-one copy.
- haikuginger 9y ago> You really have to pick whether you want possible additional copies or possibly zero copies. You really don't. You can, as mentioned above, have a hidden UUID field in the form that's used serverside to deduplicate submissions within a given timeframe.
- LunaSea 9y agoWouldn't you also need to regenerate CSRF tokens somehow so that the second submission doesn't reuse the initial CSRF token?
- haikuginger 9y agoDepends on how often your CSRF tokens expire. For example, Django keeps the same token for the entire session by default IIRC.
- yebyen 9y agoThat wouldn't be a successful request... if the user gets an error from your application, they know to try again. (Or more likely and more on-topic, for the offline-friendly form if the application gets the error first, the application could be smart enough to try again...) The UUID doesn't guarantee anything other than capability to prevent duplicate records from duplicate submissions. Something else has to be responsible to make sure the submission is not abandoned without user input, unless a 200 or other successful status is received.
- AgentME 9y agoThere's no reason to expire CSRF tokens after each request.
- TomMarius 9y agoAnother pitfall is that navigator.onLine can be true even though the device has no access to the Internet.