npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

parse-offline

v1.0.6

Published

Parse JS SDK Addons for handling offline for PWAs

Downloads

6

Readme

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:

  • [x] 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.

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

  • [ ] 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