GithubHelp home page GithubHelp logo

sergiodxa / swr-sync-storage Goto Github PK

View Code? Open in Web Editor NEW
109.0 3.0 4.0 1.09 MB

Synchronize SWR cache with localStorage or sessionStorage to get offline cache

License: MIT License

TypeScript 100.00%
swr swr-sync-storage sessionstorage localstorage web-storage

swr-sync-storage's Introduction

swr-sync-storage

CI Publish Maintainability Test Coverage

Synchronize SWR cache with localStorage or sessionStorage to get offline cache.

Usage

Install it

$ yarn add swr-sync-storage

Note: You will need to provide SWR v0.2.0-beta.0 or greater

import { syncWithStorage } from "swr-sync-storage";
syncWithStorage("local");
syncWithStorage("session");

You can also import to already bound versions of local or session storage.

import { syncWithLocalStorage } from "swr-sync-storage";
syncWithLocalStorage();
import { syncWithSessionStorage } from "swr-sync-storage";
syncWithSessionStorage();

Every function will return a new function to unsubscribe for cache changes.

swr-sync-storage's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar sergiodxa avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

swr-sync-storage's Issues

cause the swr's key serialization, it it should do not work with key of Array type

key serialization: https://github.com/vercel/swr/blob/master/src/cache.ts#L45
think of the using as follow

function App() {
  const {data} = useSWR('/api/a');
  const {data} = useSWR(['/api/list', {id: data.id}], fetcher);
}

the key may serialize to /api/list0,
{name: 'xxx', age: 'xxx'} will transform to a Incremental number

page1

function App() {
  const {data} = useSWR(['/api/list', {id: '1'}], fetcher);
}

page2

function App() {
  const {data} = useSWR(['/api/list', {id: '2'}], fetcher);
}

when i open page1 and page2, ['/api/list', {id: '1'}] and ['/api/list', {id: '2'}] will be serialized to /api/list0, but they stand for a different query. so they may use the error data

Request feature: Choose what key save in localstorage

Hi, if I use load more scroll, that will be much data in localstorage.
I hope you can edit more to archive which key should we cache

I want something like this
import { syncWithStorage } from "swr-sync-storage";
syncWithStorage("local",["key-i-want-to-save"]); // this key just in localstorage, not in session too. And regex will be more great i think
syncWithStorage("session",["key-i-want-to-save"]);

Help with alternatives

Hi. I keep stumbling across this repo and your comments on other peoples issues.
It seems this packages is not maintained anymore - could you recommend any alternatives?

I have already tried react query, but prefer SWR, but maybe you know of others?

Kind regards
Dennis

Example

Hi can you make a simple example for use this? I am trying to save public user info to local.
I use localStorage with SWR but it not work well.

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.