nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: Kong/swrv
Pull request #50 (2020-06-20 21:53:36)

fix(cache) serve from cache across components
- move the if statement down some lines so cache can be served across components as initially intended.


Patch:
diff --git a/src/use-swrv.ts b/src/use-swrv.ts
index 8a30833..101224a 100644
--- a/src/use-swrv.ts
+++ b/src/use-swrv.ts
@@ -154,7 +154,6 @@ export default function useSWRV<Data = any, Error = any> (key: IKey, fn?: fetche
    */
   const revalidate = async () => {
     const keyVal = keyRef.value
-    if (!fn) return
     if (!isDocumentVisible()) { return }
     const cacheItem = config.cache.get(keyVal, ttl)
     let newData = cacheItem && cacheItem.data
@@ -165,6 +164,7 @@ export default function useSWRV<Data = any, Error = any> (key: IKey, fn?: fetche
       stateRef.error = newData.error
     }
 
+    if (!fn) return
     /**
      * Currently getter's of SWRVCache will evict
      */


Test patch:
diff --git a/tests/use-swrv.spec.tsx b/tests/use-swrv.spec.tsx
index a94cee6..fd0255d 100644
--- a/tests/use-swrv.spec.tsx
+++ b/tests/use-swrv.spec.tsx
@@ -256,6 +256,46 @@ describe('useSWRV', () => {
     expect(invoked).toBe(1) // empty fetcher is OK
     done()
   })
+
+  it('should return cache when no fetcher provided, across components', async done => {
+    let invoked = 0
+
+    const Hello = (cacheKey: string) => {
+      return defineComponent({
+        template: '<div>hello {{fromCache}}</div>',
+        setup () {
+          const { data: fromCache } = useSWRV(cacheKey)
+          return { fromCache }
+        }
+      })
+    }
+
+    const vm = new Vue({
+      template: `<div>data:{{ data }} <Hello v-if="data" /></div>`,
+      components: { Hello: Hello('cache-key-6') },
+      setup  () {
+        const fetcher = () => {
+          invoked += 1
+          return new Promise(res => setTimeout(() => res('SWR'), 200))
+        }
+        const { data } = useSWRV('cache-key-6', fetcher)
+
+        return { data }
+      }
+    }).$mount()
+
+    expect(invoked).toBe(1)
+
+    expect(vm.$el.textContent).toBe('data: ')
+    expect(invoked).toBe(1)
+    timeout(200)
+    await tick(vm, 2)
+
+    timeout(200)
+    expect(vm.$el.textContent).toBe('data:SWR hello SWR')
+    expect(invoked).toBe(1) // empty fetcher is OK
+    done()
+  })
 })
 
 describe('useSWRV - loading', () => {
Repository: Kong/swrv
Pull request #124 (2020-12-21 16:20:22)

fix(cache/localStorage) remove encoding, fix TypeError
Fixes `TypeError: Cannot read property 'expiresAt' of undefined` and removes encoding so that localStorage can include utf string.


Patch:
diff --git a/src/cache/adapters/localStorage.ts b/src/cache/adapters/localStorage.ts
index a9093c3..f8a7bf9 100644
--- a/src/cache/adapters/localStorage.ts
+++ b/src/cache/adapters/localStorage.ts
@@ -12,16 +12,16 @@ export default class LocalStorageCache extends SWRVCache<any> {
     this.STORAGE_KEY = key
   }
 
-  private encode (storage) { return btoa(JSON.stringify(storage)) }
-  private decode (storage) { return JSON.parse(atob(storage)) }
+  private encode (storage) { return JSON.stringify(storage) }
+  private decode (storage) { return JSON.parse(storage) }
 
   get (k) {
     const item = localStorage.getItem(this.STORAGE_KEY)
     if (item) {
       const _key = this.serializeKey(k)
-      const itemParsed: ICacheItem<any> = JSON.parse(atob(item))[_key]
+      const itemParsed: ICacheItem<any> = JSON.parse(item)[_key]
 
-      if (itemParsed.expiresAt === null) {
+      if (itemParsed?.expiresAt === null) {
         itemParsed.expiresAt = Infinity // localStorage sets Infinity to 'null'
       }
 


Test patch:
diff --git a/tests/cache.spec.tsx b/tests/cache.spec.tsx
index 2f5c0fb..4d04b11 100644
--- a/tests/cache.spec.tsx
+++ b/tests/cache.spec.tsx
@@ -35,7 +35,7 @@ describe('cache - adapters', () => {
 
       await timeout(100)
 
-      const localStorageData: Record<string, ICacheItem<any>> = JSON.parse(atob(localStorage.getItem('swrv')))
+      const localStorageData: Record<string, ICacheItem<any>> = JSON.parse(localStorage.getItem('swrv'))
 
       expect(localStorage.getItem('swrv')).toBeDefined()
       expect(localStorageData).toHaveProperty('/api/users')
@@ -59,7 +59,7 @@ describe('cache - adapters', () => {
 
       expect(localStorage.getItem('swrv')).toBeDefined()
       const checkStorage = (key): Record<string, ICacheItem<any>> => {
-        return JSON.parse(atob(localStorage.getItem('swrv')))[key]
+        return JSON.parse(localStorage.getItem('swrv'))[key]
       }
 
       await timeout(100)
@@ -92,7 +92,7 @@ describe('cache - adapters', () => {
         if (!db) {
           return undefined
         }
-        return JSON.parse(atob(db))[key]
+        return JSON.parse(db)[key]
       }
 
       await timeout(100)
Repository: Kong/swrv
Pull request #197 (2021-06-15 12:31:14)

docs(readme) fix incorrect `revalidate`
Change the incorrect return value name `revalidate` to `mutate`

CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/Kong/swrv?pullRequest=197) <br/>Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/Kong/swrv?pullRequest=197) before we can accept your contribution.<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/Kong/swrv?pullRequest=197) it.</sub>

Patch:
diff --git a/README.md b/README.md
index 8ffc6ce..72c799b 100644
--- a/README.md
+++ b/README.md
@@ -504,7 +504,7 @@ data.
 
 ### How can I refetch swrv data to update it
 
-Swrv fetcher functions can be triggered on-demand by using the `revalidate`
+Swrv fetcher functions can be triggered on-demand by using the `mutate`
 [return value](https://github.com/Kong/swrv/#return-values). This is useful when
 there is some event that needs to trigger a revalidation such a PATCH request that
 updates the initial GET request response data.
diff --git a/package.json b/package.json
index 7b58d71..80b234f 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "swrv",
-  "version": "0.9.3",
+  "version": "0.9.4",
   "main": "./dist/index.js",
   "module": "./esm/index.js",
   "repository": "github:Kong/swrv",
diff --git a/src/use-swrv.ts b/src/use-swrv.ts
index 9c66493..e549caa 100644
--- a/src/use-swrv.ts
+++ b/src/use-swrv.ts
@@ -384,6 +384,7 @@ function useSWRV<Data = any, Error = any> (...args): IResponse<Data, Error> {
     watch(keyRef, (val) => {
       keyRef.value = val
       stateRef.key = val
+      stateRef.isValidating = Boolean(val)
       setRefCache(keyRef.value, stateRef, ttl)
 
       if (!IS_SERVER && !isHydrated && keyRef.value) {


Test patch:
diff --git a/tests/use-swrv.spec.tsx b/tests/use-swrv.spec.tsx
index e06b7ca..d412e24 100644
--- a/tests/use-swrv.spec.tsx
+++ b/tests/use-swrv.spec.tsx
@@ -683,6 +683,19 @@ describe('useSWRV - loading', () => {
 
     done()
   })
+
+  // #195
+  it('should return loading state isValidating with nullish key', async done => {
+    const vm = new Vue(defineComponent({
+      template: `<div>{{ error }}:{{this.isValidating ? 'loading' : 'ready'}}</div>`,
+      setup () {
+        return useSWRV(() => null)
+      }
+    })).$mount()
+
+    expect(vm.$el.textContent).toBe(':ready')
+    done()
+  })
 })
 
 describe('useSWRV - mutate', () => {