0

i have a problem with using serviceWorker - React - indexedDB the problem is i can get data from indexedDB in serviceWorker.js and i return this data but the react code cant receive the data and returns error here is the code (the request method is POST)

self.addEventListener('fetch', (event) => {
if (event.request.url.indexOf(API.getWords.api) !== -1) {
    const sign = event.request.url.slice(event.request.url.indexOf("=") + 1,event.request.url.indexOf("=").length);
    event.respondWith(
        fetch(event.request)
            .then((res) => {
                const clonedRes = res.clone();
                clonedRes.json()
                    .then((data) =>{
                        dbPromise
                            .then((db) => {
                                const tx = db.transaction(['dreams-word'],'readwrite');
                                const store = tx.objectStore('dreams-word');
                                store.put({
                                    sign,
                                    value: data,
                                });
                                return tx.done;
                            })
                    })
                return res;
            })
            .catch(() => {
                return dbPromise
                    .then((db) => {
                        const tx = db.transaction(["dreams-word"], 'readonly');
                        const store = tx.objectStore("dreams-word");
                        return store.get(sign);
                    })
                    .then((res) => {
                        console.log(res.value);
                        return res.value;
                    })
                    .catch((eerror) => {
                        console.log(eerror)
                        return caches.match('offline.html')
                    })
            })


    )
}

})

i am sure that i can take data from indexedDB but i think the problem is this part (the way of returning the result)

.catch(() => {
                return dbPromise
                    .then((db) => {
                        const tx = db.transaction(["dreams-word"], 'readonly');
                        const store = tx.objectStore("dreams-word");
                        return store.get(sign);
                    })
                    .then((res) => {
                        console.log(res.value);
                        return res.value;
                    })
                    .catch((eerror) => {
                        console.log(eerror)
                        return caches.match('offline.html')
                    })
            })
shahriar
  • 1
  • 1

0 Answers0