Is there any way to safely retain/save data (settings) other than IndexedDB and manual saving (prompt the user with a dialog box to save)?
Basically I am writing an offline application and will be bundled and deployed as a local html file (e.g. file:///D:/test/index.html, no servers, just run by double-clicking the html file). It will have some settings and I need to be able to save these settings locally. The first time the app boots up, I retrieve the settings from a app-settings.json
file and save it to IndexedDB.
However, my problem is that when the user clears the browser data, the settings will be deleted as well.
I do not want to prompt the user with a dialog box asking to save the app-settings.json
everytime an update is made to the settings. (It would also be prone to duplicate, misplaced settings)
Any suggestion on how to go about this? How can I safely retain the application settings without any servers? Maybe there is an offline database of some sort that can be used in scenarios like this?
Would appreciate any help here! I'm desperately out of ideas :(
Thank you!
CodePudding user response:
// Check if site's storage has been marked as persistent
if (navigator.storage && navigator.storage.persist) {
const isPersisted = await navigator.storage.persisted();
console.log(`Persisted storage granted: ${isPersisted}`);
}
please enable persistent storage by enabling navigator.storage.persist, and I would suggest start using service workers to manage offline applications.
https://web.dev/persistent-storage/ https://developers.google.com/web/fundamentals/primers/service-workers
CodePudding user response:
You Can use the Use the Javascript File Api https://developer.mozilla.org/en-US/docs/Web/API/File_and_Directory_Entries_API
The dialog to save the file will be shown once, Then reading the file will not require a file dialog.
BTW
VS Code web also uses the api: https://vscode.dev/