Problem/Motivation

If a user loses internet connectivity then currently no autosaving will be possible anymore, but we could and should take advantage of the browser local storage and use it in such a case.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

hchonov created an issue. See original summary.

grienauer’s picture

+1 for this feature from me.
Is there an idea if this could be a possibility/feature which could be implemented?

mostepaniukvm’s picture

We are currently discussing a possible fallback approach for cases where autosave cannot reach the server.

The idea would be to implement an optional feature in the module.
Backup process change:
- if an AJAX request fails, store the same outgoing payload in localStorage
- continue autosaving while offline and keep updating the localStorage backup
Restore process change
- before showing the recovery dialog, also check whether a newer backup exists in localStorage
- if a backup exists in localStorage and the user chooses to restore it, first send that backup to the server, then use it to reload the form

Does this sound like a reasonable direction, or are there known limitations that would make this approach unreliable?

jvdkolk’s picture

I think <input type="file" /> will be a problem: Its value will be a read-only reference to the file (because of security), so you cannot use it to restore its value (source: https://developer.mozilla.org/en-US/docs/Web/API/FileList).

Folks on StackOverflow read the file and store it as a base64 encoded string (e.g. https://stackoverflow.com/questions/75479585/how-to-save-an-image-input-...), but localStorage has a 5 Mb limit on the amount of data it can store (https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quo...)

One could argue though, that the referenced file will not get lost when there is no internet, as it is on the user's computer.

jvdkolk’s picture

mostepaniukvm’s picture

Status: Active » Needs review

In the PR, I added an implementation as an optional feature of the module that can be enabled on the settings page.

In case of an AJAX error, it will continue saving backups to localStorage. Once the server becomes reachable and the user reloads the form, it will try to restore the latest backup from localStorage.

The difference compared to the auto_save_form module is that the restore does not happen on the front end. When the user chooses to restore, it sends data from localStorage to the back end and continues with the existing restore procedure.

a.milkovsky’s picture

Status: Needs review » Reviewed & tested by the community

Tested the MR. It works great!