Skip to content

Commit ea9d054

Browse files
added some documentation
1 parent ecee235 commit ea9d054

File tree

1 file changed

+101
-3
lines changed

1 file changed

+101
-3
lines changed

README.md

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
# Parse Offline (Work in progress)
1+
# Parse Offline
22

33
Parse JS SDK Addons for handling offline for PWAs
44

55
Bonus: No external dependencies
66

7+
<!-- TOC -->
8+
9+
- [Parse Offline](#parse-offline)
10+
- [The problem](#the-problem)
11+
- [The solution](#the-solution)
12+
- [How it works](#how-it-works)
13+
- [Examples](#examples)
14+
- [Basic](#basic)
15+
- [Controlled cache time](#controlled-cache-time)
16+
- [With custom cache key](#with-custom-cache-key)
17+
- [Want to contribute?](#want-to-contribute)
18+
- [Dev Features](#dev-features)
19+
- [Credits](#credits)
20+
- [License](#license)
21+
- [Support us on Patreon](#support-us-on-patreon)
22+
23+
<!-- /TOC -->
24+
725
## The problem
826

927
When you develop applications with Parse, you constantly need to fetch the information from the remote server, affecting the offline experience.
@@ -16,11 +34,91 @@ With this addon for the Parse JS SDK, you can:
1634

1735
* [x] Have a synced cache for your results in the `localStorage` for any class. So everytime you make a request, you can keep a local cache of the results, and display them when your app goes offline.
1836

19-
* [ ] Save in the Service Worker cache images from the results.
37+
* [ ] Edit/destroy objects, and save them when the app goes online.
2038

2139
## How it works
2240

23-
Because the Parse JS SDK contacts your Parse Server via POST requests, and saves the user token in `localStorage`, it is currently not possible to handle retries via Service Worker. So, the `localStorage` is used to
41+
Because the Parse JS SDK contacts your Parse Server via POST requests, and saves the user token in `localStorage`, it is currently not possible to handle retries via Service Worker. To solve this problem, the `localStorage` is used to save sets of results that you might need to see when the app goes offline.
42+
43+
## Examples
44+
45+
Please be aware that the following examples are given in Typescript. The main difference between the Typescript and the Javascript examples is the way in which `parse` is imported.
46+
47+
In Typescript:
48+
```ts
49+
import * as Parse from 'parse';
50+
```
51+
52+
In Javascript:
53+
```ts
54+
import Parse from 'parse';
55+
```
56+
57+
### Basic
58+
In this example, we get a set of results from the database, and save them to the localStorage. To do this, we call `findWithFallbackAndCache` as follows:
59+
60+
```ts
61+
import * as Parse from 'parse';
62+
import { ParseOffline } from 'parse-offline';
63+
64+
const query = new Parse.Query('Vehicle');
65+
const results: Parse.Object[] = await ParseOffline.findWithFallbackAndCache({
66+
query
67+
});
68+
```
69+
70+
`findWithFallbackAndCache` always tries to fetch the most recent content, and only if the navigator is not online it returns the elements saved in the cache.
71+
72+
The results are saved in the `localStorage`, available in the key `_cache_:className`. So, you could do the following, to get the items saved in the browser's local storage:
73+
74+
```ts
75+
localStorage.getItem('_cache_Vehicle'); // [{ ... }]
76+
```
77+
78+
79+
### Controlled cache time
80+
81+
In this example, we set a maximum age to the fetched results. If the results are still valid, they are returned. Else, the library returns an empty array.
82+
83+
```ts
84+
import * as Parse from 'parse';
85+
import { ParseOffline } from 'parse-offline';
86+
87+
const ONE_WEEK = 60 * 60 * 24 * 7;
88+
89+
const query = new Parse.Query('Test');
90+
const results: Parse.Object[] = await ParseOffline.findWithFallbackAndCache({
91+
query,
92+
options: { sessionToken: '123' },
93+
maxAge: ONE_WEEK,
94+
});
95+
```
96+
97+
### With custom cache key
98+
99+
Sometimes, you have different queries for the same class and you want to cache those results.
100+
101+
```ts
102+
import * as Parse from 'parse';
103+
import { ParseOffline } from 'parse-offline';
104+
105+
// Here we save a cache for the elements of the class Post
106+
const query = new Parse.Query('Post');
107+
const results: Parse.Object[] = await ParseOffline.findWithFallbackAndCache({
108+
query,
109+
localStorageKey: 'my-custom-key'
110+
});
111+
112+
// Here we save another cache for the elements of the class Post
113+
const query2 = new Parse.Query('Post');
114+
const results2: Parse.Object[] = await ParseOffline.findWithFallbackAndCache({
115+
query2,
116+
localStorageKey: 'my-custom-key2'
117+
});
118+
```
119+
120+
The results do not collide because the `localStorageKey` is different.
121+
24122

25123
## Want to contribute?
26124

0 commit comments

Comments
 (0)