-
Notifications
You must be signed in to change notification settings - Fork 0
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
IndexedDB or Local Storage for saving data when offline? #8
Comments
@SimonLab good thinking. 👍 |
Backround Sync and Sync Manager. Unfortunately these APIs are specific to the Chrome/Firefox |
@SimonLab good researching. 👍 It could be worth reading: <script src="//cdn.jsdelivr.net/npm/pouchdb@7.1.1/dist/pouchdb.min.js"></script>
<script>
var db = new PouchDB('my_database');
</script> Docs are much better than last time I checked. Thoughts? (also, send photo of your painting progress to group chat!) 😊 |
Currently reading the doc on how to use PouchDB.
At the moment the Elm PWA app is using the API build with Phoenix and Postgres to save items. I haven't looked how PouchDB synchronises with CouchDB but if there is a way to avoid writting the logic code this might be worth it. I'm just not sure how easy it is to replace Postgres by CouchDB, I'm not sure there is an easy way to use Ecto and CouchDB together. |
Following the getting started guide for PouchDB https://pouchdb.com/getting-started.html
port pouchDB : String -> Cmd msg saveCapture : Bool -> String -> Cmd Msg
saveCapture appOnline capture =
if appOnline then
Http.post
{ url = "https://dwylapp.herokuapp.com/api/captures/create"
, body = Http.jsonBody (captureEncode capture)
, expect = Http.expectJson SaveCaptureResult captureDecoder
}
else
-- if not online save the item in PouchDB via ports
pouchDB capture on the javascript side we can for now create a dummy function: app.ports.pouchDB.subscribe(function(capture) {
console.log("capture offline", capture)
}); This subscribe function will later on call the pouchDB api to save the capture:
function saveCapture(capture) {
var capture = {
_id: new Date().toISOString(),
text: capture
};
db.put(capture, function callback(err, result) {
if (!err) {
console.log('Successfully saved capture!');
}
});
}
function showCaptures() {
db.allDocs({include_docs: true, descending: true}, function(err, doc) {
console.log(doc.rows)
});
} |
The above issue describe quickly the basic "save and get" functions for adding/retrieving captures from PouchDB. Now that we now how do the basic we need to see what are our best options to synchronise data between the saved data on the server and the saved data on the front-end and how to resolve and conflicts (captures updated/deleted from different places at the same time) |
https://developers.google.com/web/fundamentals/instant-and-offline/web-storage/offline-for-pwa
https://developers.google.com/web/ilt/pwa/live-data-in-the-service-worker
The request/response are saved with the cache api (https://developer.mozilla.org/en-US/docs/Web/API/Cache)
The recommended api for saving other data is indexedDB (https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) as the localStorage api is not supported with service worker
The text was updated successfully, but these errors were encountered: