Giter Site home page Giter Site logo

parse-offline's Introduction

Parse Offline

Parse JS SDK Addons for handling offline for PWAs

Bonus: No external dependencies

The problem

When you develop applications with Parse, you constantly need to fetch the information from the remote server, affecting the offline experience.

The solution

With this addon for the Parse JS SDK, you can:

  • Save query results in the localStorage for any class. With this, given a query, you could fetch its results and save them in the localStorage for later usage.

  • 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.

  • Edit/destroy objects, and save them when the app goes online.

How it works

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.

Examples

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.

In Typescript:

import * as Parse from 'parse';

In Javascript:

import Parse from 'parse';

Basic

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:

import * as Parse from 'parse';
import { ParseOffline } from 'parse-offline';

const query = new Parse.Query('Vehicle');
const results: Parse.Object[] = await ParseOffline.findWithFallbackAndCache({
  query
});

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.

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:

localStorage.getItem('_cache_Vehicle'); // [{ ... }]

Controlled cache time

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.

import * as Parse from 'parse';
import { ParseOffline } from 'parse-offline';

const ONE_WEEK = 60 * 60 * 24 * 7;

const query = new Parse.Query('Test');
const results: Parse.Object[] = await ParseOffline.findWithFallbackAndCache({
  query,
  options: { sessionToken: '123' },
  maxAge: ONE_WEEK,
});

With custom cache key

Sometimes, you have different queries for the same class and you want to cache those results.

import * as Parse from 'parse';
import { ParseOffline } from 'parse-offline';

// Here we save a cache for the elements of the class Post
const query = new Parse.Query('Post');
const results: Parse.Object[] = await ParseOffline.findWithFallbackAndCache({
  query,
  localStorageKey: 'my-custom-key'
});

// Here we save another cache for the elements of the class Post
const query2 = new Parse.Query('Post');
const results2: Parse.Object[] = await ParseOffline.findWithFallbackAndCache({
  query2,
  localStorageKey: 'my-custom-key2'
});

The results do not collide because the localStorageKey is different.

Want to contribute?

Open a PR with your contribution and I'll be glad to merge it.

Dev Features

  • Testing with Jest
  • Linting out of the box (checks the style of your code), with TSLint
  • Build, prepublish and other scripts to help you to develop
  • Works with Typescript: Static typing for your JS Applications, reducing amount of runtime errors
  • Coverage out of the box, thanks to Jest
  • Uses deterministic module resolving, with Yarn

Credits

Developed by Juan Camilo Guarín Peñaranda,
Otherwise SAS, Colombia
2018

License

MIT.

Support us on Patreon

patreon

parse-offline's People

Contributors

jcguarinpenaranda avatar renovate-bot avatar renovate[bot] avatar skyroller5 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

skyroller5

parse-offline's Issues

How to use this?

Hi,
Found ur repo and seems interesting.

But there doesn't seem to be a guide on using this package.

Mind giving some examples?

Thanks!

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency tslint to v6
  • chore(deps): update dependency typescript to v5
  • 🔐 Create all rate-limited PRs at once 🔐

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • @types/jest 22.2.2
  • @types/parse 2.4.7
  • jest 23.6.0
  • moment 2.22.0
  • parse 1.11.0
  • ts-jest 21.2.4
  • tslint 5.11.0
  • tslint-config-airbnb 5.3.0
  • typescript 2.8.1
  • @types/parse ^2.4.4
  • parse ^1.11.0

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Preset name not found within published preset config (monorepo:angularmaterial). Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.